USACO Section 1.1 Greedy Gift Givers

/*
ID: lucien23
PROG: gift1
LANG: C++
*/

#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;

int main()
{
	ifstream infile("gift1.in");
	ofstream outfile("gift1.out");
	if(!infile || !outfile)
	{
		cout<<"file operation failure!"<<endl;
		return -1;
	}

	int NP;
	infile>>NP;
	map<string,int> mapFriends;
	string *friends=new string[NP];
	string name;
	for (int i=0;i<NP;i++)
	{
		infile>>name;
		friends[i]=name;
		mapFriends[name]=0;
	}

	string giver;
	string receiver;
	int NG,totalMoney,evenMoney;
	for (int i=0;i<NP;i++)
	{
		infile>>giver>>totalMoney>>NG;
		if(NG==0)
			continue;
		evenMoney=(totalMoney-totalMoney%NG)/NG;
		mapFriends[giver]-=evenMoney*NG;
		for (int j=0;j<NG;j++)
		{
			infile>>receiver;
			mapFriends[receiver]+=evenMoney;
		}
	}

	for (int i=0;i<NP;i++)
	{
		outfile<<friends[i]<<" "<<mapFriends[friends[i]]<<endl;
	}

	delete[] friends;

	return 0;
}

你可能感兴趣的:(C++,USACO)