CodeForces(503) 1019A Elections(枚举)

本文介绍了一道关于选举中如何通过最小花费确保某政党获胜的算法题目。通过枚举改变选票数量的方法,实现了计算最优解决方案的目标。文章详细阐述了解决方案的思路与实现过程。

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

                                                                                Elections

                                                                       time limit 2 seconds

As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.

Elections are coming. You know the number of voters and the number of parties — nn and mm respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give ii-th voter cici bytecoins you can ask him to vote for any other party you choose.

The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.

Input

The first line of input contains two integers nn and mm (1≤n,m≤30001≤n,m≤3000) — the number of voters and the number of parties respectively.

Each of the following nn lines contains two integers pipi and cici (1≤pi≤m1≤pi≤m, 1≤ci≤1091≤ci≤109) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision.

The United Party of Berland has the index 11.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples

input

1 2
1 100

output

0

input

5 5
2 100
3 200
4 300
5 400
5 900

output

500

input

5 5
2 100
3 200
4 300
5 800
5 900

output

600

题意:给出每个人的初始投票对象,和改变每个人投票对象的花费,问让1团队赢的最小花费.

思路:枚举总共改变的人数sum,如果一个团队被支持的人数比1号团队多的数量k大于等于总共改变的人数,那么就需要至少改变此团队的人数为k-sum+1,如果最后还有剩余,再按照花费从小到大来,sum从0枚举到n,取最小值.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

struct P
{
	int v;
	int pos;
	int par;
	P(){}
	P(int v,int pos,int par):v(v),pos(pos),par(par){}
	friend bool operator < (P x,P y)
	{
		return x.v< y.v;
	}
}b[5321];

struct node
{
	int num;
	vector<P> a;
	friend bool operator < (node x,node y)
	{
		return x.num< y.num;
	}
} e[5321];

int n,m; 
int vis[5321];

ll solve(int sum)
{
	mem(vis,0);
	ll ans = 0;
	int k = e[1].num;
	int tmp = sum;
	for(int i = 2;i<= m;i++)
	{
		if(e[i].num-k>= sum)
		{
			int all = e[i].num-k-sum+1;
			tmp-= all;
			for(int j = 0;j< all&&j< e[i].a.size();j++)
			{
				vis[e[i].a[j].pos] = 1;
				ans+= e[i].a[j].v;
			}
			if(tmp< 0) break;
		}
	}
	
	if(tmp< 0) return (ll)1e15;
	for(int i = 1;(i<= n)&&tmp> 0;i++)
	{
		if(vis[b[i].pos]||b[i].par == 1) continue;
		ans+= b[i].v;
		tmp--;
	}
	return ans;
}

int main()
{
	cin>>n>>m;
	
	for(int i = 1;i<= n;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		e[x].num++;
		e[x].a.push_back(P(y,i,x));
		b[i] = P(y,i,x);
	}
	
	for(int i = 1;i<= m;i++)
		sort(e[i].a.begin(),e[i].a.end());
	
	sort(e+2,e+m+1);
	sort(b+1,b+n+1);
	
	ll ans = (ll)1e15;
	for(int i = 0;i<= n;i++)
		ans = min(ans,solve(i));
	
	cout<<ans<<endl;
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值