USACO 1.1 - Greedy Gift Givers(杂题)

来源:互联网 发布:耽美网络剧百度云 编辑:程序博客网 时间:2024/06/06 06:37

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

The rules for gift-giving are potentially different than youmight expect. Each person sets aside a certain amount of money togive and divides this money evenly among all those to whom he orshe is giving a gift. No fractional money is available, so dividing3 among 2 friends would be 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 are more giving than others(or at least may have more acquaintances) and some people have moremoney than others.

Given a group of friends, no one of whom has a name longer than14 characters, the money each person in the group spends on gifts,and a (sub)list of friends to whom each person gives gifts, determinehow much more (or less) each person in the group gives than theyreceive.

IMPORTANT NOTE

The grader machine is a Linux machine that uses standard Unixconventions: end of line is a single character often known as '\n'.This differs from Windows, which ends lines with two charcters,'\n' and '\r'. Do not let your program get trapped by this!

PROGRAM NAME: gift1

INPUT FORMAT

Line 1:The single integer, NPLines 2..NP+1:Each line contains the name of a group memberLines NP+2..end:NP groups of lines organized like this:The first line in the group tells the person's name who will be giving gifts.The second line in the group contains two numbers: The initialamount of money (in the range 0..2000) to be divided up into gifts by the giverand then the number of people to whom the giver will 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 bya single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed inthe same order they appear on line 2 of the input.

All gifts are integers. Each person gives the same integer amountof money to each friend to whom any money is given, and gives as muchas possible that meets this constraint. Any money not given is kept bythe giver.

SAMPLE OUTPUT (file gift1.out)

dave 302laura 66owen -359vick 141amr -150

                                                

CODE:

/*ID:sotifis3LANG:C++TASK:gift1*/#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;map<string, int> ma;char s[15][20];int main(){    //freopen("in", "r", stdin);    freopen("gift1.in","r",stdin);    freopen("gift1.out","w",stdout);    int n;    scanf("%d%*c", &n);    for(int i = 0; i < n; ++i){        scanf("%s", s[i]);    }    int p, m;    ma.clear();    char s1[20], s2[20];    for(int i = 0; i < n; ++i){        scanf("%s%*c", s1);        scanf("%d %d", &p, &m);        if(m == 0) continue;        int pp = p / m;        ma[s1] -= pp * m;        for(int j = 0; j < m; ++j){            scanf("%s%*c", s2);            ma[s2] += pp;        }    }    for(int i = 0; i < n; ++i){        printf("%s %d\n", s[i], ma[s[i]]);    }    return 0;}


0 0
原创粉丝点击