跳到正文

题解

题解:UVA10374 Election

baokun约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月22日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
2
题目
UVA10374

版本

  1. v1当前

UVA10374 Election 题解

题意

先给 nn 个人名和其对应的政党名,再给 mm 张票投给个人,求票数最多的候选人的政党。如果有多个候选人同票就输出 tie。

思路

直接模拟,由于数据很水 nn 只有 20,所以可以用结构体直接把每个人的信息按下标存下来,然后暴力遍历。

代码

C++
#include <bits/stdc++.h>
#define int long long
constexpr int N = 100+5;
using namespace std;
typedef pair<int,int> P;
struct node{
	string dang;
	int tot;
}e[N];
int T,n,m,idx=0;
map<string,int>id;
void print(int cnt,int ans){
	if(cnt <= 1)
		cout<<e[ans].dang<<'\n';
	else
		cout<<"tie"<<'\n';
	return;
}
signed main(){
	scanf("%d\n",&T);
	while(T--){
		id.clear();
		string name,pol,vot;
		int Max=0,now=0,ans=0;
		scanf("\n%d\n",&n);
		for(int i = 1 ; i <= n ; i++){
			getline(cin,name);
			getline(cin,pol);
			if(!id[name]) id[name] = ++idx;
			e[id[name]].tot = -1;
			e[id[name]].dang = pol;
		}
		scanf("%d\n",&m);
		for(int i = 1 ; i <= m ; i++){
			getline(cin,vot);
			int nid = id[vot];
			if(e[nid].tot != 0){
				if(e[nid].tot == -1) e[nid].tot = 1;
				else e[nid].tot += 1;
			} 
		}
		for(auto p : id){
			int nid = p.second;
			int tot = e[nid].tot;
			if(Max < tot){
				Max = tot;
				now = 1;
				ans = nid;
			}
			else if(Max == tot){
				now++;
			}
		}
		print(now,ans);
		if(T) cout<<'\n';
	}
}
原文
存档评论1 条抓取于