Greedy Gift Givers c++

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, '\r\ and ‘\n’. Do not let your program get trapped by this!

PROGRAM NAME: gift1

INPUT FORMAT

Line #Contents
1A single integer, NP
2…NP+1Line i+1 contains the name of group member i
NP+2…endNP 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 giverNGi (0 ≤ NGi ≤ NP), the number of people to whom the giver will give giftsIf 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

OUTPUT EXPLANATION

Five names: dave, laura, owen, vick, amr. Let’s keep a table of the gives (money) each person ‘has’:

davelauraowenvickamr
00000
First, ‘dave’ splits 200 among ‘laura’, ‘owen’, and ‘vick’. That comes to 66 each, with 2 left over
-200+2+66+66+660
-1986666660
Second, ‘owen’ gives 500 to ‘dave’:
-198**+500**6666 -500660
30266-434660
Third, ‘amr’ splits 150 between ‘vick’ and ‘owen’:
30266-434 +7566 +75-150
30266-359141-150
Fourth, ‘laura’ splits 0 between ‘amr’ and ‘vick’; no changes:
30266-359141-150
Finally, ‘vick’ gives 0 to no one:
davelauraowenvickamr
30266-359141-150

AC code

/*
ID: 
TASK: gift1
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int main() {
    ofstream fout("gift1.out");
    ifstream fin("gift1.in");

    map<string, int>members;
    vector<string>names;
    int NP; fin >> NP;
    for (int i = 1; i <= NP; i++)
    {
        string name; fin >> name;
        names.push_back(name);
        members.insert(pair<string, int>(name, 0));
    }

    string giverName, receiverName;
    int amount, num;
    while (fin >> giverName >> amount >> num)
    {
        if (!amount && !num)
        {
            continue;
        }

        int perAmount = amount / num;

        while (num--)
        {
            fin >> receiverName;
            members[giverName] -= perAmount;
            members[receiverName] += perAmount;
        }
    }

    for (auto x : names)
    {
        fout << x << ' ' << members[x] << endl;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值