码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 1038C. Gambling(思维,模拟)

时间:2018-09-08 11:53:45      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:action   removes   etc   moved   ios   limit   sub   data   std   

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of input contains an integer nn (1n1000001≤n≤100000) — the sizes of the list.

The second line contains nn integers aiai (1ai1061≤ai≤106), describing the list of the player A, who starts the game.

The third line contains nn integers bibi (1bi1061≤bi≤106), describing the list of the player B.

Output

Output the difference between A‘s score and B‘s score (A?BA?B) if both of them are playing optimally.

Examples
input
Copy
2
1 4
5 1
output
Copy
0
input
Copy
3
100 100 100
100 100 100
output
Copy
0
input
Copy
2
2 1
5 6
output
Copy
-3
Note

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两个竞赛者各自拥有一个序列,他们可以在每一个回合中进行两种操作,第一种操作是在对方的序列中删除一个元素,第二种操作是把自己序列中的一个元素加入自己的得分中,A,B都想要自己的得分较高,输出A-B的值。

思路:将两个序列排序,进行操作模拟。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int a[100010];
int b[100010];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    for(int i=0; i<n; i++)
        scanf("%d",&b[i]);
    sort(a,a+n);
    sort(b,b+n);
    long long sum=0;
    int t=0;
    int x=n-1;
    int y=n-1;
    while(x>=0||y>=0)
    {
        if(t%2==0)
        {
            if(a[x]>b[y]) sum+=a[x],x--;
            else y--;
        }
        else
        {
            if(a[x]>=b[y]) x--;
            else sum-=b[y],y--;
        }
        t++;
    }
    printf("%I64d\n",sum);
    return 0;
}

 

codeforces 1038C. Gambling(思维,模拟)

标签:action   removes   etc   moved   ios   limit   sub   data   std   

原文地址:https://www.cnblogs.com/wangxwws/p/9608063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!