TOJ刷题记录(2/50)

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

TOJ刷题记录(2/50)的相关文章

刷题记录:[De1CTF 2019]Giftbox &amp;&amp; Comment

目录 刷题记录:[De1CTF 2019]Giftbox && Comment 一.知识点 1.sql注入 && totp 2.RCE 3.源码泄露 4.敏感文件读取 刷题记录:[De1CTF 2019]Giftbox && Comment 题目复现链接:https://buuoj.cn/challenges 参考链接:De1CTF Web WriteUp BUUCTF平台 web writeup 第三弹 一.知识点 1.sql注入 && t

BZOJ 刷题记录 PART 4

[BZOJ1143]CTSC的题目...先用floyed传递闭包,然后直接上匈牙利算法. [BZOJ1452]从未写过的二维树状数组.好像很简单.. struct two_bit { int f[305][305]; inline void add(int x,int z,int A) { for (;x<=n;x+=L(x)) for (int y=z;y<=m;y+=L(y)) f[x][y]+=A; } inline int ask(int x,int z) { int ans=0; f

[2015.6.28] OI刷题记录

FZSZOJ刷题记录: 1051 砝码称重: DP 多重背包 1058 liqeuer: 序列DP 1061 挖地雷:DP,注意需要倒过来做DP,同时记录路径. 1059 Number:DP 1054 数塔问题:同数字三角形,普通DP 1390 等式问题:爆搜,枚举每个+号或-号的位置 1006 中位数:维护大根堆+小根堆,每次插入调整 1005 Cube Stacking:并查集维护当前在第几个和当前集合的高度,并查集变种. 1073 DNA分子的最佳比对:序列DP 1110 奖学金:傻逼题,

首师大附中科创教育平台 我的刷题记录(3)

首师大附中科创教育平台我的刷题记录(给大家刷11--15题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> #include<math.h> using namespace std; int main() { int x,y; cin>>x; y=abs(x+2); cout<<x*x-y+5<<endl; return 0; } 绝对值函数 #include

BZOJ 刷题记录 PART 5

拖了好久才写的. [BZOJ2821]接触分块大法.这道题略有点新颖.首先我们先分块,然后统计每块中每个数出现的个数. 下面是联立各个方块,预处理出第I个方块到第J个方块出现正偶数次数的个数. for (i=1;i<=s;i++) { for (j=i;j<=s;j++) { sum[i][j]=sum[i][j-1]; for (k=a[j].l;k<=a[j].r;k++) { temp[data[k]]++; if (!(temp[data[k]]&1)) sum[i][j

首师大附中科创教育平台 我的刷题记录(7)

首师大附中科创教育平台我的刷题记录(这次给大家刷多一点,31--40题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> using namespace std; int main() { int n; cin>>n; if(n%4==0&&n%100!=0||n%400==0) cout<<"yes"<<endl; else cout&

首师大附中科创教育平台 我的刷题记录(6)

首师大附中科创教育平台我的刷题记录(给大家刷26--30题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> #include<math.h> using namespace std; int main() { double I,R,r,E; cin>>I>>R>>r; E=I*(R+r); cout<<E<<endl; return 0

首师大附中科创教育平台 我的刷题记录 0284 最强大脑

从现在开始,我的刷题记录都开始给大家一个一个刷!今天给大家献上“E”级题:最强大脑!! 试题编号:0284     最强大脑 难度级别:E: 运行时间限制:3000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 zhb国是一个传说中的国度,国家的居民叫做最强(chang)大脑(diao).Zhb国是一个N×M的矩形方阵,每个格子代表一个街区.然而zhb国是没有交通工具的.居民大脑(diao)们完全靠地面的弹射装置来移动.每个街区都装有弹射装置.使用弹射装置是需

首师大附中科创教育平台 我的刷题记录(5)

首师大附中科创教育平台我的刷题记录(给大家刷21--25题吧) 仅供同学们参考,禁止抄袭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include<iostream> #include<math.h> using namespace std; int main() { double a,b,x; cin>>a>>b; x=-b/a; cout<<x<<endl; return 0; } 一元一次方程 #in