USACO_CHA1_Greedy Gift Givers

一、题目描述:

Question Link:
https://train.usaco.org/usacoprob2?a=u6Y9yo27xLS&S=gift1
Source: USACO TRAINING

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 mon1.ey each person ends up with.

二、中文题干

链接https://ac.nowcoder.com/acm/contest/395/B
来源:牛客网

对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for those who view gift giving with cynicism)。
在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。
然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。
给出一群朋友, 没有人的名字会长于14字符,给出每个人将花在送礼上的钱,和将收到他的礼物的人的列表,
请确定每个人收到的比送出的钱多的数目。

三、题解

本题较为简单,只是正常的模拟。
我的AC代码,用了两个map,第一个map是名字对应人的编号(及其数组下标),第二个map是人的编号对应人的名字。
这样做的好处是不用每到一个人就遍历找他。
当然这个策略有点蠢笨,相信大家不难找到更好更简单的方法。

四、AC代码

/*
ID: ph S
TASK: gift1
LANG: C++
*/
#include <iostream>
#include <map>
using namespace std;
struct people
{
    int init;
    // 初始钱数
    int n_peo;
    // 把钱分给了多少人
    int get;
    // 得到了多少钱
    int giv_mon;
    // 给每个人多少钱
    int giv_total;
    // 一共给出去多少钱
};
int n;
people p[100];
map<string, int> mp;
map<int, string> mp2;
int main()
{
    // USACO是文件输入输出。其他平台忽视以下两行代码即可
    freopen("gift1.in", "r", stdin);
    freopen("gift1.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        string name;
        cin >> name;
        mp.insert(make_pair(name, i));
        mp2[i] = name;
    }

    for (int i = 1; i <= n; i++)
    {
        string name;
        int m, ng;
        int index;
        cin >> name;
        cin >> m >> ng;
        index = mp.find(name)->second;
        p[index].init = m;
        p[index].n_peo = ng;
        if (ng != 0)
            p[index].giv_mon = m / ng;
        p[index].giv_total = p[index].giv_mon * p[index].n_peo;
        for (int j = 1; j <= ng; j++)
        {
            string na;
            int ind;
            cin >> na;
            ind = mp.find(na)->second;
            p[ind].get += p[index].giv_mon;
        }
    }

    for (int i = 1; i <= n; i++)
    {
        cout << mp2[i] << ' ';
        cout << p[i].get - p[i].giv_total << endl;
    }
    // system("pause");
    return 0;
}

如果该题解对您有帮助的话,希望您不吝点赞。
欢迎讨论交流,期待共同进步!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值