USACO习题:Greedy Gift Givers

几个朋友给钱的问题。建立一个数组保存每个人的钱,收的加,出的减即可。

 

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


using namespace std;


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

int np = 0;
fin>>np; //get the number of people

vector<string> people(np);
//name mapping with money
map<string,int> wallet;
string name;
for(int i =0 ; i<np;i++){
fin>>name;
wallet[name]=0;
people[i]=name;
}


//calculate the money flow
for(int i=0;i<np;i++){

int money=0,number_of_friend=0,gift=0;
fin>>name;
fin>>money>>number_of_friend;
string friend_name;

//cout<<name<<" "<<money<<" "<<number_of_friend<<endl;
if(number_of_friend==0)
continue; //don't forget to check zero
gift=(int)(money/number_of_friend);//guard cast

//the giver gives his money out
wallet[name] -= gift*number_of_friend;
for(int j=0;j<number_of_friend;j++){
fin>>friend_name;
//the friend receives the giver's money
wallet[friend_name]+=gift;
}
}

//output the money
for(vector<string>::iterator iter=people.begin();
iter!=people.end();++iter)
fout<<*iter<<" "<<wallet[*iter]<<endl;


fin.close();
fout.close();
return 0;
}



posted on 2012-03-18 17:26  lzyzizi  阅读(165)  评论(0编辑  收藏  举报

导航