首页 > 其他分享 >题解:CodeForces 1019 A Elections[贪心/三分]

题解:CodeForces 1019 A Elections[贪心/三分]

时间:2024-07-14 13:57:31浏览次数:11  
标签:std int 题解 inp number CodeForces ++ Elections input

CodeForces 1019A

A. Elections

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

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 — \(n\) and \(m\) 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 \(i\)-th voter \(c_i\) 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 \(n\) and \(m\) (\(1 \le n, m \le 3000\)) — the number of voters and the number of parties respectively.

Each of the following \(n\) lines contains two integers \(p_i\) and \(c_i\) (\(1 \le p_i \le m\), \(1 \le c_i \le 10^9\)) — 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 \(1\).

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

这题有两种解法

第一种解法:贪心暴搜

根据题意,一共有N票,M个候选人
数据范围是[1,3000],比较小,可以暴搜
先要把每一票记录下来,按花费从小到大排序
我们假设最终每个候选人不超过k票,而我们的目标(1号候选人)至少要有k票
把达成这个目标的成本记录下来,从1到N遍历求最小值就好了(我尝试过N/2也能过,差异不大)
那么这个计算的过程是这样的:
按花钱从小到大的顺序遍历,
假如说这票是1号的,就跳过
假如说这个投票人投票的对象的票数 < k,暂且跳过
假如说这个投票人投票的对象的票数 > k,那就把这票抢过来
如果完成上面三个过程之后,1号的票数还是 < k,那就按照花费从小到大的顺序,还没抢过来的就抢过来,直到 ≥ k 为止。

第二种解法:三分

在用三分解决这道题目之前,首先你要了解什么是三分(不知道就点这里)
剩下的就很简单了,这个图像可以近似看成一个方向向下的凹函数,套上板子就好了

我的代码

#include <bits/stdc++.h>
#define int long long

struct node{
	int peo,cost;
	bool operator < (const node &a){
		return this->cost < a.cost;
	}
};

int n,m;
const int N = 3010;
node inp[N];
std::vector<int> num(N);

int check(int max_c)
{
	int ans = 0;
	bool book[N];
	std::vector<int> p(num);
	for(int i = 0 ; i < n ; i ++)
	{
		if(inp[i].peo == 1) book[i] = true;
		else if(p[inp[i].peo] < max_c) book[i] = false;
		else
		{
			book[i] = true;
			p[1] ++;
			p[inp[i].peo] --;
			ans += inp[i].cost;
		}
	}
	

	for(int i = 0 ; i < n && p[1] < max_c; i ++)
	{
		if(book[i] == true) continue;
		else
		{
			p[1]++;
			ans += inp[i].cost;	
		}
	}
	
	return ans;
}


signed main()
{
	std::cin >> n >> m;
	for(int i = 0 ; i < n ; i ++)
	{
		std::cin >> inp[i].peo >> inp[i].cost;
		num[inp[i].peo]++;
	}
	std::sort(inp,inp + n);
	
//贪心暴搜代码 
	/*	
	int ans = check(1);
	for(int i = 2 ; i <= N/2 + N%2 ; i ++)
	{
		ans = std::min(ans,check(i));
	}
	
	std::cout << ans;
	*/
	
//三分代码	
	int l=0,r=n-1;
	while(l < r-1)
	{
		int mid = l + r >> 1;
		int mmid = r + mid >> 1;
		if(check(mid) > check(mmid))
			l = mid;
		else
			r = mmid;
	}
	
	std::cout << std::min(check(l),check(r));
	return 0;
}

PS:

思路参考:(大佬链接指路)



有什么出现纰漏的地方还请大家在评论区指出!谢谢!

标签:std,int,题解,inp,number,CodeForces,++,Elections,input
From: https://www.cnblogs.com/jiejiejiang2004/p/18301485

相关文章

  • [CF1941E] Rudolf and k Bridges 的题解
    题目大意在第\((i,j)\)个格子修建一个桥墩需要\(a_{i,j}+1\)的花费而且要求\((i,0)\)与\((i,m)\)必须修建桥墩并且桥墩之间的距离不得大于\(d\)。现在需要求见\(k\)个连续的桥,求最小代价。其中\(1\lek\len\le100,3\lem\le2\cdot10,1\led\lem\)。思路因为......
  • [ABC206E] Divide Both 的题解
    题目大意求出从\(l\)至\(r\)中满足以下条件的\((x,y)\)的个数。\(\gcd(x,y)\ne1\)且\(\gcd(x,y)\nex\)且\(\gcd(x,y)\ney\)。其中\(1\lel\ler\le10^6\)。思路正难则反,所以可以求出所有互质或者是相互倍数的\((x,y)\)的对数,在将其减去所有的方案数就是......
  • [CF1178D] Prime Graph 的题解
    题目大意构造一个简单无向图,是所有的有度的点的度都是质数而且总共的边的数量的个数是质数。思路因为需要让所有的入度都为质数,所以我们可以找到两个相邻的质数\(2,3\),因为这样即使增加了一条边那么这个节点的度也是质数。先将这个图构成一个巨大的环,接着如果所有的边数并不......
  • [CF1538F] Interesting Function 的题解
    题目大意给定两个正整数\(l,r\),将\(l\)不断加\(1\)直到\(l=r\),求出这一过程中\(l\)发生变化的位数总数。\(1\lel<r\le10^9\)。思路假设从\(l\)处理到\(r\)变化的次数为\(f(l,r)\)。因为直接求解出\(f(l,r)\)十分困难,所以可以通过求出\(f(0,l)\)......
  • 题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]
    CodeForces1511CC.YetAnotherCardDeckDescriptionYouhaveacarddeckof\(n\)cards,numberedfromtoptobottom,i. e.thetopcardhasindex\(1\)andbottomcard —index\(n\).Eachcardhasitscolor:the\(i\)-thcardhascolor\(a_i\......
  • 「ABC217F」Make Pair 的题解
    题目大意一共\(2N\)个学生站成一排,其中有\(M\)对朋友关系。老师每次从队列中挑出两个相邻的学生作为同桌。为了关系和睦,每次选出的两个学生必须是朋友关系。选出的两个学生离开队列,空出来的位置左右合拢。请问老师有多少种方式选完所有学生?对于两种选人的方案,即使同桌关系相......
  • [COCI2015-2016#6] PAROVI 的题解
    题意选择一些\(n\)一下互质的二元组\(\{a,b\}\),求对于任意\(x\in\big[2,n\big]\)都不满足\(a,b<x\)和\(a,b\gex\)的个数。简化题意因为无解的情况只发生在所有的\(\{a,b\}\)之间没有多余的位置用于放置\(x\),所以题意可以抽象成这样:选择一些区间互质的区间\([a......
  • [COCI2006-2007#4] ZBRKA 的题解
    题目大意在一个长度为\(n\)的排列中找出逆序对数量恰好为\(c\)的排列总数,其中\(1\len\le10^3,1\lec\le10^4\)。思路考虑将\(1\)到\(n\)这些数从小到大一次填进去,因为每一次填入的数多是最大的,所以逆序对增加的数量只与其所在的位置相关,所以设计\(f_{i,j}\)表......
  • Snow Walking Robot 的题解
    题目大意给你一个机器人和机器人的\(n\)个运动,要求你在给出的运动路径的基础上设计一种不会走重复的路径的方法,注意只能减少原来的步数而不能增加,其中\(1\len\le10^5\)。思路因为这道题目可以自由的配置路径并且要求机器人在最后回到原来的位置,那么就应该要到一种适合所有......
  • [EGOI2021] Luna likes Love 的题解
    题目大意有\(2\timesn\)个人站成一排,然后给每个人分配一个\(1\)至\(n\)之间的数字,每种数字出现\(2\)次。现在,你可以进行两种操作:删除操作,将数字相同且相邻的两人删除,删除后两端剩下的队列合并。交换操作,交换相邻两个人的位置。每次,问至少操作多少次能够删除所有人......