2018SDIBT_国庆个人第二场
Posted 一只小毛球
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018SDIBT_国庆个人第二场相关的知识,希望对你有一定的参考价值。
C.1038C Gambling
Two players A and B have a list of nn integers each. They both want to maximize the subtraction between their score and their opponent‘s score.
In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent‘s list (assuming his opponent‘s list is not empty).
Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements {1,2,2,3}{1,2,2,3} in a list and you decided to choose 22 for the next turn, only a single instance of 22 will be deleted (and added to the score, if necessary).
The player A starts the game and the game stops when both lists are empty. Find the difference between A‘s score and B‘s score at the end of the game, if both of the players are playing optimally.
Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent‘s score, knowing that the opponent is doing the same.
The first line of input contains an integer nn (1≤n≤1000001≤n≤100000) — the sizes of the list.
The second line contains nn integers aiai (1≤ai≤1061≤ai≤106), describing the list of the player A, who starts the game.
The third line contains nn integers bibi (1≤bi≤1061≤bi≤106), describing the list of the player B.
Output the difference between A‘s score and B‘s score (A−BA−B) if both of them are playing optimally.
2
1 4
5 1
0
3
100 100 100
100 100 100
0
2
2 1
5 6
-3
In the first example, the game could have gone as follows:
- A removes 55 from B‘s list.
- B removes 44 from A‘s list.
- A takes his 11.
- B takes his 11.
Hence, A‘s score is 11, B‘s score is 11 and difference is 00.
There is also another optimal way of playing:
- A removes 55 from B‘s list.
- B removes 44 from A‘s list.
- A removes 11 from B‘s list.
- B removes 11 from A‘s list.
The difference in the scores is still 00.
In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be 00.
大致题意:有两个人a,b,有两个数组,每个人可以从自己的数组选一个数当作自己的积分,这个数被除去,或者从别人的数组里除去一个数,双方都会进行这个操作,当双方数组里的没有数字的时候,游戏结束,尽可能使得自己的积分最多,最后输出两个人最终积分的差
分析:先给两个数组从小到大排个序,使用双指针,另i=n,j=n;
如果a[i]>b[j]时,
如果轮到a来选,那么a的积分ans1+=a[i],i--;
如果轮到b来选,那么b将a[i]从数组a里除去,i--;
如果a[i]<b[j]时,
如果轮到a来选,那么a将b[j]从数组b里除去,j--;
如果轮到b来选,那么b的积分ans2+=b[j],j--;
如果a[i]=b[j],
跳过i,j,i--,j--
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int main() 6 { 7 int n,a[1000005],b[1000005]; 8 while(~scanf("%d",&n)) 9 { 10 for(int i=1;i<=n;i++) 11 scanf("%d",&a[i]); 12 for(int i=1;i<=n;i++) 13 scanf("%d",&b[i]); 14 sort(a+1,a+1+n); 15 sort(b+1,b+1+n); 16 a[0]=0; 17 b[0]=0; 18 long long k=0,ans1=0,ans2=0; 19 for(int i=n,j=n;i>=0&&j>=0;) 20 { 21 if(a[i]>b[j]) 22 { 23 if(k==0)//使用k来标记这轮轮到谁,0是a,1是b 24 { 25 ans1+=a[i]; 26 k=1; 27 i--; 28 } 29 else 30 { 31 i--; 32 k=0; 33 } 34 } 35 else if(a[i]<b[j]) 36 { 37 if(k==1) 38 { 39 ans2+=b[j]; 40 k=0; 41 j--; 42 } 43 else 44 { 45 k=1; 46 j--; 47 } 48 } 49 else if(a[i]==b[j]) 50 { 51 i--; 52 j--; 53 } 54 } 55 printf("%lld\n",ans1-ans2); 56 } 57 return 0; 58 }
以上是关于2018SDIBT_国庆个人第二场的主要内容,如果未能解决你的问题,请参考以下文章