Elections CodeForces - 1020C 枚举+贪心
作者:互联网
题意:
有n个人,m个党派,现在给出每个人给哪个党派投了票,以及让他们改票所需钱数。
问至少需要多少钱,使得第一个党派的票数严格大于其他人。
思路:
按照费用从小到大排序。
我们枚举最大的得票数为i,那么当有一个党派的票数大于i时,修改党派票数,让其减1,1党派票数+1。
最后判断一下即可。
代码:
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#include<string.h>
#include<queue>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=3000+6;
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
bool vis[maxn];
int cnt[maxn];
int num[maxn];
struct node
{
int id;
int p;
ll c;
} a[maxn];
bool cmp(node a,node b)
{
return a.c<b.c;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(num,0,sizeof(num));
rep(i,1,n)
{
cin>>a[i].p>>a[i].c;
a[i].id=i;
num[a[i].p]++;
}
sort(a+1,a+n+1,cmp);
ll ans=INF;
rep(i,0,n-1)
{
ll tmp=0;
memset(vis,0,sizeof(vis));
rep(j,1,m)
{
cnt[j]=num[j];
}
for(int j=1; j<=n; j++)
{
if(a[j].p==1)continue;
if(cnt[a[j].p]>i)
{
cnt[1]++;
cnt[a[j].p]--;
tmp+=a[j].c;
vis[j]=1;
}
}
if(cnt[1]<=i)
{
rep(j,1,n)
{
if(cnt[1]>i)
break;
if(vis[j])
continue;
if(a[j].p==1)continue;
tmp+=a[j].c;
cnt[1]++;
}
}
if(cnt[1]>i)
{
ans=min(ans,tmp);
}//cout<<tmp<<"+++"<<endl;
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
}
Preeee 发布了1019 篇原创文章 · 获赞 63 · 访问量 8万+ 关注
标签:cnt,1020C,int,CodeForces,Elections,vis,num,maxn,include 来源: https://blog.csdn.net/lanshan1111/article/details/104433291