牛客 - 拿物品(贪心)

题目链接:点击查看

题目大意:给出 n 个物品,每个物品有两个属性代表价值,有两个玩家依次轮流拿物品,玩家一拿到物品后的贡献为属性一的累加,同理玩家二拿到物品后的贡献为属性二的累加,现在问两人都希望在拿完物品后的贡献比对方尽可能多,问两人会如何选择

题目分析:现在看来是一道很简单的贪心问题,但是在比赛的时候却没有反应过来,可能是被那两道简单但是坑非常奇怪的题卡崩了心态吧,贪心策略就是将每个物品的属性累加即可,因为当某个玩家选择了这个物品后,其贡献是相应的属性,而其余的那个属性则代表了对方拿不到这个物品所损失的代价,所以这个物品的贡献是 a + b,同理,无论轮到哪个玩家拿取时,每个物品的贡献都是 a + b ,所以按照这个值排序后,依次分配个两个玩家就好了

代码:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
 
typedef long long LL;

typedef unsigned long long ull;
 
const int inf=0x3f3f3f3f;
 
const int N=2e5+100;

struct Node
{
	int a,b,id;
	bool operator<(const Node& t)const
	{
		return a+b>t.a+t.b;
	}
}a[N];

int main()
{
//#ifndef ONLINE_JUDGE
//	freopen("input.txt","r",stdin);
//#endif
//	ios::sync_with_stdio(false);
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i].a);
		a[i].id=i;
	}
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i].b);
	sort(a+1,a+1+n);
	vector<int>ans1,ans2;
	for(int i=1;i<=n;i++)
	{
		if(i&1)
			ans1.push_back(a[i].id);
		else
			ans2.push_back(a[i].id);
	}
	for(int i=0;i<ans1.size();i++)
		printf("%d ",ans1[i]);
	printf("\n");
	for(int i=0;i<ans2.size();i++)
		printf("%d ",ans2[i]);
	printf("\n");
	
	
	
	
	
	
	
	
	
	return 0;
}
发布了646 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104204269