描述


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. mongodb 安装使用遇到的问题记录

    mongodb 常用命令: https://www.mongodb.org/downloads 官网64位下载链接 https://fastdl.mongodb.org/linux/mongodb-l ...

  2. NoSQL简单学习(一)

    只是简单的知道有这个东西,却从来没有去接触,今天看了几篇文章,记录一下,开始慢慢接触这一领域 简介: 8种Nosql数据库系统对比 http://blog.jobbole.com/1344/ 一网打尽 ...

  3. C++知识点 内存占用问题

    有一次去面试,谈的挺好的,被人问了一个问题,瞬间暴露自己基础能力弱的弱点了,这里自己记录下,以后慢慢长进. 问题 char test1[]={1,2,3,4}; char test2[]={1,2,3 ...

  4. Linux-Ps命令使用

    ps -ef | grep java ps aux | grep java ps aux 是用BSD的格式来显示Java进程 显示的项目有: USER        PID %CPU %MEM    ...

  5. 【java并发编程】十三章:显式锁:LOCK

    java5以后,新增了显式锁,用于当内置锁不能满足需求后可选择的一种高级方案. lock接口的特点 与内置锁一样,他能提供互斥性,内存可见性,可重入等特征,与内置锁不同的是,Lock提供了一种无条件, ...

  6. Django源码分析之权限系统_擒贼先擒王

    乍见 Django内置的权限系统已经很完善了,加上django-guardian提供的功能,基本上能满足大部分的权限需求.暂且不说django-guardian,我们先来看下Django内置的权限系统 ...

  7. restFul介绍及其使用规范

    什么是REST和RESTful API? REST:(英文:Representational State Transfer,简称REST)表征性状态转移,是一种软件架构风格. RESTful : RE ...

  8. video on web

    一.video容器      你可能经常看到.avi或.mp4的视频文件,实际上avi或者mp4只是一种视频容器.打个比方,ZIP的压缩文件可以包含各种各样的文件,同理,视频容器也定义用来怎么存放各种 ...

  9. oop &&GP 模板 ---> 特化和偏特化

    OOP面向对象编程 GP泛型编程(generic programming) 两者的主要区别就是OOP将数据和对数据的操作放在一起, GP就是将数据和操作独立开来 GP:   数据就是container ...

  10. EXEL文件转成简书MD表格

    EXEL文件转成简书MD表格 0.1.3 mac: https://github.com/fanfeilong/exceltk/blob/master/pub/exceltk.0.1.3.pkg wi ...