Number Game
来源:互联网 发布:ubuntu键盘左alt没反应 编辑:程序博客网 时间:2024/05/15 13:32
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?
InputThe 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 a single number: the maximal sum of values bi of items taken by the second player that he can guarantee.
Sample Input Sample Output Hint1 <= n <= 1000
ai and bi are integers from 1 to 10e9
题目大意:你和另一个人Kim在玩轮流一个游戏,对方先开始,每次从ai组成的数组里面选取剩余的最大的数,然后轮到你从bi组成的数组里选择,每次已经被选的下标为i的数字双方都不能再一次选择。让你编写程序算出你选择的数字的和最大可以是多少。
输入:第一行为T,代表一共T组数据,每组数据有n,代表双方都是n个数字
接下来两行是ai和bi。
输出:你选择的数字最大和。
分析:首先用一个结构体把ai,bi存下来,然后以ai的大小从大到小排序,ai相同的以bi从大到小排序。那么如果对对方最有利的,而对自己的最后结果最小的就是ai选择0,2,4,6……,bi选择剩下的。首先对方第一个选择的肯定是第0个ai,从第二个开始,到你选择了,先把你最坏的结果存进一个小顶堆,那么下次你遇到比堆顶大的元素,此时把堆顶的元素删除,加入这个元素,就按照这样的方法贪心,最后可以贪心得到最后结果为和最大的选择。
#include<iostream>#include<cstdio>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{ int a,b;};bool cmp(node n,node m){ if(n.a!=m.a) return n.a>m.a; else return n.b>m.b;}priority_queue<int ,vector<int>,greater <int > >que;int main(){ struct node std[10000]; int n,t,m,i; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&std[i].a); } for(i=0;i<n;i++) { scanf("%d",&std[i].b); } sort(std,std+n,cmp); for(i=1;i<n;i++) { if(i%2!=0) { que.push(std[i].b); } else if(std[i].b>que.top()) { que.pop(); que.push(std[i].b); } } long long sum=0; while(!que.empty()) { sum+=que.top(); que.pop(); } printf("%lld\n",sum); } return 0;}
- Number Game
- Number Game
- Number Game
- The four number game
- ZOJ 1039 Number Game
- POJ 1143 Number Game
- POJ 1143 Number Game
- [院赛]A number game
- ZOJ - 3180 Number Game
- poj1143(Number Game)
- zoj 1039 Number Game
- POJ 1143 Number Game
- poj 1143 Number Game
- zoj 1039 Number Game
- zoj 3180 Number Game
- POJ-1143-Number Game
- toj3071 Number Game
- zoj1039 Number Game
- BZOJ 1623: [Usaco2008 Open]Cow Cars 奶牛飞车
- 一些重要的文档收藏起来
- 大数阶乘--------------------------南阳理工学院题目28
- Sparse Graph HDU
- PHP:客户端禁用cookie之后如何使用session
- Number Game
- 2743:字符串判等
- 原生js节点操作
- Hibernate实体关系映射—Annotation
- ceph固化实验(一)
- 理解Angular中的$apply()以及$digest()
- C/C++中的预定义宏
- gcc编译与gdb调试
- PopUpWindow使用详解(一)——基本使用