- http://ac.nbutoj.com/Problem/view.xhtml?id=1173
-
[1173] Birdlike Angry Pig
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
-
There's no end to revenge. Pigs like to eat birds' eggs, so birds decide to revenge. For this, pigs decide to revenge as well. Pigs give a task to a birdlike pig. They let he sneak into the birds group so that they can destroy the birds group.
Every bird has a ID number (It may not be unique). To let pigs know, the birdlike pig makes the "AND operation" amount all the birds' ID number and sign himself to the answer.For example: Birds are signed as 5, 3, 4. So the birdlike pig signs himself as (5 & 3 & 4 = 0).
One day, a group birds pass by pigs' home. So the angry pig want to know whether the birdlike pig is in. - 输入
-
This problem has several cases. The first line of each case is an integer N (2 <= N <= 100 000). Then follows a line with N integers, indicates the ID of each bird/birdlike pig.
- 输出
-
For each case, if you can find the birdlike pig then output the ID of birdlike pig. Or output 'CAUTION: NO BIRDLIKE'.
- 样例输入
-
5 4 0 3 4 5
- 样例输出
-
0
- 思路:
- 题目的意思为 鸟猪 伪装在 鸟群里面,鸟猪的值为鸟群值的与集(&),设鸟猪为d,鸟群为a,b,c,则a&b&c=d;
- 现在题目给出一个序列,问是否鸟猪在这个序列之中,所以序列只要满足一个数的值等于其他数的相与集(&),则这个数就是鸟猪;
- 现在我先定义三个数组A,B,C;A里面存输入的数,B里面存着的是A数组里面前n项的与集(&),C里面存着的是A数组里面后n项的与集(&);
- 假设A里面存着a,b,c,d;
- A ==> a b c d
- B ==> a a&b a&b&c a&b&c&d
- C ==> a&b&c&d b&c&d c&d d
不难发现:如果A[i]==B[i-1]&C[i+1],则A[i]即为鸟猪
代码:C++
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<algorithm> 5 #include<string.h> 6 #include<string> 7 #include<ctime> 8 #include<queue> 9 #include<list> 10 #include<map> 11 #include<set> 12 #define INF 999999999 13 #define MAXN 10000000 14 using namespace std; 15 16 int a[100010],b[100010],c[100010]; 17 int main() 18 { 19 int n; 20 while(~scanf("%d",&n)) 21 { 22 int i; 23 for(i=1;i<=n;i++) 24 scanf("%d",&a[i]); 25 b[0]=-1; 26 for(i=1;i<=n;i++) 27 b[i]=(b[i-1]&a[i]); 28 c[n+1]=-1; 29 for(i=n;i>0;i--) 30 c[i]=(c[i+1]&a[i]); 31 for(i=1;i<=n;i++) 32 if((b[i-1]&c[i+1])==a[i]) 33 { 34 printf("%d\n",a[i]); 35 break; 36 } 37 if(i>n) 38 printf("CAUTION: NO BIRDLIKE\n"); 39 } 40 return 0; 41 }