Codechef 初学者 permut2:收到错误 - 它在 codechef 上给出了错误的答案,但给出了正确的示例输

Codechef beginner permut2: getting error -it gives wrong answer on codechef but gives correct for sample input

本文关键字:错误 答案 初学者 permut2 它在 codechef Codechef      更新时间:2023-10-16

>问题

整数 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(){
...
}