출처: https://train.usaco.org/usacogate


문제 설명

Task 'gift1': Greedy Gift Givers

A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to some or all of the other friends (although some might be cheap and give to no one). Likewise, each friend might or might not receive money from any or all of the other friends. Your goal is to deduce how much more money each person receives than they give.

The rules for gift-giving are potentially different than you might expect. Each person goes to the bank (or any other source of money) to get a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 7 among 2 friends would be 3 each for the friends with 1 left over – that 1 left over goes into the giver's "account". All the participants' gift accounts start at 0 and are decreased by money given and increased by money received.

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

Given:
a group of friends, no one of whom has a name longer than 14 characters,
the money each person in the group spends on gifts, and
a (sub)list of friends to whom each person gives gifts,
determine how much money each person ends up with.

입력 양식

Line # Contents
1 A single integer, NP
2..NP+1 Line i+1 contains the name of group member i
NP+2..end NP groups of lines organized like this:
The first line of each group tells the person's name who will be giving gifts.
The second line in the group contains two numbers:
The amount of money (in the range 0..2000) to be divided into gifts by the giver
NGi (0 ≤ NGi ≤ NP), the number of people to whom the giver will give gifts
If NGi is nonzero, each of the next NGi lines lists the name of a recipient of a gift; recipients are not repeated in a single giver's list.

출력 양식

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 that person. The names should be printed in the same order they appear starting on line 2 of the input.

입력 예

5 
dave 
laura 
owen 
vick 
amr 
dave 
200 3 
laura 
owen 
vick 
owen 
500 1 
dave 
amr 
150 2 
vick 
owen 
laura 
0 2 
amr 
vick 
vick 
0 0

출력 예

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


문제 풀이 내용

각 인원마다 선물을 주고 받은 금액을 더하고 빼줘야 한다. 다만, 나머지 부분을 주는 사람이 가지게 하면 된다.

간단하게 이름/금액을 가지는 클래스를 만들고, 이름으로 검색해서 금액을 조정해주도록 했다.

프로그램 내용

더보기
class GiftMap
{
public:
    string Names;
    int gifts;
};

class GiftMap Party[MAXPEOPLE];

/* look for name in people table */
class GiftMap* lookup(string inName)
{
    for(int i=0; i<MAXPEOPLE; ++i)
	if(inName == Party[i].Names)
	    return &Party[i];
}
...
    for(int index=0;index < size ; ++index)
    {
        string gName;
        fin >> gName;
        int amount, targets;
        fin >> amount >> targets ;

		// Reduce Giver's amount
        class GiftMap *giver;
        giver = lookup(gName);

        if(targets)
        {
            giver->gifts -= amount;
            giver->gifts += amount%targets;
        }

 

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.6 Friday the Thirteenth  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07
USACO Training - Chapter 1. Getting started  (0) 2019.09.06

+ Recent posts