Codechef 初学者 permut2:收到错误 - 它在 codechef 上给出了错误的答案,但给出了正确的示例输
Codechef beginner permut2: getting error -it gives wrong answer on codechef but gives correct for sample input
>问题:
整数 1 到 n 的排列是这些整数的排序。因此,表示排列的自然方法是按此顺序列出整数。当 n = 5 时,排列可能看起来像 2、3、4、5、1。但是,还有另一种表示排列的可能性:您创建一个数字列表,其中第 i 个数字是整数 i 在排列中的位置。让我们把这第二种可能性称为逆排列。上述序列的逆排列为 5, 1, 2, 3, 4。模糊排列是无法与其逆排列区分开来的排列。例如,排列 1、4、3、2 是不明确的,因为它的逆排列是相同的。为了摆脱这些烦人的示例测试用例,您必须编写一个程序来检测给定的排列是否模棱两可。
输入规范
输入包含多个测试用例。
每个测试用例的第一行包含一个整数 n(1 ≤ n ≤ 100000)。然后,整数 1 到 n 的排列在下一行之后。连续整数之间只有一个空格字符。您可以假设 1 到 n 之间的每个整数在排列中只出现一次。最后一个测试用例后跟零。
输出规格
对于每个测试用例输出,排列是否不明确。 遵循示例输出中显示的格式。
示例输入
4
1 4 3 2
5
2 3 4 5 1
1
1
0
答:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int t;
while(true){
scanf("%d",&t); // no of digits/numbers
if(t ==0) //if its 0 then break
break;
int a[t+2],i=1;
while(t--){
scanf("%d",&a[i++]); // take t numbers
}
int f=0;
for(int j=1;j<i;j++){
int p=a[j]; //for every position take array value at that position
f=0;
if(a[p]!= j){ //for array value at that position check if its equal to index according to sample input or output
f=1; // if fails for any digit then break loop and not ambiguous
break;
}
}
if(f==1)
printf("not ambiguousn"); //inverse ambiguous
else
printf("ambiguousn"); //not ambiguous
}
return 0;
}
问题是您无法在函数中创建如此大的(100000 个元素)数组。您应该创建一个全局数组:
using namespace std;
const int MaxN = 100000;
int a[MaxN+2];
int main(){
...
}
相关文章:
- 递归求和任务的错误答案
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 寻找最小楼梯成本的动态规划问题的错误答案
- 为什么可变大小的数组会导致Codechef出现错误答案
- 如何修复此代码对几个测试用例的错误答案?
- 使用幂函数的计算给出了大数字的错误答案
- 快速排序算法,一些特定输入序列的错误答案和分段错误
- 二分法程序的错误答案(C++)
- 无法理解绘图程序中的错误答案
- C++ - 整数除以整数返回错误答案
- 布尔函数的错误答案
- SPOJ FASTFLOW上的错误答案
- 代码的错误答案是在Java Camel案件和C 下划线标识符之间转换的错误答案
- 200万以下所有素数的总和,线程给出错误答案
- 0-1整数背包返回错误答案(动态编程)
- UVa 在线判断 - 3n + 1 - 错误答案
- 提升C++原始算法错误答案
- 二项式系数函数C++错误答案 n>13
- 需要帮助以避免 SPOJ 上的"错误答案"