描述


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. android中的AIDL学习笔记

    一.定义 AIDL是用来解决进程间通信的(一般有四种方式:Activity.Service.ContentProvider.Broadcast Receiver),两个进程间无法直接通信,所以要用AI ...

  2. 关于Vue脚手架写法的问题

    问题描述: main.js import Vue from 'vue' import App from './App' /* eslint-disable no-new */ new Vue({ el ...

  3. Lua工具类

    1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...

  4. TP5 急速上手 语法规则

    Tp5  规则 命名规范 目录和文件名采用‘小写+下划线’,并且以小写字母开头: 类库.函数文件统一以.php为后缀: 类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致(包括大小写 ...

  5. 使用CodeBlocks为你的程序添加程序文件图标和启动读入图标

    其实也非常简单,自己这两天用win32api做了一个小程序,可是发现图标却是dos的,太难看了,于是就想起以前学win32汇编时候用到的工具,ResEd,已经被我汉化了一些,估计有新的版本发布吧,但是 ...

  6. Hessian 2.0 序列化协议 - Hessian 2.0 Serialization Protocol 翻译

    Hessian是一种轻量.快速的web协议,在微服务场景下经常被使用. Hessian协议实际上包含两种含义: 1. Web网络通信远程调用服务,具体可以参考:http://hessian.cauch ...

  7. 第18讲——string类

    关键字:string类  字符串  C-风格字符串  C库字符串函数 字符串:存储在内存的连续字节中的一系列字符. C++处理字符串的方式有两种: 来自C语言,常被称为C-风格字符串: 基于strin ...

  8. rcnn spp_net

    在http://www.cnblogs.com/jianyingzhou/p/4086578.html中 提到 rcnn开创性工作,但是计算时间太长,重复计算太大. spp_net将重复计算避免了 我 ...

  9. maven环境变的配置(复制自己看)

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具.由于 Maven 的缺省构建 ...

  10. 编程练习:寻找发帖"水王"

    题目: 寻找发帖"水王" 来源: 编程之美 分析 衍生:就是给定一个数组,其中某个元素出现次数超过了数组长度的一半,找出这个元素 方法s 方法1 对这个串进行遍历,同时对出现的元素 ...