Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
B Free Goodies
Input
On the first line a positive integer: the number of test cases, at most 100. After that per test case:- One line with an integer n (1 ≤ n ≤ 1 000): the number of goodies.
- One line with a string, either "Petra" or "Jan": the person that chooses first.
- n lines with two integers pi and ji (0 ≤ pi,ji ≤ 1 000) each: the values that Petra and Jan assign to the i-th goodie, respectively.
Output
Per test case:- One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations.
Sample in- and output
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 |
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <math.h> 6 using namespace std; 7 typedef struct point 8 { 9 int p,j,i; 10 } ponit; 11 ponit a[1005]; 12 int b[1005]; 13 bool cmp(ponit x,ponit y) 14 { 15 if(x.p==y.p)return x.j<y.j; 16 return x.p>y.p; 17 } 18 bool cnp(ponit x,ponit y) 19 { 20 if(x.j==y.j)return x.p<y.p; 21 return x.j>y.j; 22 } 23 bool check() 24 { 25 int i,sum=0;; 26 for(i=1;i<1005;i++) 27 { 28 sum+=b[i]; 29 if(b[i]) 30 { 31 if(sum>(i+1)/2)return 0; 32 } 33 } 34 return 1; 35 } 36 int main() 37 { 38 int t,n,p,j,i,sum[2],st; 39 char z[20]; 40 scanf("%d",&t); 41 while(t--) 42 { 43 memset(b,0,sizeof(b)); 44 scanf("%d",&n); 45 scanf("%s",z); 46 for(i=0; i<n; i++) 47 scanf("%d%d",&a[i].p,&a[i].j); 48 sort(a,a+n,cmp); 49 sum[0]=sum[1]=0; 50 if(strcmp(z,"Petra")==0)sum[0]+=a[0].p,st=1; 51 else st=0; 52 for(i=0; st<n; i++,st++) 53 { 54 a[i].p=a[st].p; 55 a[i].j=a[st].j; 56 a[i].i=i+1; 57 } 58 n=i; 59 sort(a,a+n,cnp); 60 for(i=0;i<n;i++) 61 { 62 b[a[i].i]=1; 63 if(check())sum[1]+=a[i].j; 64 else b[a[i].i]=0,sum[0]+=a[i].p; 65 } 66 printf("%d %d\n",sum[0],sum[1]); 67 } 68 }