"USACO1.1.2" greedy Gift givers (map)

Source: Internet
Author: User

Greedy Gift Givers

A Group of NP (2≤NP≤10) uniquely named friends have decided to exchange gifts of money. Each of the these friends might or might is give some money to any or all of the other friends. Likewise, each friend might or might is not a receive money from any or all of the other friends. Your goal in this problem are to deduce how much + money each person gives than they receive.

The rules for gift-giving is potentially different than you might expect. Each person sets aside a certain amount the money to give and divides this money evenly among all those to whom he or she I s giving a gift. No fractional money are available, so dividing 3 among 2 friends would being 1 each for the friends with 1 left over – that 1 Left over stays in the giver's "account".

In any group of friends, some people is more giving than others (or at least may has more acquaintances) and some people There are more money than others.

Given A group of friends, no one of the whom have a name longer than characters, the money each person in the group spends O n Gifts, and a (sub) list of friends to whom each person gives gifts, determine how much + (or less) the Group gives than they receive.

IMPORTANT NOTE

The grader machine was a Linux machine, that uses, Unix conventions:end of Line was a single character often known a s ' \ n '. This differs from Windows, which ends lines with the charcters, ' \ n ' and ' \ R '. Your program get trapped by this!

Program Name:gift1input FORMAT
line 1: The single integer, NP
Lines 2..np+1: Each line contains the name of a group member
lines np+2..end: NP groups of Lines organized like this: Td>the first line in the group tells the person ' s name is giving gifts.
The second line in the group contains, numbers:the initial amount of money (in the range 0 .. () to is divided up into gifts by the giver and then the number of people to whom the giver would give gifts, ngi  ( 0≤ngi ≤np-1).
If ngi is Nonzero, each of the next Ngi lines lists the the name of a recipient of a Gift.

SAMPLE INPUT (file gift1.in)
5davelauraowenvickamrdave200 3lauraowenvickowen500 1daveamr150 2vickowenlaura0 2amrvickvick0 0
OUTPUT FORMAT

The output is NP-lines, each with the name of a person followed by a single blank followed by the net gain or loss (Final_ Money_value-initial_money_value) for the person. The names should is printed in the same order they appear on line 2 of the input.

All gifts is integers. Each person gives the same integer amount of money to all friend to whom all money is given, and gives as much as Possibl E that meets this constraint. Given is kept by the giver.

SAMPLE OUTPUT (file gift1.out)
Dave 302laura 66owen-359vick 141amr-150


This problem in codeforces above to do the same idea is probably the application of map

Is that there's a lot of people giving presents to each other. The money for each gift is given to the other person at the end of the price of the money sent out by each person and the gift received to find out the final harvest if not the average to other people to take the value of the model left to themselves

Actually is map<string, int> string saves the person name int to save the gift and the harvest the present price Finally the result is good the feeling will use the map this question can quickly make does not use the words on the structure body also will not use for a long time

This problem has the following two points in the deepest harvest

1. Usaco is really a source of many topics for reference

2. STL is really a good thing.

/*id:alpha augurprog:gift1lang:c++*/#include <iostream> #include <string> #include <cstdio># Include <cstring> #include <map>using namespace std;string str[40];int main () {    freopen ("gift1.in", "R" , stdin);    Freopen ("Gift1.out", "w", stdout);    int n;    Map<string, int> na;    CIN >> N;    for (int i = 0; i < n; ++i) {        cin >> str[i];    }    for (int i = 0; i < n; ++i) {        string s;        int num, m;        Cin >> S;        CIN >> num >> m;        NA[S]-= num;        if (M! = 0) {            na[s] + = num% m;        }        for (int j = 0; j < m; ++j) {            cin >> s;            Na[s] + = num/m;        }    }    for (int i = 0; i < n; ++i) {        cout << str[i] << "" << Na[str[i]] << Endl;    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"USACO1.1.2" greedy Gift givers (map)

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.