Uva oj 119-Greedy gift givers (Greedy gifts)

Source: Internet
Author: User

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

 

The Problem
Problem

This problem involves determining, for a group of gift-giving friends, how much more each person gives than they receive (and vice versa for those that view gift-giving with cynicism ).
This problem is to determine the number of people who give gifts to each other (or those who do not get the rewards ).

In this problem each person sets aside some money for gift-giving and divides this money evenly among all those to whom gifts are given.
In the question, every friend took out a sum of money to give gifts and distributed the money evenly to all his friends.

However, 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.
However, no matter which circle of friends, some people always give more than others (or more than others), while some people have more money.

Given a group of friends, the money each person in the group when Ds on gifts, and a (sub) list of friends to whom each person gives gifts; you are to write a program that determines how much more (or less) each person in the group gives than they receive.
Give a circle of friends, and the money each person spends on buying gifts, and a list of friends who will give gifts to each person.ProgramCalculate the number of people in this circle of friends who pay more than returns.

 

The input
Input

The input is a sequence of gift-giving groups. A group consists of several lines:
Input is a group of moments of mutual gifts. A circle consists of the following rows:

    • The number of people in the group,
      Number of people in the circle,
    • A list of the names of each person in the group,
      Name of each person in the circle,
    • A line for each person in the group consisting of the name of the person, the amount of money spent on gifts, the number of people to whom gifts are given, and the names of those to whom gifts are given.
      Each person has an exclusive row, including the name, the total amount used for the purchase, the number of people who want to give gifts, and the name of each person who wants to give gifts.

All names are lower-case letters, there are no more than 10 people in a group, and no name is more than 12 characters in length. money is a non-negative integer less than 2000.
All names are small letters, and each circle contains no more than 10 people. All names are less than or equal to 12 characters in length. The total amount is a non-negative integer less than 2000.

The input consists of one or more groups and is terminated by end-of-file.
After entering one or more circles, the end is indicated by EOF.

 

The output
Output

For each group of gift-givers, the name of each person in the group shocould be printed on a line followed by the net gain (or loss) received (or spent) by the person. names in A group shoshould be printed in the same order in which they first appear in the input.
For each group of friends who give gifts to each other, each of them has to output a line: First name, followed by his net income (which may be negative, indicating that his payment is greater than the return ). The name in the circle should be entered in the order given in the second line.

The output for each group shocould be separated from other groups by a blank line. 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. any money not given is kept and is part of a person's "net worth" printed in the output.
The output of each circle must be an empty line separated from the output of other circles. The value of all gifts is an integer. For everyone, the total number of gifts given to all friends is the same, and as many gifts as possible. The zero header not given should be added to his net income and output.

 

Sample Input
Input example

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
3
Liz Steve Dave
Liz 30 1 Steve
Steve 55 2 Liz Dave
Dave 0 2 Steve Liz

 

Sample output
Output example

Dave 302
Laura 66
Wen-359
Vick 141
Amr-1, 150

Liz-3
Steve-24
Dave 27

 

Analysis
Analysis

This is a pure language question. You can use it to practice STL and see how many lines can solve the problem (CodeValid number of rows is 29 ). NoAlgorithmAnd there is nothing to analyze. The only thing to note is that the upper and lower rows cannot be empty when the results of the first circle are output, and there must be a blank row above all the circles (after the input data ). The location cannot be wrong. Otherwise, WA.

 

Solution
Answer

 

# Include <algorithm> # include <iostream> # include <iterator> # include <string> # include <vector> using namespace STD; // main function int main (void) {// cyclically process the input circle of friends for (INT npeople, nfirst = 1; CIN> npeople; nfirst = 0) {// vecname stores the names of each user in order, vecworth is the net income vector <string> vecname (npeople); vector <int> vecworth (npeople, 0); // enter the name of all people for (INT I = 0; I <npeople; CIN> vecname [I ++]); // cyclically processes the income and expenditure of each person (INT I = 0, nmoney, n Fricnt; I <npeople; ++ I) {// read the name, amount, and friends string strname; CIN >>> strname >>> nmoney >> nfricnt; // locate this person's position in the array vector <string>: iterator ipeople = find (vecname. begin (), vecname. end (), strname); Int & ncurworth = vecworth [distance (vecname. begin (), ipeople)]; // the expenditure amount in the net income of this person is evenly distributed to friends ncurworth-= nmoney; If (nfricnt> 0) {// evenly distributed to the net income of each friend for (Int J = 0, nshare = nmoney/nfricnt; j <nfricnt; ++ J) {// locate a friend in the array and add it to the net Revenue: CIN> strname; ipeople = find (vecname. begin (), vecname. end (), strname); vecworth [distance (vecname. begin (), ipeople)] + = nshare;} // The remaining nmoney % = nfricnt after the split is calculated;} // if there are no headers left or no friends left, then the returned net income ncurworth + = nmoney;} // as required, each group of outputs should be separated by null rows, but the first group cannot have empty rows of cout <(nfirst = 1? "": "\ N"); // output results in sequence. Names are at the beginning of each line, and net income is at the end. For (INT I = 0; I! = Vecname. Size (); ++ I) {cout <vecname [I] <''<vecworth [I] <Endl ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.