Description
| Input | Output |
3
4
Petra
100 80
70 80
50 80
30 50
4
Petra
10 1
1 10
6 6
4 4
7
Jan
4 1
3 1
2 1
1 1
1 2
1 3
1 4
|
170 130
14 16
9 10
|
题意:n个糖果,两个人 Petra,Jan,每个糖果对于这两个人都有一定的val值,Petra每次选对于自己val值最大的那个,如果一样,就选Jan的val值小的,Jan想让自己选的糖果的val值之和最大,如果有多种路径,那么就选使Petra最后的val最大的那种(真是好基友,好丽友。。 = =),告诉你先手,轮着来。问你最后他们两个的分别得val值之和。
思路:贪心。先按照Petra的顺序两人逐个选取,就会得到两组,一组是Petra的,一组是Jan的,然后再倒着来,对于每个Jan选取的糖果,对于当前点再往下找,去和本来Petra的糖果进行贪心,再交换,因为Jan可以随意取,他总可以拿到这些糖果。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int MAXN = 1010;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
struct node
{
int a,b;
} p[MAXN];
int vis[MAXN];
int n;
char name[10];
bool cmp(node x,node y)
{
if(x.a==y.a)
{
return x.b<y.b;
}
return x.a>y.a;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int Case, x, y;
cin>>Case;
while(Case--)
{
cin>>n>>name;
for(int i = 0; i < n; i++)
{
scanf("%d %d", &p[i].a, &p[i].b);
}
sort(p, p+n ,cmp);
for(int i = 0; i < n; i++)
{
if(name[0] == 'J')
{
vis[i] = (i%2==0)?1:0;
}
else
{
vis[i] = (i%2==0)?0:1;
}
}
for(int i = n-1; i >= 0; i--)
{
int temp;
if(vis[i])
{
temp = i;
vis[i] = 0;
for(int j = i+1; j < n; j++)
{
if(!vis[j] && p[j].b>=p[temp].b)
{
temp = j;
}
}
vis[temp] = 1;
}
}
x = y = 0;
for(int i = 0; i < n; i++)
{
//printf("%d ", vis[i]);
if(vis[i])
{
y += p[i].b;
}
else
{
x += p[i].a;
}
}
printf("%d %d\n", x, y);
}
return 0;
}
/*LA 4945 Free Goodies(贪心).cpp*/
原文:http://blog.csdn.net/u014028317/article/details/45654041