Elections 【结构体】

本文介绍了一个特殊的选举算法,该算法通过两阶段确定胜者。首先在每个城市中统计投票数最高的候选人,若出现平局则选择索引较小的候选人。接着在城市级别上应用相同原则选出最终获胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elections
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.

Input
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.

Output
Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.

Example
Input
3 3
1 2 3
2 3 1
1 2 1
Output
2
Input
3 4
10 10 3
5 1 6
2 2 2
1 5 7
Output
1
思路 :第一次看错题,WA了然后又忽略 边界值,又WA了一次。。代码能力太差了。。。有思路还有WA几次,,,,
代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define LL long long
#define M  1000000
#define inf 0x3f3f3f3f
#define mod 100009
using namespace std;
struct data
{
    LL num;
    LL sum;
};
data arr[M];
int cmp(data a,data b)
{
    if(a.sum!=b.sum) return a.sum>b.sum;
    return a.num<b.num;
}
int main()
{
    LL i,j;
    LL n,m;LL sum; 
    scanf("%lld%lld",&n,&m);
    for(i=1;i<=M;i++)
    {
        arr[i].num=i;
        arr[i].sum=0;
    }
    for(i=1;i<=m;i++)
    {
        LL maxx=0;
        LL next=1;// 一开始 赋值为0 了,WA了,如果都是 0票的话,也是要投1号的,边界值
        for(j=1;j<=n;j++)
        {
            LL k;
         scanf("%lld",&k);
         if(k>maxx) 
         {
            maxx=k;
            next=j;
         }
        }
         arr[next].sum++;       
    }
    sort(arr+1,arr+n+1,cmp);
    printf("%lld\n",arr[1].num);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值