zoj 3715 K - Kindergarten Election

一个班有n个小朋友,要选一个班长,(n>=3),每个人不能投自己,给出每个人想要选的人,1号小朋友相当班长,如果一个小朋友不选自己

那么,自己可以给他bi个糖果让他选自己,那么请输出,最少花费多少个糖果 ,1号小朋友可以当上班长

开始的做法是贪心小的,那么问题很复杂,直接枚举小的是不符合条件的

后来想,枚举一号小朋友最后的票为k,那么其他人的票肯定小于等于k-1,如果有小朋友的选票高于k-1,那么先把投这个人的所有小朋友贿赂到小于k-1,按照需要的糖果数量

如果这个时候的选票大于k了,那么显然这个k不符合条件,如果等于k,那么用现在的花费更新答案,如果小于k,那么再从不选自己的小朋友中挑选需要糖果最少的,补足k张票

直接贪心无法做,那么通过枚举一个值,那么减少条件,从而能贪心

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
int a[maxn],b[maxn];
vector<int> g[maxn];

int main(){
	int t,n,m;

	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<maxn;i++)
			g[i].clear();

		for(int i=2;i<=n;i++)cin>>a[i];
		for(int i=2;i<=n;i++)cin>>b[i],g[a[i]].push_back(b[i]);
		int ans=INT_MAX;
		for(int i=1;i<=n;i++)sort(g[i].begin(),g[i].end());

		int hhh=g[1].size();
		if(hhh<1)
			hhh=1;
		for(int k=hhh;k<=n;k++){
			int res=0;
			int cnt=g[1].size();

			priority_queue<int,vector<int>,greater<int> > q;
			while (!q.empty()) q.pop();

			for(int i=2;i<=n;i++){
				int j=0;
				if(g[i].size()>=k){
					for(;j<g[i].size()-k+1;j++){
						res+=g[i][j],cnt++;
					}
				}
				for(;j<g[i].size();j++)q.push(g[i][j]);
			}
			if(cnt>k)
				continue;
			while(cnt<k){
				cnt++;
				res+=q.top();
				q.pop();
			}

			ans=min(ans,res);
		}

		cout<<ans<<endl;
	}
	return 0;
}

  

时间: 2024-10-06 08:30:21

zoj 3715 K - Kindergarten Election的相关文章

ZOJ 3715 Kindergarten Election(枚举+贪心)

Kindergarten Election Time Limit: 2 Seconds      Memory Limit: 65536 KB At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for convenience) in class need to elect their new leader. The ith kid will vote for his

ZOJ - 3715贪心

ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会把票投给1号小朋友而不是他最好的朋友,对于不同的小朋友贿赂的花费也不同,1号小朋友想要自己是唯一的班长(票数最高),问他最少需要花费多少糖果? 由题目来想,很容易想到贪心,但是不知道怎么贪心.如果是简单让1号是票数最高的小朋友,他每次贿赂都有两种选择,一种是贿赂花费小的人来投他,另一种是贿赂票数比他

ZOJ 3599 K倍动态减法游戏

下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可以看作 Fibonacci 博弈的扩展,建议没弄懂 Fibonacci博弈的先学那个,个人整理 http://blog.csdn.net/tbl_123/article/details/24033245 : 而说扩展体现在数列不再是Fib数列,是根据 k 的值自行构造的,其它换汤不换药,具体构造方法

融合后如何如何后如何如何

http://ypk.39.net/search/all?k=%A6%C6%B6%F5%D6%DD%C4%C4%C0%EF%C2%F2%C3%C0%C9%B3%CD%AAQ%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%C6 http://ypk.39.net/search/all?k=%A8z%BE%A3%C3%C5%C4%C4%C0%EF%C2%F2%C3%C0%C9%B3%CD%AAQ%A3%BA%A3%B8%

哥哥ukulele

http://ypk.39.net/search/all?k=%A1%FB%C6%CE%CC%EF%C4%C4%C0%EF%D3%D0%B0%B2%C0%D6%CB%C0%D2%A9%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%BC http://ypk.39.net/search/all?k=%A1%D3%B8%A3%C7%E5%C4%C4%C0%EF%D3%D0%B0%B2%C0%D6%CB%C0

ZOJ 3212 K-Nice (K)

K-Nice Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge This is a super simple problem. The description is simple, the solution is simple. If you believe so, just read it on. Or if you don't, just pretend that you can't see this on

ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)

Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They

zoj 2112 Dynamic Rankings(主席树&amp;动态第k大)

Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They

zoj 3635 Cinema in Akiba (树状数组求第K大)

Cinema in Akiba Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies wit