Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 400
points
Problem Statement
There are N
dishes of cuisine placed in front of Takahashi and Aoki. For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N
.
When Takahashi eats Dish i
, he earns Ai points of happiness; when Aoki eats Dish i, she earns Bi
points of happiness.
Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat. Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".
Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".
Constraints
- 1≤N≤105
- 1≤Ai≤109
- 1≤Bi≤109
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A1 B1
:
AN BN
Output
Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".
Sample Input 1
3
10 10
20 20
30 30
Sample Output 1
20
In this sample, both of them earns 10
points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3
In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3
, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30+10)−20=20
.
Sample Input 2
3
20 10
20 20
20 30
Sample Output 2
20
In this sample, Takahashi earns 20
points of happiness by eating any one of the dishes 1,2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3
.
In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3
, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20+20)−20=20
.
Sample Input 3
Copy
6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
Sample Output 3
Copy
-2999999997
Note that the answer may not fit into a 32-bit integer.
题意:
给你N个有食物盘子,如果A吃可以获得Ai幸福值,如果B吃获得Bi个幸福值,A先开始挑,(每个盘子只可以给一个人用),每次挑选的时按照,以便最大化以下价值:“MAX(Sum_A - Sum_B);
即,他/她最终将获得的开心值总和减去他人最终将获得的幸福值总和
分析: 很显然是个贪心的题,首先范围可能爆int,注意开longlong,
我们想得到价值最大,所以要把其价值最大(an+bn)的盘子从大到小排序,
假设有很多盘子,A第一个吃,第一个选肯定要吃掉最大价值的第一盘子的食物,
当B开始选盘子(考虑为A开始损失值),应选第二价值大的盘子,这样才可以保证价值每次都是增长的,如果B换做其他的选取方式进行选取,很难保证价值每次增长,体现贪心的策略。
代码:
#include<cstdio>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
struct node{
ll a;
ll b;
};
bool cmp(node x,node y){
return x.a+x.b >y.a+y.b ;
}
int main()
{
ll n;
scanf("%lld",&n);
vector<node> nu(n);
for(int i=0;i<n;i++)
scanf("%lld%lld",&nu[i].a,&nu[i].b);
sort(nu.begin(),nu.end(),cmp);
ll an1=nu[0].a;
ll an2=0;
for(int i=1;i<n;i++)
{
if(i%2!=0)
an2+=nu[i].b;
else
an1+=nu[i].a;
}
printf("%lld\n",an1-an2);
return 0;
}