USACO Training Section 1.2 Greedy Gift Givers

这道题难度不大,算是纯纯的模拟题目,但是交了5次都没有过,原因是没有把送礼者剩余的钱加回来,加回来就AC了。

因为数据很小(2 <= NP <= 10)所以不用优化。

保存人的名字的时候其实可以用vector<string>,理论上应该更加方便、高效。

代码如下:

/*
 ID: aircraf1 PROB: gift1 LANG: C++ */

#include 
<fstream>

#include 
<cstring> using namespace std; int np; int find(string s); struct

{ string
name; }p[11 ]; int main() { ifstream fin ("gift1.in" ); ofstream fout ("gift1.out" ); int in[11], out[11 ], giver, receiver; fin >> np; memset(in, 0, sizeof(in )); memset(out, 0, sizeof(out )); //fout << 'db:' << np << endl; for (int i = 1; i <= np; i++ ) fin >> p[i].name; for (int i = 1; i <= np; i++ ) { int ng; string gn; fin >> gn; giver = find(gn); fin >> out[giver] >> ng; if (ng) { string rn; for (int j = 1; j <= ng; j++ ) { string rn; int gift = out[giver] / ng, rest = out[giver] % ng; fin >> rn; receiver = find(rn); in[receiver] += gift; out[giver] -= rest; } } } for (int i = 1; i <= np; i++ ) fout << p[i].name << ' ' << in[i] - out[i] << endl; fin.close(); fout.close(); return 0 ; } int find(string s) { int n; for (int i = 1; i <= np; i++ ) { if (p[i].name == s) n = i; } return
 n; }

你可能感兴趣的:(USACO Training Section 1.2 Greedy Gift Givers)