描述


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命令

    本文为转载,原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4530333.html 1.说明:创建数据库CREATE DATABASE databas ...

  2. 深入Python的类和对象

    多态:不同的子类对象,可以调用相同的父类方法,通过改写父类的方法,产生不同的执行结果 instance和type的区别: instance能够顺延到父类,比对对象与父类是否类型一致.而type只能比对 ...

  3. cocos2d-x 场景切换

    场景切换的方法 场景切换是通过导演类director实现的,其中的相关方法如下: director.run(new_scene).该方法可以运行场景,只能在启动第一个场景时调用该方法.如果已运行场景, ...

  4. flask中static_folder与static_url_path的区别与联系

    # -*- coding:utf-8 -*- from flask import Flask, url_for app1 = Flask(__name__, static_folder='mystat ...

  5. Java 集合学习--ArrayList

    一.ArrayList 定义 ArrayList 是一个用数组实现的集合,支持随机访问,元素有序且可以重复. ①.实现 List 接口 List接口继承Collection接口,是List类的顶层接口 ...

  6. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  7. [译]10个有关SCP的命令

    原文来源: https://www.tecmint.com/scp-commands-examples/ 基本语法 scp source_file_name username@destination_ ...

  8. Python 随笔01---列表

    列表 1.取出列表中的元素方法?? 2.删除列表中的元素del.remove对比?? 3.linux 常用操作命令

  9. B树(B-树)

    1.基本概念: M定义为树的高度,也叫阶,就是树的深度: (1).B树又称为多路平衡查找树. (2).根节点至少有两个子节点. (3).除根节点以外的非叶子节点的儿子树为[M/2,M]. (4).每个 ...

  10. 【python】Python3中出现'gbk' codec can't encode characte的成功解决方法?

    亲身测试,所遇问题完全解决!2018/07/08 21:37 环境:windows,Pycharm,python3.6.2 使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情 ...