Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 17490 Accepted Submission(s): 7060
Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
Sample Output
3 5 1比较水的题,代码量少,但是比较古怪,比较难想到怎么做省时间。之前想都没想就用的结构体数组,然后超时了,(⊙o⊙)…这种数据很大的一般都要找找规律的,不然就超时了,还有之前没注意到可以是负数,还多错了一次,以后要注意了!想法:题目说那个特别的数超过(n+1)/2 ,所以每次减去两个不同的数不会影响那个最多的数, 即新序列中的那个最多数还是那个旧序列中的最多数, 比如:7 : 1 2 3 2 -1 2 2 依次减去1 2, 3 2,-1 2 , 然后就剩下2了,具体代码实现贴在下面:#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main() { int n, num, count, c; while(scanf("%d", &n)!=EOF) { count = 0; for(int i = 0; i < n; i++) { scanf("%d", &num); if(count == 0) { c = num; count = 1; } else { if(num == c) count++; else count--; } } printf("%d\n", c); } return 0; }