P1172:二分,最大化最小值
1 #include <algorithm> 2 #include <bitset> 3 #include <cctype> 4 #include <complex> 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <map> 9 #include <queue> 10 #include <set> 11 #include <string> 12 #include <vector> 13 14 using namespace std; 15 16 const int inf = 0x3f3f3f3f; 17 18 #define rep(id,from,to) for (int id = (from); id < (to); id++) 19 #define irep(id,from,to) for (int id = (from); id > (to); id--) 20 #define reset(arr,c) memset (arr, c, sizeof (arr) ) 21 #define grt(type) greater<type> 22 #define Que(type) queue<type> 23 #define Pque(type) priority_queue<type> 24 #define gPque(type) priority_queue<type, vector<type>, greater<type> > 25 26 map<string, int> city; 27 int N, R; 28 29 struct Edge 30 { 31 int to, next, len; 32 void assign (int t, int n, int l) 33 { 34 to = t; 35 next = n; 36 len = l; 37 } 38 }; 39 40 Edge elist[40000]; 41 int head[205]; 42 int ecnt; 43 int st, dest; 44 45 inline void add (int v1, int v2, int l) 46 { 47 elist[ecnt].assign (v2, head[v1], l); 48 head[v1] = (ecnt++); 49 elist[ecnt].assign (v1, head[v2], l); 50 head[v2] = (ecnt++); 51 } 52 53 bool input() 54 { 55 reset (head, -1); 56 ecnt = 0; 57 cin >> N >> R; 58 if (N == 0) return false; 59 string nm1, nm2; 60 int len; 61 int cnt = 0; 62 rep (i, 0, R) 63 { 64 cin >> nm1 >> nm2 >> len; 65 if (city.count (nm1) == 0) city[nm1] = (cnt++); 66 if (city.count (nm2) == 0) city[nm2] = (cnt++); 67 add (city[nm1], city[nm2], len); 68 } 69 cin >> nm1 >> nm2; 70 st = city[nm1]; 71 dest = city[nm2]; 72 return true; 73 } 74 75 bool vis[205]; 76 77 bool dfs (int cur, int lim) 78 { 79 vis[cur] = true; 80 if (cur == dest) return true; 81 bool res = false; 82 for (int t, e = head[cur]; e != -1; e = elist[e].next) 83 { 84 t = elist[e].to; 85 if (!vis[t] && elist[e].len >= lim) 86 res |= dfs (t, lim); 87 } 88 return res; 89 } 90 91 int solve() 92 { 93 int lb = 1, rb = 10000; 94 while (lb < rb) 95 { 96 reset (vis, 0); 97 int m = (lb + rb + 1) >> 1; 98 if (!dfs (st, m) ) rb = m - 1; 99 else lb = m; 100 } 101 return lb; 102 } 103 104 int main() 105 { 106 int ks (0); 107 while (input() ) printf ("Scenario #%d\n%d tons\n\n", ++ks, solve() ); 108 return 0; 109 }
TOJ P1172
P1290:模拟题
1 #include <algorithm> 2 #include <bitset> 3 #include <cctype> 4 #include <complex> 5 #include <cstdio> 6 #include <cstring> 7 #include <map> 8 #include <queue> 9 #include <set> 10 #include <vector> 11 12 using namespace std; 13 14 const int inf = 0x3f3f3f3f; 15 16 #define rep(id,from,to) for (int id = (from); id < (to); id++) 17 #define irep(id,from,to) for (int id = (from); id > (to); id--) 18 #define reset(arr,c) memset (arr, c, sizeof (arr) ) 19 #define grt(type) greater<type> 20 #define Que(type) queue<type> 21 #define Pque(type) priority_queue<type> 22 #define gPque(type) priority_queue<type, vector<type>, greater<type> > 23 24 25 struct Card 26 { 27 char suit; 28 int val; 29 bool read() 30 { 31 char ch[2]; 32 if (scanf ("%s", ch) == EOF) return false; 33 suit = ch[1]; 34 if (ch[0] >= ‘2‘ && ch[0] <= ‘9‘) val = ch[0] - ‘0‘; 35 else switch (ch[0]) 36 { 37 case ‘J‘: 38 val = 11; 39 break; 40 case ‘Q‘: 41 val = 12; 42 break; 43 case ‘K‘: 44 val = 13; 45 break; 46 case ‘A‘: 47 val = 14; 48 break; 49 } 50 return true; 51 } 52 }; 53 54 int isStraight (int* cnt, bool isFlush) 55 { 56 bool ok = true; 57 int i; 58 rep (j, 2, 15) if (cnt[j]) 59 { 60 i = j; 61 break; 62 } 63 rep (j, i + 1, i + 5) if (!cnt[j]) 64 { 65 ok = false; 66 break; 67 } 68 return ok ? (isFlush ? 8 : 4) : 0; 69 } 70 71 int isFourOfAKind (int* cnt) 72 { 73 bool ok = false; 74 rep (i, 2, 15) if (cnt[i] == 4) 75 { 76 ok = true; 77 break; 78 } 79 return ok ? 7 : 0; 80 } 81 82 int isFullHouse (int* cnt) 83 { 84 bool f3 = false, f2 = false; 85 rep (i, 2, 15) 86 { 87 if (cnt[i] == 3) f3 = true; 88 else if (cnt[i] == 2) f2 = true; 89 } 90 return f3 && f2 ? 6 : 0; 91 } 92 93 int isThreeOfAKind (int* cnt) 94 { 95 rep (i, 2, 15) if (cnt[i] == 3) return 3; 96 return 0; 97 } 98 99 int isPair (int* cnt) 100 { 101 int res = 0; 102 rep (i, 2, 15) if (cnt[i] == 2) ++res; 103 return res; 104 } 105 106 int cmpStraight (int* cntA, int* cntB) 107 { 108 int ma, mb; 109 irep (i, 14, 1) if (cntA[i]) 110 { 111 ma = i; 112 break; 113 } 114 irep (i, 14, 1) if (cntB[i]) 115 { 116 mb = i; 117 break; 118 } 119 if (ma > mb) return 1; 120 else if (ma < mb) return -1; 121 else return 0; 122 } 123 124 int cmpFour (int* cntA, int* cntB) 125 { 126 int ma, mb; 127 rep (i, 2, 15) if (cntA[i] == 4) 128 { 129 ma = i; 130 break; 131 } 132 rep (i, 2, 15) if (cntB[i] == 4) 133 { 134 mb = i; 135 break; 136 } 137 if (ma > mb) return 1; 138 else if (mb > ma) return -1; 139 else return 0; 140 } 141 142 int cmpFullHouse (int* cntA, int* cntB) 143 { 144 int ma, mb; 145 rep (i, 2, 15) if (cntA[i] == 3) 146 { 147 ma = i; 148 break; 149 } 150 rep (i, 2, 15) if (cntB[i] == 3) 151 { 152 mb = i; 153 break; 154 } 155 if (ma > mb) return 1; 156 else if (mb > ma) return -1; 157 else return 0; 158 } 159 160 int cmpPair (int* cntA, int* cntB, int pr) 161 { 162 int pa = 0, ha = 0, pb = 0, hb = 0; 163 int ma[5] = {0}, mb[5] = {0}; 164 irep (i, 14, 1) 165 { 166 if (cntA[i] == 2) ma[pa++] = i; 167 else if (cntA[i] == 1) ma[pr + (ha++)] = i; 168 } 169 irep (i, 14, 1) 170 { 171 if (cntB[i] == 2) mb[pb++] = i; 172 else if (cntB[i] == 1) mb[pr + (hb++)] = i; 173 } 174 rep (i, 0, 5) 175 { 176 if (ma[i] > mb[i]) return 1; 177 else if (mb[i] > ma[i]) return -1; 178 } 179 return 0; 180 } 181 182 int cmp (Card* A, Card* B) 183 { 184 int cntA[15] = {0}; 185 int cntB[15] = {0}; 186 rep (i, 0, 5) ++cntA[A[i].val]; 187 rep (i, 0, 5) ++cntB[B[i].val]; 188 int typeA = 0, typeB = 0; 189 bool isFlushA = true, isFlushB = true; 190 rep (i, 1, 5) if (A[i].suit != A[0].suit) 191 { 192 isFlushA = false; 193 break; 194 } 195 rep (i, 1, 5) if (B[i].suit != B[0].suit) 196 { 197 isFlushB = false; 198 break; 199 } 200 typeA = isFlushA ? 5 : 0; 201 typeA = max (typeA, isStraight (cntA, isFlushA) ); 202 typeA = max (typeA, isFourOfAKind (cntA) ); 203 typeA = max (typeA, isFullHouse (cntA) ); 204 typeA = max (typeA, isThreeOfAKind (cntA) ); 205 typeA = max (typeA, isPair (cntA) ); 206 typeB = isFlushA ? 5 : 0; 207 typeB = max (typeB, isStraight (cntB, isFlushB) ); 208 typeB = max (typeB, isFourOfAKind (cntB) ); 209 typeB = max (typeB, isFullHouse (cntB) ); 210 typeB = max (typeB, isThreeOfAKind (cntB) ); 211 typeB = max (typeB, isPair (cntB) ); 212 if (typeA > typeB) return 1; 213 else if (typeA < typeB) return -1; 214 if (typeA == 4 || typeA == 8) return cmpStraight (cntA, cntB); 215 else if (typeA == 7) return cmpFour (cntA, cntB); 216 else if (typeA == 6 || typeA == 3) return cmpFullHouse (cntA, cntB); 217 else return cmpPair (cntA, cntB, (typeA == 5) ? 0 : typeA); 218 return 0; 219 } 220 221 int main() 222 { 223 Card A[5], B[5]; 224 while (A[0].read() ) 225 { 226 rep (i, 1, 5) A[i].read(); 227 rep (i, 0, 5) B[i].read(); 228 int ans = cmp (A, B); 229 if (ans == 1) printf ("Black wins.\n"); 230 else if (ans == -1) printf ("White wins.\n"); 231 else printf ("Tie.\n"); 232 } 233 return 0; 234 }
TOJ P1290
P1557:H2O,暴力枚举即可
1 #include <algorithm> 2 #include <bitset> 3 #include <cctype> 4 #include <complex> 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <map> 9 #include <queue> 10 #include <set> 11 #include <string> 12 #include <vector> 13 14 using namespace std; 15 16 const int inf = 0x3f3f3f3f; 17 18 #define rep(id,from,to) for (int id = (from); id < (to); id++) 19 #define irep(id,from,to) for (int id = (from); id > (to); id--) 20 #define reset(arr,c) memset (arr, c, sizeof (arr) ) 21 #define grt(type) greater<type> 22 #define Que(type) queue<type> 23 #define Pque(type) priority_queue<type> 24 #define gPque(type) priority_queue<type, vector<type>, greater<type> > 25 26 int pr[16], pg[16], pb[16]; 27 28 inline int dist (int r, int g, int b, int id) 29 { 30 return (r - pr[id]) * (r - pr[id]) + (g - pg[id]) * (g - pg[id]) + (b - pb[id]) * (b - pb[id]); 31 } 32 33 int main() 34 { 35 rep (i, 0, 16) scanf ("%d%d%d", pr + i, pg + i, pb + i); 36 int r, g, b; 37 while (1) 38 { 39 scanf ("%d%d%d", &r, &g, &b); 40 if (r == -1) break; 41 int md = inf, id = inf; 42 rep (i, 0, 16) 43 { 44 int d = dist (r, g, b, i); 45 if (d < md) 46 { 47 md = d; 48 id = i; 49 } 50 } 51 printf ("(%d,%d,%d) maps to (%d,%d,%d)\n", r, g, b, pr[id], pg[id], pb[id]); 52 } 53 return 0; 54 }
TOJ P1557
时间: 2024-10-18 17:35:16