标签:
水题,但是pow的精度不高,被hack掉了。用long long能存的下
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; int b1[44], b2[44]; int n, m; ll _pow(int m, int x) { ll ret = 1; for (int i=1; i<=x; ++i) { ret *= m; } return ret; } int main(void) { cout << (ll) pow (39, 9) << endl; cout << _pow (39, 9) << endl; scanf ("%d%d", &n, &m); ll ans1 = 0, ans2 = 0; for (int i=1; i<=n; ++i) { scanf ("%d", &b1[i]); } for (int i=n; i>=1; --i) { ans1 += _pow (m, n-i) * b1[i]; } scanf ("%d%d", &n, &m); for (int i=1; i<=n; ++i) { scanf ("%d", &b2[i]); } for (int i=n; i>=1; --i) { ans2 += _pow (m, n-i) * b2[i]; } if (ans1 < ans2) puts ("<"); else if (ans1 > ans2) puts (">"); else puts ("="); return 0; } /* 9 39 10 20 16 36 30 29 28 9 8 9 38 12 36 10 22 6 3 19 12 34 */
尺取法 B - Approximating a Constant Range
简单说就是维护[i, j]区间的最大值和最小值以及它们的最后的位置。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; int a[N]; int main(void) { int n; scanf ("%d", &n); for (int i=1; i<=n; ++i) { scanf ("%d", &a[i]); } int ans = 1, mn = a[1], mx = a[1], i = 1, j = 2; int p1 = 1, p2 = 1; while (j <= n) { if (a[j] <= mn) { mn = a[j]; p1 = j; } else if (a[j] >= mx) { mx = a[j]; p2 = j; } if (mx - mn <= 1) { ans = max (ans, j - i + 1); } else { while (mx - mn > 1) { if (p1 < p2 && p1 + 1 <= j) { mn = a[p1+1]; i = p1 + 1; p1++; } else if (p1 >= p2 && p2 + 1 <= j) { mx = a[p2+1]; i = p2 + 1; p2++; } else break; } ans = max (ans, j - i + 1); } j++; } printf ("%d\n", ans); return 0; }
两点之间要不是地铁要不就是汽车,那么1到n也一样,只要一次BFS就行了,水水的。
我刚做了一道双向BFS,想套一个试试,写麻烦了,但是还是A掉了,想想还是有问题,vis数组有问题,数据水了。。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 4e2 + 10; const int M = N * N; const int INF = 0x3f3f3f3f; struct Edge { int v, w, nex; Edge() {} Edge(int v, int w, int nex) : v (v), w (w), nex (nex) {} }edge[M]; int head[N]; bool lk[N][N]; bool vis[N][N][2]; bool vis2[N][2]; bool ok[2]; int n, m, e; queue<int> que[2]; void init(void) { memset (head, -1, sizeof (head)); e = 0; } void add_edge(int u, int v, int w) { edge[e].v = v; edge[e].w = w; edge[e].nex = head[u]; head[u] = e++; } bool BFS(int typ, int tim) { int sz = que[typ].size (); while (sz--) { int u = que[typ].front (); que[typ].pop (); for (int i=head[u]; ~i; i=edge[i].nex) { int v = edge[i].v, w = edge[i].w; if (w != typ) continue; if (v == n) { ok[typ] = true; if (ok[typ^1]) return true; } if (vis2[v][typ]) continue; vis2[v][typ] = true; if (vis[v][tim][typ^1]) continue; vis[v][tim][typ] = true; que[typ].push (v); } } return false; } int run(void) { que[0].push (1); que[1].push (1); int step = 0; while (!que[0].empty () || !que[1].empty ()) { step++; if (step > 800) break; if (!ok[0]) { if (BFS (0, step)) return step; } if (!ok[1]) { if (BFS (1, step)) return step; } } return -1; } int main(void) { init (); scanf ("%d%d", &n, &m); for (int u, v, i=1; i<=m; ++i) { scanf ("%d%d", &u, &v); add_edge (u, v, 1); add_edge (v, u, 1); lk[u][v] = lk[v][u] = true; } int cnt = 0; for (int i=1; i<=n; ++i) { for (int j=i+1; j<=n; ++j) { if (i == j || lk[i][j]) continue; cnt++; add_edge (i, j, 0); add_edge (j, i, 0); } } if (cnt == 0) { puts ("-1"); return 0; } printf ("%d\n", run ()); return 0; }
Codeforces Round #333 (Div. 2)
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4994189.html