标签:poj priority section names 思维 cin div lag pair
CF618C,水啊。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 5 struct point { 6 ll x, y; 7 int id; 8 bool operator < (const point b) const { 9 if (x != b.x) return x < b.x; 10 return y < b.y; 11 } 12 }c[100005]; 13 14 bool ok(int i, int j, int k) { 15 return (c[k].y-c[j].y)*(c[j].x-c[i].x) != (c[j].y-c[i].y)*(c[k].x-c[j].x); 16 } 17 18 int main() { 19 int n; 20 cin >> n; 21 for (int i = 1; i <= n; i++) { 22 cin >> c[i].x >> c[i].y; 23 c[i].id = i; 24 } 25 sort(c+1, c+1+n); 26 for (int j = 3; j <= n; j++) { 27 if (ok(1, 2, j)) { 28 printf("%d %d %d\n", c[1].id, c[2].id, c[j].id); 29 return 0; 30 } 31 } 32 }
CF621C,不好dp,其实逆向思维一下用总的减去不行的概率乘积即可简单求得。
1 #include <bits/stdc++.h> 2 #define ld long double 3 #define maxn 100005 4 using namespace std; 5 6 int n, p; 7 ld pro[maxn]; 8 ld ans; 9 10 int main() { 11 cin >> n >> p; 12 for (int i = 1; i <= n; i++) { 13 int l, r; 14 cin >> l >> r; 15 ld cnt = r/p - (l-1)/p; 16 ld range = r-l+1; 17 pro[i] = 1.0 - cnt/range; 18 } 19 20 for (int i = 1; i <= n; i++) { 21 int nxt = i == n ? 1 : i+1; 22 ans += 1.0 - pro[i]*pro[nxt]; 23 } 24 cout << fixed << setprecision(6) << ans*2000; 25 return 0; 26 }
POJ2096,期望这东西的概率,正着和倒着概率是一样的,所以倒着dp的时候概率还用正着的即可。
1 #include <cstdio> 2 #define db double 3 int n, s; 4 db dp[1005][1005]; 5 6 int main() { 7 scanf("%d%d", &n, &s); 8 for (int i = n; ~i; i--) 9 for (int j = s; ~j; j--) { 10 if (i == n && j == s) continue; 11 db p1 = (db)(n-i) * (s-j) / n / s; 12 db p2 = (db)i * (s-j) / n / s; 13 db p3 = (db)(n-i) * j / n / s; 14 db p4 = (db)i * j / n / s; 15 dp[i][j] = (p1*dp[i+1][j+1] + p2*dp[i][j+1] + p3*dp[i+1][j] + 1) / (1-p4); 16 } 17 printf("%.4f", dp[0][0]); 18 return 0; 19 }
CF621A,太水啦。
1 #include <cstdio> 2 #include <queue> 3 #define ll long long 4 using namespace std; 5 6 int n, t; 7 ll ans; 8 priority_queue<ll> Q; 9 10 int main() { 11 scanf("%d", &n); 12 for (int i = 0; i < n; i++) { 13 ll x; 14 scanf("%lld", &x); 15 if (x & 1) Q.push(x), t++; 16 else ans += x; 17 } 18 for (int i = 0; i < t>>1; i++) { 19 ll x = Q.top(); 20 Q.pop(); 21 ll y = Q.top(); 22 Q.pop(); 23 ans += x + y; 24 } 25 printf("%lld", ans); 26 return 0; 27 }
CF624C,直接把二分图填了,然后查矛盾。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n, m; 5 int du[505]; 6 bool mp[505][505]; 7 char s[505]; 8 9 inline void judge(int x) { 10 if (du[x] == n-1) 11 s[x] = ‘b‘; 12 } 13 14 int main() { 15 cin >> n >> m; 16 for (int i = 0; i < m; i++) { 17 int a, b; 18 cin >> a >> b; 19 mp[a][b] = mp[b][a] = true; 20 du[a]++, du[b]++; 21 judge(a), judge(b); 22 } 23 24 int tmp = 1; 25 for (int i = 1; i <= n; i++) { 26 mp[i][i] = true; 27 if (s[i] != ‘b‘) { 28 tmp = i; 29 } 30 } 31 32 for (int i = 1; i <= n; i++) 33 if (!s[i]) { 34 if (mp[tmp][i]) s[i] = ‘a‘; 35 else s[i] = ‘c‘; 36 } 37 38 bool flag = true; 39 for (int i = 1; i <= n; i++) 40 for (int j = 1; j <= n; j++) 41 if (mp[i][j] && abs(s[i] - s[j]) == 2) 42 flag = false; 43 else if (!mp[i][j] && abs(s[i] - s[j]) < 2) 44 flag = false; 45 46 if (flag) { 47 puts("Yes"); 48 printf("%s", s+1); 49 } else { 50 puts("No"); 51 } 52 53 return 0; 54 }
CF624D,这个题好哇、题解果然要货比三家的。首先要察觉答案范围可用第一个数和最后一个数大幅度压缩。然后gcd不要死板,素因子只会比合数要求更宽松所以更优所以枚举素数即可。接下来dp思想进行处理,通过这题感觉dp就是个扩大范围的马尔可夫呀233.
1 #include <bits/stdc++.h> 2 #define ll long long 3 #define maxn 1000005 4 #define INF (1LL<<60) 5 #define pb push_back 6 #define rep(i, a, b) for (int i = a; i <= b; i++) 7 using namespace std; 8 9 int n, a, b; 10 int val[maxn]; 11 vector<int> gcd; 12 ll ans = INF; 13 14 void get(int num) { 15 for (int i = 2; i * i <= num; i++) { 16 if (num % i == 0) { 17 gcd.pb(i); 18 while (num % i == 0) 19 num /= i; 20 } 21 } 22 if (num > 1) gcd.pb(num); 23 } 24 25 ll cal(int x) { 26 ll havnt_delet = 0ll, is_deleting = 0ll, hav_done = 0ll; 27 rep(i, 1, n) { 28 if (val[i] % x == 0) { 29 hav_done = min(hav_done, is_deleting); 30 is_deleting = min(havnt_delet, is_deleting) + a; 31 } 32 else if ((val[i]-1) % x == 0 || (val[i]+1) % x == 0) { 33 hav_done = min(hav_done, is_deleting) + b; 34 is_deleting = min(havnt_delet, is_deleting) + a; 35 if (havnt_delet < INF) { 36 havnt_delet += b; 37 } 38 } else { 39 is_deleting = min(havnt_delet, is_deleting) + a; 40 hav_done = havnt_delet = INF; 41 } 42 } 43 return min(havnt_delet, min(is_deleting, hav_done)); 44 } 45 46 int main() { 47 cin >> n >> a >> b; 48 rep(i, 1, n) cin >> val[i]; 49 50 rep(i, -1, 1) { 51 get(val[1] + i); 52 get(val[n] + i); 53 } 54 gcd.erase(unique(gcd.begin(), gcd.end()), gcd.end()); 55 for (auto i : gcd) 56 ans = min(ans, cal(i)); 57 58 cout << ans; 59 return 0; 60 }
洛谷1525,二分后进行二分图,自己乱搞一下懒得看更优雅的std了。
1 #include <bits/stdc++.h> 2 #define maxn 20005 3 #define pb push_back 4 #define mp make_pair 5 #define P pair<int, int> 6 #define fi first 7 #define se second 8 using namespace std; 9 10 int n, m, maxx; 11 vector<P> v[maxn]; 12 vector<int> tmp[maxn]; 13 int vis[maxn]; 14 int l, r, mid; 15 16 bool dfs(int cur, int color) { 17 vis[cur] = color; 18 for (int i : tmp[cur]) 19 if (vis[i] && vis[i] == color) 20 return false; 21 else if (!vis[i] && !dfs(i, 3-color)) 22 return false; 23 return true; 24 } 25 26 bool ok(int mid) { 27 for (int i = 1; i <= n; i++) tmp[i].clear(); 28 memset(vis, 0, sizeof(vis)); 29 30 for (int i = 1; i <= n; i++) 31 for (auto j : v[i]) 32 if (j.se > mid) 33 tmp[i].pb(j.fi); 34 for (int i = 1; i <= n; i++) 35 if (!vis[i] && !dfs(i, 1)) 36 return false; 37 return true; 38 } 39 40 int main() { 41 cin >> n >> m; 42 for (int i = 0; i < m; i++) { 43 int a, b, c; 44 cin >> a >> b >> c; 45 v[a].pb(mp(b, c)); 46 v[b].pb(mp(a, c)); 47 maxx = max(maxx, c); 48 } 49 for (l = 0, r = maxx; l <= r;) { 50 mid = (l+r) >> 1; 51 if (ok(mid)) r = mid-1; 52 else l = mid+1; 53 } 54 printf("%d", l); 55 return 0; 56 }
CF600B,水死。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int a, b, x; 5 vector<int> v; 6 7 int main() { 8 cin >> a >> b; 9 for (int i = 0; i < a; i++) { 10 cin >> x; 11 v.push_back(x); 12 } 13 sort(v.begin(), v.end()); 14 for (int i = 0; i < b; i++) { 15 cin >> x; 16 cout << upper_bound(v.begin(), v.end(), x) - v.begin() << " "; 17 } 18 return 0; 19 }
CF600D,垃圾题目卡我精度毁我青春。很想吐槽学校训练时的rank为什么过二分图的人比过这题的人少?全世界都会计几系列。
1 #include <bits/stdc++.h> 2 #define db long double 3 using namespace std; 4 5 const db PI = 3.141592653589793; 6 const db eps = 1e-10; 7 db da, S1, S2; 8 9 struct Point { 10 db x, y; 11 Point(){} 12 Point(db x, db y):x(x), y(y){} 13 }; 14 struct Circle { 15 Point c; 16 db r; 17 Circle(){} 18 Circle(Point c, db r):c(c), r(r){} 19 Point point(db a) { 20 return Point(c.x + cos(a)*r, c.y + sin(a)*r); 21 } 22 }; 23 typedef Point Vector; 24 int dcmp(db x) { 25 if (x <= eps && x >= -eps) return 0; 26 return x > 0 ? 1 : -1; 27 } 28 bool operator == (Vector A, Vector B) { 29 return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0; 30 } 31 Vector operator + (Vector A, Vector B) { 32 return Vector(A.x + B.x, A.y + B.y); 33 } 34 Vector operator - (Vector A, Vector B) { 35 return Vector(A.x - B.x, A.y - B.y); 36 } 37 Vector operator * (Vector A, db p) { 38 return Vector(A.x * p, A.y * p); 39 } 40 Vector operator / (Vector A, db p) { 41 return Vector(A.x / p, A.y / p); 42 } 43 db Cross(Vector A, Vector B) { 44 return A.x * B.y - A.y * B.x; 45 } 46 db Dot(Vector A, Vector B) { 47 return A.x * B.x + A.y * B.y; 48 } 49 db Length(Vector A) { 50 return sqrt(Dot(A, A)); 51 } 52 db angle(Vector A) { 53 return atan2(A.y, A.x); 54 } 55 56 db cal(Circle C, db da) { 57 if (da > PI/2) da = PI - da; 58 return C.r*C.r*da - C.r*C.r*sinl(da)*cosl(da); 59 } 60 61 int Get_Circle_Intersection(Circle C1, Circle C2, vector<Point> &v) { 62 db d = Length(C1.c - C2.c); 63 64 if (dcmp(d) == 0) return 0; 65 if (dcmp(C1.r + C2.r - d) < 0) return -1; 66 if (dcmp(fabs(C1.r-C2.r) - d) > 0) return 0; 67 68 db a = angle(C2.c - C1.c); 69 da = acosl((C1.r*C1.r + d*d - C2.r*C2.r) / (2*C1.r*d)); 70 Point p1 = C1.point(a-da), p2 = C1.point(a+da); 71 S1 = cal(C1, da); 72 73 a = angle(C1.c - C2.c); 74 da = acosl((C2.r*C2.r + d*d - C1.r*C1.r) / (2*C2.r*d)); 75 S2 = cal(C2, da); 76 77 v.push_back(p1); 78 if (p1 == p2) return 1; 79 v.push_back(p2); 80 return 2; 81 } 82 83 bool differe(Vector A, Vector B, Vector C) { 84 return dcmp(Cross(A, B)) * dcmp(Cross(A, C)) <= 0; 85 } 86 87 Circle C1, C2; 88 vector<Point> v; 89 db ans; 90 91 int main() { 92 cin >> C1.c.x >> C1.c.y >> C1.r; 93 cin >> C2.c.x >> C2.c.y >> C2.r; 94 int k = Get_Circle_Intersection(C1, C2, v); 95 if (k == -1) { 96 ans = 0.0; 97 } else if (k == 0) { 98 db r = min(C1.r, C2.r); 99 ans = r*r*PI; 100 } else if (k == 1) { 101 if (Dot(v[0]-C1.c, v[0]-C2.c) > 0) { 102 db r = min(C1.r, C2.r); 103 ans = r*r*PI; 104 } else ans = 0.0; 105 } else { 106 if (differe(v[1]-v[0], C1.c-v[0], C2.c-v[0])) 107 ans = S1 + S2; 108 else if (C1.r < C2.r) 109 ans = C1.r*C1.r*PI - S1 + S2; 110 else ans = C2.r*C2.r*PI - S2 + S1; 111 } 112 cout << fixed << setprecision(7) << ans << endl; 113 return 0; 114 }
标签:poj priority section names 思维 cin div lag pair
原文地址:https://www.cnblogs.com/AlphaWA/p/10353358.html