Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 18038 Accepted Submission(s): 7281
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
Author
Ignatius.L
很简单的一道题,主要就是要明白求得是那个特殊的数字,所以可以将数列当做0,1数列来做,非0即1。还可以存到数组里,用dp,但是会超。dyx
#include <iostream>
using namespace std;
//方法想到,题很简单。
int main()
{
int N;//给定一个为N的奇数。
int t;//进行输入的数据。
int dyx;//设立一个计数器。
int ans;//最终需要输出的结果。
//这道题我们知道,这个特殊的数字,一定是最少//出现(N+1)/2次。
//在这个N个数的序列当中,这个特殊的数字一定///是出现次数最多的。
//则将输入的第一个数字赋值给ans,如果遇到相///同的数字,则计数器dyx++,如果遇到不同的数字//,计数器dyx--。
//因为特殊数字出现的次数肯定会大于其他数字出//现的次数。
while(cin>>N)
{
dyx=0;
for(int i=0;i<N;i++)
{
cin>>t;
//如果减到了0,则将当前输入的t值赋值给 //ans。
if(dyx==0)
{
dyx=1;
ans=t;
}
//因为我们只是需要在这个数列中寻找这个///特殊的数字,则可以将之看为只有两个数字的数列
//那么问题想起来就简单很多了,一个数字///至少出现(N+1)/2次
else
{
if(t==ans)
dyx++;
else
dyx--;
}
}
cout<<ans<<endl;
}
return 0;
}