LA 4945 free goodies (greedy)

Source: Internet
Author: User

Description

B free GoodiesPetra and Jan has just received a box full of free goodies, and want to divide the goodies between them. However, it is the not-easy-to-do fairly, since they both value different goodies differently. To divide the goodies, they has decided upon the following Procedure:they choose goodies One by one, turn, until all The Goodies is chosen. A coin is tossed to decide who gets to choose the first goodie. Petra and Jan has different strategies in deciding-choose. When faced with a choice, Petra always selects the goodie that's most valuable to her. In case of a tie, she's very considerate and picks the one that's least valuable to Jan. (Since Petra and Jan was good F Riends, they know exactly how much value the other places on each goodie.) Jan ' s strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices leads to the same optimal result, he prefers Petra to has as much FINA L value as possible. You are given the ResulT of the initial coin toss. After Jan and Petra has finished dividing all the goodies between themselves, what's the total value of the goodies each Of them ends up with?InputOn the first line a positive integer:the number of test cases for most 100. After, per test case:
    • One line with an integer n (1≤n≤1): The number of goodies.
    • One line with a string, either 'Petra' or 'Jan': The person that chooses first.
    • n lines with integers pi and ji (0≤pi,ji≤1) each:the values of Petra and Jan assign to the i-th goodie, Res Pectively.
OutputPer test Case:
    • One line with integers:the value Petra gets and the value of Jan. Both values must is according to their own valuations.
Sample In-and Output
Input Output
34petra100 8070 8050 8030 504petra10 one 106 47jan4 13 12 11 11 21 31 4
170 13014 169 10

Test instructions: N Candy, two people petra,jan, each candy for these two people have a certain Val value, Petra each election for their Val value of the largest, if the same, choose the Val value of Jan Small, Jan want to let themselves choose the sum of the Val value of Candy Max, if there are many paths, So choose the one that makes Petra the last Val the biggest (really good friends, good friends.) =), tell you the tempo, turn around. Ask you the sum of the Val value of the last of their two differences.


Idea: Greed. First in accordance with the order of Petra two people, you will get two groups, a group is Petra, a group is the Jan, and then inverted, for each of the sweets selected by Jan, for the current point and then go down to find, and the original Petra Candy greedy, and then exchange, because the Jan can be arbitrarily taken, He can always get these candies.



#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 (greedy). cpp*/


LA 4945 free goodies (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.