描述


http://train.usaco.org/usacoprob2?a=y0SKxY0Kc2q&S=gift1

给出不超过$10$个人,每个人拿出一定数量的钱平分给特定的人,求最后每个人的财产变化.

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.

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 characters, '\n' and '\r'. Do not let your program get trapped by this!

PROGRAM NAME: gift1

INPUT FORMAT

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 (1 ≤ 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.

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 starting 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

分析


第二种写法貌似简短了些..

英文捉鸡QAQ

 /*
TASK:gift1
LANG:C++
Time:2018.5.11-21:24
*/
#include <bits/stdc++.h>
using namespace std; const int maxn=,maxm=;
int n;
struct A{
char name[maxm+];
int m=;
}a[maxn+]; int main(){
freopen("gift1.in","r",stdin);
freopen("gift1.out","w",stdout);
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%s",a[i].name);
for(int i=;i<n;i++){
char _x[maxm+];
int x,num,mon;
scanf("%s%d%d",_x,&mon,&num);
for(int j=;j<n;j++) if(!strcmp(_x,a[j].name)){
x=j;
break;
}
for(int j=;j<num;j++){
char _y[maxm+];
int y;
scanf("%s",_y);
for(int k=;k<n;k++) if(!strcmp(_y,a[k].name)){
y=k;
break;
}
int m=mon/num;
a[y].m+=m;
a[x].m-=m;
}
}
for(int i=;i<n;i++) printf("%s %d\n",a[i].name,a[i].m);
return ;
}

NO.1

 /*
TASK:gift1
LANG:C++
Time:2018.5.11-21:35
*/
#include <bits/stdc++.h>
using namespace std; const int maxn=,maxm=;
int n;
struct B{
char name[maxm+];
int m=;
};
struct A{
B b[maxn+];
B& operator [] (const char* name){
for(int i=;i<n;i++) if(!strcmp(name,b[i].name)) return b[i];
}
B& operator [] (const int& i){
return b[i];
}
}a; int main(){
freopen("gift1.in","r",stdin);
freopen("gift1.out","w",stdout);
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%s",a[i].name);
for(int i=;i<n;i++){
char _x[maxm+];
int num,mon;
scanf("%s%d%d",_x,&mon,&num);
B& x=a[_x];
for(int j=;j<num;j++){
char y[maxm+];
scanf("%s",y);
int m=mon/num;
a[y].m+=m;
x.m-=m;
}
}
for(int i=;i<n;i++) printf("%s %d\n",a[i].name,a[i].m);
return ;
}

NO.2

USACO_1.1_Greedy_Gift_Givers_(模拟+水题)的更多相关文章

  1. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. POJ 2014:Flow Layout 模拟水题

    Flow Layout Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3091   Accepted: 2148 Descr ...

  3. 模拟水题,查看二维数组是否有一列都为1(POJ2864)

    题目链接:http://poj.org/problem?id=2864 题意:参照题目 哈哈哈,这个题discuss有翻译哦.水到我不想交了. #include <cstdio> #inc ...

  4. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  5. Codeforces 1082B Vova and Trophies 模拟,水题,坑 B

    Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...

  6. HDU4287-STL模拟水题

    一场2012天津网络预选赛的题,签到题. 但是还是写了三四十分钟,C++和STL太不熟悉了,总是编译错误不知道怎么解决. 一开始用的Char [] 后来改成了string,STL和string搭配起来 ...

  7. hdu 4891 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...

  8. Mishka and Contest(模拟水题)

    Mishka started participating in a programming contest. There are nn problems in the contest. Mishka' ...

  9. 模拟水题,牛吃草(POJ2459)

    题目链接:http://poj.org/problem?id=2459 题目大意:有C头牛,下面有C行,每头牛放进草地的时间,每天吃一个草,总共有F1个草,想要在第D的时候,草地只剩下F2个草. 解题 ...

随机推荐

  1. SQL On Streaming

    此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 实时计算的一个方向 实时计算未来会成为一个趋势,基本上所有的离线计算任务都能通过实时计算来完成,对于实时计算来 ...

  2. vs调试代码的时候断点无法命中

    https://blog.csdn.net/xxdddail/article/details/18696399 该链接提供的解决方案主要是如下图片:禁用 图片标记的这个选项即可:

  3. spring location设置本地路径

    <context:property-placeholder location="file:D:/jdbc.properties"/> 直接在路径前加上 file:

  4. Performanced C++ 经验规则

    http://www.cnblogs.com/ccdev/archive/2012/12/27/2836448.html Performanced C++,意为“高性能C++“编程,是笔者和所在团队多 ...

  5. java-2018-01-17计划

    1.一句英语 包括单词 2.一个java版本的设计模式 参考:https://github.com/iluwatar/java-design-patterns 学习了抽象工厂模式 总结:java的RS ...

  6. 使用emit发出信号

    1. 信号声明 在发送信号的模块类头文件中声明信号函数 signals: void sendRate(QString rate); 2. 在发送模块的成员函数中发出信号 emit sendRate(u ...

  7. HDFS集群和YARN集群

    Hadoop集群环境搭建(一)   1集群简介 HADOOP集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起 HDFS集群: 负责海量数据的存储,集群中的角色主要 ...

  8. 【bzoj1922】[Sdoi2010]大陆争霸 堆优化Dijkstra

    题目描述 一张n个点m条边的图,通过每条边需要一定的时间.有一些限制条件,每个限制条件形如“x保护y”,表示到达y的最短时间不能小于到达x的最短时间(即如果在其之前到达,则需要等待至xd到达).问1到 ...

  9. combobox下拉框

    ----------------------------------------------combobox下拉框----------------------------------------- f ...

  10. BZOJ5110 CodePlus2017Yazid 的新生舞会(线段树)

    考虑统计每个数字的贡献.设f[i]为前缀i中该数的出现次数,则要统计f[r]-f[l]>(r-l)/2的数对个数,也即2f[r]-r>2f[l]-l. 注意到所有数的f的总变化次数是线性的 ...