【HDU 1029】Ignatius and the Princess IV

“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?
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
注意,快读函数也不一定快,这里不如scanf
要意识到过半的数就是中位数

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define INF 0x7fffffff
#define N 1000000
using namespace std;
//要意识到这个数就是中位数??? 
//可以与dp无关 
inline int read(){
    
    
    int x=0;
    char c=getchar();
    bool flag=0;
    while(c<'0'||c>'9'){
    
    
        if(c=='-')
            flag=1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
    
    
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    }
    if(flag)
        x=-x;
    return x;
}
int a[N] ;
int main()
{
    
    
	int n;
	while(~scanf("%d",&n)){
    
    
		for(int i=0;i<n;i++){
    
    
			//scanf("%d",&a[i]);
			a[i]=read();
		}
		sort(a,a+n);
		printf("%d\n",a[(n+1)/2]);
	}
	return 0;
} 

法2:map stl

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<map> 
using namespace std;
map<int,int>vis;//注意不能用char*,因为是指针,地址不会变的,不能count 
//可以试试string 
int main()
{
    
    
	int n;
	while (~scanf("%d",&n))
	{
    
    
		//记得把map清空!!!重复错
		vis.clear(); 
		int flag=1;
		int out=(n+1)/2;
		for(int i=1;i<=n;i++)
		{
    
    
			int m;
			scanf("%d",&m);
			if(flag){
    
    
				vis[m]+=1;
				if(vis[m]>=out){
    
    
					printf("%d\n",m);
					flag=0;						
				}
			}
		}
	}
	return 0;
}

法三,string,不行

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<map> 
#include<string>
#include<iostream> 
#include<sstream>
using namespace std;
map<string,int>vis;//注意不能用char*,因为是指针,地址不会变的,不能count 
//可以试试string 
int main()
{
    
    
	int n;
	while (~scanf("%d",&n))
	{
    
    
		//记得把map清空!!!重复错
		vis.clear(); 
		int flag=1;
		int out=(n+1)/2;
		for(int i=1;i<=n;i++)
		{
    
    
			string s;
			stringstream ss;//以十六进制记录,并且不是原数 (很可能是指针),不能直接这么输出 (这体现的是int,stringstream,string的转化方法)
			int m;
			scanf("%d",&m);
			ss<<m;
			ss>>s;
			//不能cin>>m;是到\n结束 
			if(flag){
    
    
				vis[s]++;
				if(vis[s]>=out){
    
    
					cout<<s<<endl;
					flag=0;						
				}
			}
		}
	}
	return 0;
}
//结果:样例正确,超时 

猜你喜欢

转载自blog.csdn.net/qq_51945248/article/details/113820207