Number Game | ||||||
| ||||||
Description | ||||||
There are n items and two players, Kim and you. For each player and for each item, the value of the item for this player is known. Denote values of the i-th item for the first and the second player as ai and bi correspondingly. Players take the items in turns. Kim starts the game. Kim is greedy: each turn, he chooses the item which has the maximal ai among the remaining items. If there are several such items, he can take any one of them. What is the maximal possible sum of values bi of items taken by the second player that he can guarantee regardless of the first player’s moves? | ||||||
Input | ||||||
The first line is an integer T, describes the number of tests. Then T tests. Each case contains a single integer n, the number of items. The second line contains n numbers, i-th is equal to ai, the value of the i-th item for Kim. The third line contains n numbers, i-th is equal to bi, the value of the i-th item for the second player.
| ||||||
Output | ||||||
Output a single number: the maximal sum of values bi of items taken by the second player that he can guarantee. | ||||||
Sample Input | ||||||
1 5 1 2 3 4 5 2 3 4 5 6 | ||||||
Sample Output | ||||||
8 | ||||||
Hint | ||||||
1 <= n <= 1000 ai and bi are integers from 1 to 10e9 | ||||||
Source | ||||||
"科林明伦杯"哈尔滨理工大学第六届程序设计团队赛 |
题目大意:
给出N个物品,物品有两个属性,x,y,每一次对手都会拿x最大值的那个物品,如果有多个相同价值的物品,那么对应拿y最大值的那个物品,我们后手拿物品,问我们最高能够拿取的y属性的总价值是多少。
思路:
首先将所有物品排序(先按照x最大排序,再按y最大排序),那么接下来对于i%2==0的物品,就是我们可以拿的物品,i%2==1的物品,是按照顺序对手拿的物品。
那么我们将i%2==0的所有物品都放到优先队列中(最大值优先),然后对于i%2==1的物品,如果我们想要的话,必须要抛弃一个队列中的物品。
那么对应我们比较一下队头是否能够更优即可。
过程维护一下。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int x,y;
}a[150000];
int cmp(node a,node b)
{
if(a.x==b.x)return a.y>b.y;
return a.x>b.x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i].x);
for(int i=1;i<=n;i++)scanf("%d",&a[i].y);
sort(a+1,a+1+n,cmp);
priority_queue<int>s;
for(int i=2;i<=n;i++)
{
if(i%2==0)s.push(-a[i].y);
else
{
if(-a[i].y<s.top())
{
s.pop();
s.push(-a[i].y);
}
}
}
long long int ans=0;
while(!s.empty())
{
int u=s.top();s.pop();
ans+=-u;
}
printf("%lld\n",ans);
}
}