lrj紫书第五章
Posted yijiull
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lrj紫书第五章相关的知识,希望对你有一定的参考价值。
1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低。实践中一般用C字符串和哈希表来实现。 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using namespace std; 12 13 typedef pair<int,int> PII; 14 15 const int maxr = 10000 + 5; 16 const int maxc = 10 + 5; 17 18 int m, n, db[maxr][maxc], cnt; 19 20 map<string, int> id; 21 int ID(const string& s) { 22 if(!id.count(s)) { 23 id[s] = ++cnt; 24 } 25 return id[s]; 26 } 27 28 void find() { 29 for(int c1 = 0; c1 < m; c1++) 30 for(int c2 = c1+1; c2 < m; c2++) { 31 map<PII, int> d; 32 for(int i = 0; i < n; i++) { 33 PII p = make_pair(db[i][c1], db[i][c2]); 34 if(d.count(p)) { 35 printf("NO\n"); 36 printf("%d %d\n", d[p]+1, i+1); 37 printf("%d %d\n", c1+1, c2+1); 38 return; 39 } 40 d[p] = i; 41 } 42 } 43 printf("YES\n"); 44 } 45 46 47 int main() { 48 string s; 49 while(getline(cin, s)) { 50 stringstream ss(s); 51 if(!(ss >> n >> m)) break; 52 cnt = 0; 53 id.clear(); 54 for(int i = 0; i < n; i++) { 55 getline(cin, s); 56 int lastpos = -1; 57 for(int j = 0; j < m; j++) { 58 int p = s.find(‘,‘, lastpos+1); 59 if(p == string::npos) p = s.length(); 60 db[i][j] = ID(s.substr(lastpos+1, p - lastpos - 1)); 61 lastpos = p; 62 } 63 } 64 find(); 65 } 66 return 0; 67 }
1 // UVa207 PGA Tour Prize Money 2 // Rujia Liu 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<cmath> 7 #include<algorithm> 8 #include<cassert> 9 using namespace std; 10 11 #define REP(i,n) for(int i = 0; i < (n); i++) 12 13 const int maxn = 144; 14 const int n_cut = 70; 15 16 struct Player { 17 char name[25]; 18 int amateur; 19 int sc[4]; 20 int sc36, sc72, dq; 21 int rnds; 22 } player[maxn]; 23 24 int n; 25 double purse, p[n_cut]; 26 27 bool cmp1(const Player& p1, const Player& p2) { 28 if(p1.sc36 < 0 && p2.sc36 < 0) return false; // equal 29 if(p1.sc36 < 0) return false; // p2 smaller 30 if(p2.sc36 < 0) return true; // p1 smaller 31 return p1.sc36 < p2.sc36; 32 } 33 34 bool cmp2(const Player& p1, const Player& p2) { 35 if(p1.dq && p2.dq) { 36 if(p1.rnds != p2.rnds) return p2.rnds < p1.rnds; 37 if(p1.sc72 != p2.sc72) return p1.sc72 < p2.sc72; 38 return strcmp(p1.name, p2.name) < 0; 39 } 40 if(p1.dq) return false; 41 if(p2.dq) return true; 42 if(p1.sc72 != p2.sc72) return p1.sc72 < p2.sc72; 43 return strcmp(p1.name, p2.name) < 0; 44 } 45 46 void print_result() { 47 printf("Player Name Place RD1 RD2"); 48 printf(" RD3 RD4 TOTAL Money Won\n"); 49 printf("---------------------------------------"); 50 printf("--------------------------------\n"); 51 52 int i = 0, pos = 0; 53 while(i < n) { 54 if(player[i].dq) { 55 printf("%s ",player[i].name); 56 REP(j,player[i].rnds) printf("%-5d", player[i].sc[j]); 57 REP(j,4-player[i].rnds) printf(" "); 58 printf("DQ\n"); 59 i++; 60 continue; 61 } 62 63 int j = i; 64 int m = 0; // number of tied players 65 bool have_money = false; 66 double tot = 0.0; // total pooled money 67 while(j < n && player[i].sc72 == player[j].sc72) { 68 if(!player[j].amateur) { 69 m++; 70 if(pos < n_cut) { 71 have_money = true; // yeah! they still have money 72 tot += p[pos++]; 73 } 74 } 75 j++; 76 } 77 78 // print player [i,j) together because they have the same rank 79 int rank = i + 1; // rank of all these m players 80 double amount = purse * tot / m; // if m=0, amount will be nan but we don‘t use it in that case :) 81 while(i < j) { 82 printf("%s ", player[i].name); 83 char t[5]; 84 sprintf(t, "%d%c", rank, m > 1 && have_money && !player[i].amateur ? ‘T‘ : ‘ ‘); 85 printf("%-10s", t); 86 REP(e,4) printf("%-5d", player[i].sc[e]); 87 88 // with prize 89 if(!player[i].amateur && have_money) { 90 printf("%-10d", player[i].sc72); 91 printf("$%9.2lf\n", amount / 100.0); 92 } else 93 printf("%d\n", player[i].sc72); 94 i++; 95 } 96 } 97 } 98 99 int main() { 100 int T; 101 char s[40]; 102 103 gets(s); 104 sscanf(s,"%d",&T); 105 while(T--) { 106 gets(s); // empty line 107 108 // prize 109 gets(s); 110 sscanf(s,"%lf", &purse); 111 REP(i,n_cut) { 112 gets(s); 113 sscanf(s, "%lf", &p[i]); 114 } 115 116 // players 117 gets(s); 118 sscanf(s, "%d", &n); 119 assert(n <= 144); 120 REP(k,n) { 121 // read a 32-character line 122 gets(s); 123 124 // player name 125 strncpy(player[k].name, s, 20); 126 player[k].name[20] = 0; 127 player[k].amateur = 0; 128 if(strchr(player[k].name, ‘*‘)) { 129 player[k].amateur = 1; 130 } 131 132 // scores 133 player[k].sc36 = player[k].sc72 = player[k].dq=0; 134 memset(player[k].sc, -1, sizeof(player[k].sc)); 135 REP(i,4) { 136 // raw score 137 char t[5]; 138 REP(j,3) t[j] = s[20 + i*3 + j]; t[3] = ‘\0‘; 139 140 // parse 141 if(!sscanf(t,"%d", &player[k].sc[i])) { 142 // DQ! 143 player[k].rnds = i; 144 player[k].dq = -1; 145 if(i < 2) player[k].sc36 = -1; 146 break; // skip other rounds (filled with -1, initially) 147 } else { 148 player[k].sc72 += player[k].sc[i]; 149 if(i < 2) 150 player[k].sc36 += player[k].sc[i]; 151 } 152 } 153 } 154 155 // round 1 156 sort(player, player+n, cmp1); 157 assert(player[n_cut-1].sc36 >= 0); 158 for(int i = n_cut-1; i < n; i++) 159 if(i == n-1 || player[i].sc36 != player[i+1].sc36) { n = i+1; break; } 160 161 // round 2 162 sort(player, player+n, cmp2); 163 164 // print result 165 print_result(); 166 167 if(T) printf("\n"); 168 } 169 170 return 0; 171 }
以上是关于lrj紫书第五章的主要内容,如果未能解决你的问题,请参考以下文章
紫书第五章训练 uva 10763 Foreign Exchange by crq
紫书第五章训练 uva 10391 Compound Words by crq
紫书第五章训练3 D - Throwing cards away I
紫书第五章训练 uva 1594 Ducci Sequence by crq