Elections
来源:互联网 发布:mac pro 分辨率设置 编辑:程序博客网 时间:2024/05/02 22:53
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.
The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.
At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.
Determine who will win the elections.
The first line of the input contains two integers n, m (1 ≤ n, m ≤ 100) — the number of candidates and of cities, respectively.
Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≤ j ≤ n, 1 ≤ i ≤ m, 0 ≤ aij ≤ 109) denotes the number of votes for candidate j in city i.
It is guaranteed that the total number of people in all the cities does not exceed 109.
Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.
3 31 2 32 3 11 2 1
2
3 410 10 35 1 62 2 21 5 7
1
Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.
Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
思路:
每一行就是一个城市,而且是从1-m的城市编号。而每一列就是候选人1-n个,每行找最大的票数,相同就选候选人编号最小的。每行找完后,统计哪个候选人最多票数就行了。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;int main(){ /*freopen("input.txt","r",stdin);*/ int n,m,i,j,k,num; int cnt[210]; while(cin>>n>>m) { memset(cnt,0,sizeof(cnt)); int ma; for(i=1;i<=m;++i) { ma=0;num=1;//一开始num未赋初值,所以当输入k全为0时就运行错误了
for(j=1;j<=n;++j) { cin >> k; if(ma<k) ma=k,num=j; } cnt[num]++; } ma=0;k=1; for(i=1;i<=n;++i) { if(ma<cnt[i]) ma=cnt[i],k=i; } cout << k << endl; } return 0;}
- Elections
- Evaluating Elections
- A. Elections
- CodeForces570A - Elections
- ural 1263 Elections
- UVa 11748: Rigging Elections
- uva 11748 - Rigging Elections
- codeforces 458C Elections
- URAL 1263. Elections
- Codeforces 570A Elections
- Codeforces 570 A. Elections
- CodeForces-570A Elections
- Codeforces 570 A. Elections
- Bear and Elections
- A. Bear and Elections
- URAL 1263 Elections
- UVA 11748 Rigging Elections
- Codeforces 570A Elections
- dlopen failed: empty/missing DT_HASH in "libx.so" (built with --hash-style=gnu?)
- 2015 Multi-University Training Contest 2(hdu 5300 - hdu 5309)
- 如何改变NSPopover的背景颜色(包括小三角部分)?
- 查看发行版本信息
- python 定时器
- Elections
- 付出就是成功的根源
- java实现城市电话号码, 超长正整数相加, 循环报数.
- js调用安卓和ios代码
- 虚拟存储和进程空间
- 如何组织召开项目的评审会议
- .NET Word 文件格式转换
- 实现android基于百度的定位
- js 动态原型(prototype)模式