USACO Train 1.1.2 Greedy Gift Givers

这道题大意就是几个人互送礼物,让你求每个人的盈利。

原题给的样例数据:

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

这题使用模拟算法就行了,就是注意输入的时候不要掉坑里了。

还有就是比如说自己有200快钱给3个人,每个人得到66块,剩下两块自己收,并计入答案。

还有就是想题目里的vick,分给0个人,千万不要除以了0。。

下面是我的程序:

/*
ID:aaabbbr1
LANG:C++
TASK:gift1
*/
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;
struct person
{
	string name;
	int gave[10],have,cnt,receive;
	person(){have=receive=0;}
}a[10];
map<string,int>link;//这只是方便从人名找到编号而已
void gavemoney(int x)
{
	if(a[x].cnt==0)return;//小心除以0了(也就是如果给的人数为0就不给了)
	int per=a[x].have/a[x].cnt;//计算每人多少钱
	int tot=per*a[x].cnt;//计算一共给多少钱
	a[x].receive+=a[x].have-tot;//计算自己的零钱 并汇入收到的钱里面
	for(int i=0;i<a[x].cnt;i++)//给钱
		a[ a[x].gave[i] ].receive += per;
}
void display(int x)
{
	cout << a[x].name << " " << a[x].receive-a[x].have << "\n";
}
int main()
{
	freopen("gift1.in","r",stdin);
	freopen("gift1.out","w",stdout);
	string tmp;
	int n,x,t1,t2;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		cin >> tmp;
		a[i].name=tmp;
		link[tmp]=i;
	}
	for(int i=0;i<n;i++)
	{
		cin >> tmp;
		x=link[tmp];
		cin >> t1 >> t2;
		a[x].have=t1;
		a[x].cnt=t2;
		for(int j=0;j<a[x].cnt;j++)
		{
			cin >> tmp;
			a[x].gave[j]=link[tmp];
		}
	}
	for(int i=0;i<n;i++)
		gavemoney(i);
	for(int i=0;i<n;i++)
		display(i);
	fclose(stdin);
	fclose(stdout);
	return 0;
}

======================

原题中文翻译(Collected by BirDOR):

★Greedy Gift Givers 贪婪的礼物送礼者
对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for
those who view gift giving with cynicism).
在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物
的人.
然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较
多的钱.
给出一群朋友, 没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物
的人的列表,
请确定每个人收到的比送出的钱多的数目.
IMPORTANT NOTE
测试系统是 Linux 符合标准的 Unix 的协定.
用‘\n‘作为行的结束.
这和 Windows 系统用‘\n‘ 和 ‘\r‘作为行的结束是不同的.
你的程序不要被这困住了.
PROGRAM NAME: gift1
INPUT FORMAT
第 1 行:人数NP,2<= NP<=10
第 2 到 NP+1 行:这NP 个在组里人的名字 一个名字一行
第NP+2 到最后:这里的NP 段内容是这样组织的:
第一行是将会送出礼物人的名字.
第二行包含二个数字: 第一个是原有的钱的数目(在0 到2000 的范围里),第二个NGi 是将收到
这个送礼者礼物的人的个数 如果 NGi 是非零的, 在下面 NGi 行列出礼物的接受者的名字,一个
名字一行.
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
输出 NP 行
每行是一个的名字加上空格再加上收到的比送出的钱多的数目.
对于每一个人,他名字的打印顺序应和他在输入的2 到NP+1 行中输入的顺序相同.所有的送礼的钱
都是整数.
每个人把相同数目的钱给每位要送礼的朋友,而且尽可能多给,不能给出的钱被送礼者自己保留.
SAMPLE OUTPUT (file gift1.out)
dave 302
laura 66
owen -359
vick 141
amr -150

时间: 2024-08-05 11:55:06

USACO Train 1.1.2 Greedy Gift Givers的相关文章

USACO 1.1 Greedy Gift Givers

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 receiv

贪婪的送礼者Greedy Gift Givers

洛谷——P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers codevs——1381 贪婪的礼物送礼者  USACO 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱.给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和

【USACO1.1.2】Greedy Gift Givers(map)

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 receiv

1.1.4 PROB Greedy Gift Givers

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 receiv

USACO Greedy Gift Givers 解题心得

本题算法不难想出,但是中间还是出现了一些问题. 开始的时候是#11:Execution error,后来把普通的数组改成动态数组后问题消失. 后来又出现了Execution error: Your program had this runtime error: Illegal file open (/dev/tty). 随后google解决方案,多数都是数组开小了.遂开大数组,无效. 突然意识到很有可能是低级错误,于是检查代码. 发现写了个 for(int i = 0; i < n2 ; i++

usaco Greedy Gift Givers

/* ID: modengd1 PROG: gift1 LANG: C++ */ #include <iostream> #include<string.h> #include<stdio.h> using namespace std; char names[30][20]; int acount[30]; int counter,N; int getIndex(char name[20]) { for(int i=0;i<counter;i++) { if(st

洛谷P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱.给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,请确定每个人收到的比送出的钱多的数目. SilverN补充:如果钱不能按整数均分,那么余数部分会被收回,不会送出去. 输入

1.1.2 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

P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱.给出一群朋友,没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,请确定每个人收到的比送出的钱多的数目. 输入输出格式 输入格式: 第 1 行: 人数NP,2<= NP<=10 第 2