Codeforces #614 div.2 (A-E)
A ConneR and the A.R.C. Markland-N
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inc(i, l, r) for (int i = l; i <= r; i++)
5
6 int t, n, s, k, x;
7
8 int main() {
9 cin >> t;
10 while (t--) {
11 cin >> n >> s >> k;
12 map<int, int> m;
13 inc(i, 1, k) {
14 cin >> x;
15 m[x] = 1;
16 }
17 inc(del, 0, k) {
18 if ((s + del <= n && m[s + del] == 0) ||
19 (s - del >= 1 && m[s - del] == 0)) {
20 cout << del << "\n";
21 break;
22 }
23 }
24 }
25 }
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inc(i, l, r) for (int i = l; i <= r; i++)
5
6 int n;
7 double r;
8
9 int main() {
10 cin >> n;
11 for (int i = n; i >= 1; i--) r += 1.0 / i;
12 printf("%.12f", r);
13 }
每个岩浆点的对面或斜对面如果也有岩浆点,那么这两点之间就会形成障碍,统计这种障碍的数量
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inc(i, l, r) for (int i = l; i <= r; i++)
5
6 const int maxn = 1e6 + 5;
7
8 int a[3][maxn];
9 int n, q, k, x, y;
10
11 int main() {
12 cin >> n >> q;
13 inc(i, 0, q - 1) {
14 cin >> x >> y;
15 x--;
16 if (a[x][y] == 0) {
17 a[x][y] = 1;
18 inc(del, -1, 1) if (a[1 - x][y + del] == 1) k++;
19 } else {
20 a[x][y] = 0;
21 inc(del, -1, 1) if (a[1 - x][y + del] == 1) k--;
22 }
23 if(k) puts("NO");
24 else puts("YES");
25 }
26 }
分析点集的特点,可以知道每一个数据点都在下标比它小的数据点的右上方,而且离下一个数据点的距离 大于 离第0个数据点的距离。
枚举Aroma从哪个点出发;先往第0个数据点走,然后尽可能往右上走
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inc(i, l, r) for (int i = l; i <= r; i++)
5
6 const int maxn = 1e6 + 5;
7
8 ll a0, b0, ax, bx, ay, by;
9 ll xs, ys, t;
10
11 ll dx[maxn], dy[maxn];
12 ll dis[maxn];
13
14 int res;
15
16 int main() {
17 cin >> a0 >> b0 >> ax >> ay >> bx >> by;
18 cin >> xs >> ys >> t;
19 ll tx = a0, ty = b0;
20 int top;
21 for (top = 0; tx <= 2e16 && ty <= 2e16; top++) {
22 dx[top] = tx, dy[top] = ty;
23 tx = tx * ax + bx, ty = ty * ay + by;
24 }
25 inc(i, 1, top - 1) dis[i] = dx[i] - dx[0] + dy[i] - dy[0];
26 inc(i, 0, top - 1) {
27 ll tmp = t - abs(xs - dx[i]) - abs(ys - dy[i]);
28 if (tmp < 0) continue;
29 int tot = 0;
30 if (tmp <= dis[i]) {
31 tot = i - (lower_bound(dis, dis + top, dis[i] - tmp) - dis) + 1;
32 } else {
33 tot = upper_bound(dis + i + 1, dis + top, tmp - dis[i]) - dis;
34 }
35 res = max(res, tot);
36 }
37 cout << res;
38 }
最大S的方案必然是0-L都在一条链上,这条链上的数字是“山谷”型
推出状态转移方程
dp[u][v] = sub[u][v] * sub[v][u] + max(solve(par[v][u], v), solve(par[u][v], u));
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define inc(i, l, r) for (int i = l; i <= r; i++)
5 #define pb push_back
6
7 const int maxn = 3e3 + 5;
8 vector<int> edge[maxn];
9 int n, u, v;
10 int sub[maxn][maxn], par[maxn][maxn];
11 ll dp[maxn][maxn], res;
12
13 int root;
14
15 int dfs(int x, int p) {
16 sub[root][x] = 1;
17 for (int i = 0; i < edge[x].size(); i++) {
18 if (edge[x][i] != p) {
19 par[root][edge[x][i]] = x;
20 sub[root][x] += dfs(edge[x][i], x);
21 }
22 }
23 return sub[root][x];
24 }
25
26 ll solve(int u, int v) {
27 if (u == v) return 0;
28 if (dp[u][v]) return dp[u][v];
29 return dp[u][v] = sub[u][v] * sub[v][u] +
30 max(solve(par[v][u], v), solve(par[u][v], u));
31 }
32
33 int main() {
34 cin >> n;
35 inc(i, 1, n - 1) {
36 cin >> u >> v;
37 edge[u].pb(v);
38 edge[v].pb(u);
39 }
40 inc(i, 1, n) {
41 root = i;
42 dfs(root, -1);
43 }
44 inc(i, 1, n) inc(j, 1, n) {
45 res = max(res, solve(i, j));
46 }
47 printf("%lld\n", res);
48 }