您的位置:首页 > 其它

usaco 1.1 greedy gift givers

2013-01-24 11:19 351 查看

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 any or all of the other friends. Likewise, each friend might or might not receive money from any or all 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 you might expect. Each person sets aside 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 3 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 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 more (or less) each person in the group gives than they receive.

IMPORTANT NOTE

The grader machine is a Linux machine that uses standard Unix conventions: 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, 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:
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 initial amount of money (in the range 0..2000) to be divided up into gifts by the giver and 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)

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

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

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

SAMPLE OUTPUT (file gift1.out)

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

好吧。。。这是一道很简单的模拟题,不过因为在usaco上没经验,提交的格式正确啊,导致交了几次才过。。
还是没想通为什么。好像以文件的形式输入输出,是不是c与c++就不能混编了啊。。还是有其他的方法呢?这个要考虑一下。。。
还有就是,题目里强调的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!
到底是嘛意思啊,是不是说每次读取都是从头再开始?好纠结~

题目中用到了STL中的map;虽然用STL会很慢。。不过我map用的不多,权当锻炼一下吧:
下面总结一下map的用法:

定义:map <int,string> a;        //其中int型为key,map提供的是基于key的快速检索功能;也就是说,map能够提供一对一的检索,为int型找到其对应的string型

不过这道题目应该反过来:)

赋值:此处说一下最简单的数组赋值: a[int]=string      //为int型提供一对一的关系,能够快速的从int型找到string型;

这道题中,应该是这样的:  a[string]=i

代码:





View Code

1 /*
 2 ID: jings_h1
 3 PROG: gift1
 4 LANG: C++
 5 */
 6 
 7 #include<iostream>
 8 #include<fstream>
 9 #include<string.h>
10 #include<map>
11 int res[100];
12 char name[100][20];
13 using namespace std;
14 int main(){
15     ofstream fout ("gift1.out");
16     ifstream fin ("gift1.in");
17     int n;
18     fin>>n;
19        memset(res,0,sizeof(res));
20        map <string,int> a;
21        for(int i=1;i<=n;i++){
22                string temp1;
23                fin>>name[i];
24                temp1=name[i];
25                a[temp1]=i;
26                }
27        for(int j=1;j<=n;j++){
28                string temp;
29                fin>>temp;
30                int sum,num;
31                fin>>sum>>num;
32                int k=0;
33                for(k=sum;k>0;k--){
34                        if(k%num==0)
35                            break;
36                            }
37                int point=a[temp];
38                res[point]+=(0-k);
39                for(int g=1;g<=num;g++){
40                        string temp2;
41                        fin>>temp2;
42                        int point2=a[temp2];
43                        res[point2]+=k/num;
44                        }
45                        }
46        for(int i=1;i<=n;i++){
47                fout<<name[i]<<" "<<res[i]<<endl;;
48           //     if(i<n)
49             //       fout<<endl;
50                }
51      //  fout<<"\n\r";
52        return 0;
53        }
54
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: