Greedy Gift Givers Greedy Gift Givers

Description

For a group of friends who want to exchange gifts, TRW determines how much more each person is giving than they are receiving. In this one problem, everyone has some money to give a gift, and this money will be divided equally among those who will receive his gift. However, in any group of friends, some will give more gifts (perhaps because of having more friends) and some will have more money prepared. Given a group of friends, no one's name will be longer than 14 characters, give the money each person will spend on gift giving, and a list of people who will receive his gift, make sure each person receives more money than gave Number of.

Input

Line 1: Number of people NP, 2<= NP<=10 Line 2 to J+1: The name of the NP person in the group A name Line J+2 to the end: The content of paragraph I here is organized as follows: One line is the name of the person who will give the gift. The second line contains two numbers: the first is the original amount of money (in the range 0 to 2000), and the second NGi is the number of people who will receive this person's gift. If NGi is non-zero, List the name of the recipient of the gift on the NGi line below, one name per line.

Output

Output NP lines. Each line is a name plus a space plus the amount received than sent. For each person, his name should be printed in the same order he entered it on lines 2 to NP+1 of the input. All gift money is in whole numbers. Everyone gives the same amount of money to each friend who wants to give a gift, and give as much as possible, and the money that cannot be given is included in the income of the gift giver.

Sample 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

Sample Output

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



 1 #include<stdio.h>
 2 #include<string.h>
 3 struct message
 4 {
 5     char name[20];
 6     int in;
 7     int out;
 8 };
 9 int main()
10 {
11     struct message a[20];
12     int n,i,j,k,m,num,t;
13     char s[20]= {0};
14     char x[20]= {0};
15     scanf("%d",&n);
16     for(i=0; i<n; i++)
17     {
18         scanf("%s",a[i].name);
19         a[i].in=0;
20         a[i].out=0;
21     }
22     for(i=0; i<n; i++)
23     {
24         scanf("%s",s);
25         scanf("%d%d",&m,&num);
26         for(j=0; j<n; j++)
27         {
28             if(strcmp(s,a[j].name)==0)
29             {
30 
31                 break;
32             }
33         }
34         if(m>0)
35         {
36             a[j].out=a[j].out+m;
37             a[j].in=a[j].in+m%num;
38         }
39         for(k=0; k<num; k++)
40         {
41             scanf("%s",x);
42             for(j=0; j<n; j++)
43             {
44                 if(strcmp(x,a[j].name)==0)
45                 {
46 
47                     break;
48                 }
49             }
50             if(m>0)
51             {
52                 a[j].in=a[j].in+(m-m%num)/num;
53             }
54         }
55     }
56     for(i=0; i<n; i++)
57     {
58         printf("%s %d\n",a[i].name,a[i].in-a[i].out);
59     }
60     return 0;
61 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304573&siteId=291194637