您的位置:首页 > 其它

2199. [HZOI 2016] 活动投票

2017-05-22 20:20 204 查看
★★ 输入文件:
hztp.in
输出文件:
hztp.out
简单对比

时间限制:0.5 s
内存限制:2 MB


【题目描述】

衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知一名参赛选手票数超过半数,求其参赛号(参赛号随机)


【输入格式】

第一行一个整数n

第二行n个整数Ni 代表第i个学生所投选手的参赛号


【输出格式】

超过半数选手的参赛号


【样例输入】

10

5 1 2 5 5 2 3 5 5 5


【样例输出】

5


【提示】

100%的数据中:n ≤3000000,1 ≤ Ni ≤300000000;


【来源】

HZOI 2016

相同则加,不同则减

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int n;
int maxn;
int num=1;
int p;
int main()
{

freopen("hztp.in","r",stdin);
freopen("hztp.out","w",stdout);
//ios::sync_with_stdio(false);
//scanf("%d",&n);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&p);
if(p!=maxn)
{
num--;
if(num<0)
{
maxn=p;
num=1;
}
}
else
num++;
}
cout<<maxn;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: