hdu4727题意很难搞懂

来源:互联网 发布:windows 禁止卸载软件 编辑:程序博客网 时间:2024/06/08 03:00

The Number Off of FFF


X soldiers from the famous " *FFF* army" is standing in a line, from left to right.
You, as the captain of *FFF*, decides to have a "number off", that is, each soldier, from left to right, calls out a number. The first soldier should call "One", each other soldiershould call the number next to the number called out by the soldier on his left side. Ifevery soldier has done it right, they will call out the numbers from 1 to X, one by one, fromleft to right. 
Now we have a continuous part from the original line. There are N soldiers in the part. So inanother word, we have the soldiers whose id are between A and A+N-1 (1 <= A <= A+N-1 <= X).However, we don't know the exactly value of A, but we are sure the soldiers standscontinuously in the original line, from left to right. 
We are sure among those N soldiers, exactly one soldier has made a mistake. Your task is tofind that soldier. 


Input
The rst line has a number T (T <= 10) , indicating the number of test cases. 
For each test case there are two lines. First line has the number N, and the second line has N numbers, as described above. (3 <= N <= 10 5
It guaranteed that there is exactly one soldier who has made the mistake.

Output
For test case X, output in the form of "Case #X: L", L here means the position of soldier among the N soldiers counted from left to right based on 1.


题意引申:有N个人在一起报数,本来是从1 ....N, 到有一个人报错了。现在从这N个人中选出n个人,3<=n<=10^5;

这n各种中保证有一个是报错的,要你求出这个人在n个数中的第几个的位置;



因为是从左往右报数,就是从小的往大的报数,所以当前位置的数若减去前面的数的差不为一,那么错误的肯定是这个数,因为判断是从小的到大的,前面的是已经判断的,肯定是正确的;如果这n个数都符合要求,根据题意一定有一个是错的,那么第一个肯定是错的


#include <bits/stdc++.h>using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,-1,-1,1,1};const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=1e6+100;const double EPS=1e-7;const int mod=1000000007;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}LL ans[100005];int main(){    int t;    scanf("%d", &t);    for(int cnt = 1; cnt <= t; cnt++)    {        me(ans);        LL n;        scanf("%lld", &n);        for(int i = 0; i < n; i++)        {            scanf("%lld", &ans[i]);        }        int res;        int flag = 1;        for(int i = 0; i < n - 1; i++)        {            if(ans[i + 1] - ans[i] != 1)            {                flag = 0;                res = i + 1;                break;            }        }        if(flag)            printf("Case #%d: 1\n", cnt);        else            printf("Case #%d: %d\n", cnt, res + 1);    }    return 0;}


0 0
原创粉丝点击