题目链接:https://vjudge.net/problem/UVA-12412
题目大意
略。
分析
比较大规模的模拟,注意输入输出,浮点数精度,还有排名相同的输出顺序,还有一些边界情况处理。
代码如下
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 5 #define Rep(i,n) for (int i = 0; i < (n); ++i) 6 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 12 13 #define pr(x) cout << #x << " = " << x << " " 14 #define prln(x) cout << #x << " = " << x << endl 15 16 #define LOWBIT(x) ((x)&(-x)) 17 18 #define ALL(x) x.begin(),x.end() 19 #define INS(x) inserter(x,x.begin()) 20 21 #define ms0(a) memset(a,0,sizeof(a)) 22 #define msI(a) memset(a,inf,sizeof(a)) 23 #define msM(a) memset(a,-1,sizeof(a)) 24 25 #define MP make_pair 26 #define PB push_back 27 #define ft first 28 #define sd second 29 30 template<typename T1, typename T2> 31 istream &operator>>(istream &in, pair<T1, T2> &p) { 32 in >> p.first >> p.second; 33 return in; 34 } 35 36 template<typename T> 37 istream &operator>>(istream &in, vector<T> &v) { 38 for (auto &x: v) 39 in >> x; 40 return in; 41 } 42 43 template<typename T1, typename T2> 44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 45 out << "[" << p.first << ", " << p.second << "]" << " "; 46 return out; 47 } 48 49 inline int gc(){ 50 static const int BUF = 1e7; 51 static char buf[BUF], *bg = buf + BUF, *ed = bg; 52 53 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 54 return *bg++; 55 } 56 57 inline int ri(){ 58 int x = 0, f = 1, c = gc(); 59 for(; c<48||c>57; f = c=='-'?-1:f, c=gc()); 60 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 61 return x*f; 62 } 63 64 template<class T> 65 inline string toString(T x) { 66 ostringstream sout; 67 sout << x; 68 return sout.str(); 69 } 70 71 typedef long long LL; 72 typedef unsigned long long uLL; 73 typedef pair< double, double > PDD; 74 typedef pair< int, int > PII; 75 typedef pair< int, PII > PIPII; 76 typedef pair< string, int > PSI; 77 typedef pair< int, PSI > PIPSI; 78 typedef set< int > SI; 79 typedef set< PII > SPII; 80 typedef vector< int > VI; 81 typedef vector< VI > VVI; 82 typedef vector< PII > VPII; 83 typedef map< int, int > MII; 84 typedef map< int, string > MIS; 85 typedef map< int, PII > MIPII; 86 typedef map< PII, int > MPIII; 87 typedef map< string, int > MSI; 88 typedef map< string, string > MSS; 89 typedef map< PII, string > MPIIS; 90 typedef map< PII, PII > MPIIPII; 91 typedef multimap< int, int > MMII; 92 typedef multimap< string, int > MMSI; 93 //typedef unordered_map< int, int > uMII; 94 typedef pair< LL, LL > PLL; 95 typedef vector< LL > VL; 96 typedef vector< VL > VVL; 97 typedef priority_queue< int > PQIMax; 98 typedef priority_queue< int, VI, greater< int > > PQIMin; 99 const double EPS = 1e-6; 100 const LL inf = 0x7fffffff; 101 const LL infLL = 0x7fffffffffffffffLL; 102 const LL mod = 1e9 + 7; 103 const int maxN = 1e4 + 7; 104 const LL ONE = 1; 105 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 106 const LL oddBits = 0x5555555555555555; 107 108 MIS courses = {MP(0, "Chinese"), MP(1, "Mathematics"), MP(2, "English"), MP(3, "Programming")}; 109 110 struct Student{ 111 int cid, scores[4], tot = 0; 112 string sid, name; 113 double avg; 114 }; 115 vector< Student > stu; 116 MSI id_stu; 117 int totScores[107], slen; // 所有学生的分数数组和在籍学生人数 118 119 void printWelcome() { 120 printf ("Welcome to Student Performance Management System (SPMS). "); 121 printf ("1 - Add "); 122 printf ("2 - Remove "); 123 printf ("3 - Query "); 124 printf ("4 - Show ranking "); 125 printf ("5 - Show Statistics "); 126 printf ("0 - Exit "); 127 } 128 129 void add() { 130 string tmp; 131 while(cin >> tmp) { 132 printf ("Please enter the SID, CID, name and four scores. Enter 0 to finish. "); 133 if(tmp == "0") break; 134 if(id_stu.find(tmp) != id_stu.end()) { 135 Rep(i, 6) cin >> tmp; 136 printf ("Duplicated SID. "); 137 continue; 138 } 139 Student t; 140 int cnt = 0; 141 t.sid = tmp; 142 cin >> t.cid >> t.name; 143 Rep(i, 4) { 144 cin >> t.scores[i]; 145 t.tot += t.scores[i]; 146 t.avg = t.tot / 4.0; 147 } 148 totScores[slen++] = -t.tot; 149 150 id_stu[tmp] = stu.size(); 151 stu.PB(t); 152 } 153 } 154 155 void del() { 156 string tmp; 157 while(cin >> tmp) { 158 printf("Please enter SID or name. Enter 0 to finish. "); 159 if(tmp == "0") break; 160 VI t; 161 if(isdigit(tmp[0])) { 162 if(id_stu.find(tmp) != id_stu.end()) t.PB(id_stu[tmp]); 163 } 164 else foreach(i, id_stu) if(tmp == stu[i->sd].name) t.PB(i->sd); 165 printf ("%d student(s) removed. ", t.size()); 166 foreach(i, t) { 167 id_stu.erase(stu[*i].sid); 168 --slen; 169 Rep(j, slen) { 170 if(totScores[j] == -stu[*i].tot) { 171 swap(totScores[j], totScores[slen]); 172 break; 173 } 174 } 175 } 176 } 177 } 178 179 void query() { 180 sort(totScores, totScores + slen); 181 string tmp; 182 while(cin >> tmp) { 183 printf("Please enter SID or name. Enter 0 to finish. "); 184 if(tmp == "0") break; 185 VI t; 186 if(isdigit(tmp[0])) { 187 if(id_stu.find(tmp) != id_stu.end()) t.PB(id_stu[tmp]); 188 } 189 else foreach(i, id_stu) if(tmp == stu[i->sd].name) t.PB(i->sd); 190 191 sort(ALL(t)); 192 foreach(i, t) { 193 int rank = lower_bound(totScores, totScores + slen, -stu[*i].tot) - totScores + 1; 194 printf("%d %s %d %s %d %d %d %d %d %.2f ", rank, stu[*i].sid.c_str(), stu[*i].cid, stu[*i].name.c_str(), stu[*i].scores[0], stu[*i].scores[1], stu[*i].scores[2], stu[*i].scores[3], stu[*i].tot, stu[*i].avg + EPS); 195 } 196 } 197 } 198 199 void show_rank() { 200 printf("Showing the ranklist hurts students' self-esteem. Don't do that. "); 201 } 202 203 void show_stat() { 204 int totS[4] = {0}, len = 0; 205 int passed[4] = {0}, failed[4] = {0}, passedN[5] = {0}; 206 int x; 207 printf("Please enter class ID, 0 for the whole statistics. "); 208 cin >> x; 209 210 foreach(i, id_stu) { 211 if(!x || stu[i->sd].cid == x) { 212 ++len; 213 int cnt = 0; 214 Rep(j, 4) { 215 totS[j] += stu[i->sd].scores[j]; 216 if(stu[i->sd].scores[j] >= 60) { 217 ++passed[j]; 218 ++cnt; 219 } 220 else ++failed[j]; 221 } 222 ++passedN[cnt]; 223 } 224 } 225 226 Rep(i, 4) { 227 printf ("%s ", courses[i].c_str()); 228 if(len) printf ("Average Score: %.2f ", 1.0 * totS[i] / len + EPS); 229 else printf ("Average Score: -nan "); 230 printf ("Number of passed students: %d ", passed[i]); 231 printf ("Number of failed students: %d ", failed[i]); 232 printf (" "); 233 } 234 printf ("Overall: "); 235 printf ("Number of students who passed all subjects: %d ", passedN[4]); 236 printf ("Number of students who passed 3 or more subjects: %d ", passedN[4] + passedN[3]); 237 printf ("Number of students who passed 2 or more subjects: %d ", passedN[4] + passedN[3] + passedN[2]); 238 printf ("Number of students who passed 1 or more subjects: %d ", passedN[4] + passedN[3] + passedN[2] + passedN[1]); 239 printf ("Number of students who failed all subjects: %d ", passedN[0]); 240 printf (" "); 241 } 242 243 void (*func[5])() = {add, del, query, show_rank, show_stat}; 244 245 int main(){ 246 //freopen("MyOutput.txt","w",stdout); 247 //freopen("input.txt","r",stdin); 248 //INIT(); 249 int op; 250 while(1) { 251 printWelcome(); 252 cin >> op; 253 if(op == 0) break; 254 func[op - 1](); 255 } 256 return 0; 257 }