#include <iostream> #include <map> #include <math.h> using namespace std; int main() { double a, b; cin >> a >> b; int n; cin >> n; double MIN = 1e10; while (n--) { double x, y, v; cin >> x >> y >> v; double tmp = sqrt((a - x) * (a - x) + (b - y) * (b - y)) / v; if (tmp < MIN) MIN = tmp; } printf("%.10lf ",MIN); return 0; }
#include <iostream> #include <map> #include <math.h> #include <algorithm> #include <string> using namespace std; int x[100000]; int main() { int n, q; cin >> n; for (int i = 0; i < n; i++) cin >> x[i]; sort(x, x + n); cin >> q; while (q--) { int m; cin >> m; if (m < x[0]) { cout << 0 << endl; continue; } if (m >= x[n - 1]) { cout << n << endl; continue; } int l = 0, r = n - 1, mid; while (1) { mid = (l + r) >> 1; if (m < x[mid]) r = mid; else l = mid; if (l == r || l + 1 == r) break; } cout << l + 1 << endl; } //system("pause"); return 0; }
f[i][0]表示把前i个排好序且第i个不翻转的最小代价,f[i][1]表示把前i个排好序且第i个翻转的最小代价。
#include <iostream> #include <map> #include <math.h> #include <algorithm> #include <string> using namespace std; long long dp[110000][2]; bool cmp[110000][2][2]; long long c[110000]; string s[110000], r[110000]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i <= n; i++) { cin >> s[i]; r[i] = s[i]; reverse(r[i].begin(), r[i].end()); } for (int i = 2; i <= n; i++) { if (s[i - 1] <= s[i]) cmp[i][0][0] = true; else cmp[i][0][0] = false; if (r[i - 1] <= s[i]) cmp[i][0][1] = true; else cmp[i][0][1] = false; if (s[i - 1] <= r[i]) cmp[i][1][0] = true; else cmp[i][1][0] = false; if (r[i - 1] <= r[i]) cmp[i][1][1] = true; else cmp[i][1][1] = false; } dp[1][0] = 0; dp[1][1] = c[1]; for (int i = 2; i <= n; i++) { dp[i][0] = 1e15; if (cmp[i][0][0] && dp[i - 1][0] < dp[i][0]) dp[i][0] = dp[i - 1][0]; if (cmp[i][0][1] && dp[i - 1][1] < dp[i][0]) dp[i][0] = dp[i - 1][1]; dp[i][1] = 1e15; if (cmp[i][1][0] && dp[i - 1][0] + c[i] < dp[i][1]) dp[i][1] = dp[i - 1][0] + c[i]; if (cmp[i][1][1] && dp[i - 1][1] + c[i] < dp[i][1]) dp[i][1] = dp[i - 1][1] + c[i]; } long long ans; if (dp[n][0] < dp[n][1]) ans = dp[n][0]; else ans = dp[n][1]; if (ans == 1e15) cout << -1 << endl; else cout << ans << endl; //system("pause"); return 0; }
#include<bits/stdc++.h> #include<set> using namespace std; multiset<int> ms; int main() { int n; cin >> n; ms.clear(); ms.insert(0); while (n--) { char ch; int x; cin >> ch >> x; if (ch == '+') ms.insert(x); if (ch == '-') ms.erase(ms.find(x)); if (ch == '?') { int ans = 0; for (int i = 30; i >= 0; i--) { int tmp; if ((x & (1 << i)) == 0) { tmp = ans | (1 << i); auto it = ms.lower_bound(tmp); if (it == ms.end()) continue; if ((*it) - tmp >= (1 << i)) continue; if ((*it) & (1 << i)) ans |= (1 << i); } else { auto it = ms.lower_bound(ans); int debug = *it; if ((*it) & (1 << i)) ans |= (1 << i); } } cout << (ans ^ x) << endl; } } return 0; }
Vasiliy finally got to work, where there is a huge amount of tasks waiting for him. Vasiliy is given a matrix consisting of n rows and mcolumns and q tasks. Each task is to swap two submatrices of the given matrix.
For each task Vasiliy knows six integers ai, bi, ci, di, hi, wi, where ai is the index of the row where the top-left corner of the first rectangle is located, bi is the index of its column, ci is the index of the row of the top-left corner of the second rectangle, di is the index of its column, hi is the height of the rectangle and wi is its width.
It's guaranteed that two rectangles in one query do not overlap and do not touch, that is, no cell belongs to both rectangles, and no two cells belonging to different rectangles share a side. However, rectangles are allowed to share an angle.
Vasiliy wants to know how the matrix will look like after all tasks are performed.
The first line of the input contains three integers n, m and q (2 ≤ n, m ≤ 1000, 1 ≤ q ≤ 10 000) — the number of rows and columns in matrix, and the number of tasks Vasiliy has to perform.
Then follow n lines containing m integers vi, j (1 ≤ vi, j ≤ 109) each — initial values of the cells of the matrix.
Each of the following q lines contains six integers ai, bi, ci, di, hi, wi (1 ≤ ai, ci, hi ≤ n, 1 ≤ bi, di, wi ≤ m).
Print n lines containing m integers each — the resulting matrix.
4 4 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
1 1 3 3 2 2
3 1 1 3 2 2
4 4 3 3
4 4 3 3
2 2 1 1
2 2 1 1
4 2 1
1 1
1 1
2 2
2 2
1 1 4 1 1 2
2 2
1 1
2 2
1 1
显然不可能就这么傻不拉几的去模拟,仔细想一下就知道虽然每次交换的两个矩形里点的数量很多,但是单纯就这两个矩形内部而言其实根本没什么变化,这样就有了下手之处。把整个矩阵以一个链表的形式存储,对每个节点保存指向其右边的点和下边的点的指针。每次交换操作时只需修改小矩形边界上的一圈点的指针值就可以了。
#include <stdio.h> #include <stdlib.h> class node { public: int v; node * d; node * r; }; int N, M, Q; node G[1005][1005]; void show() { node * ptr = &G[0][0]; node * sp; for (int i = 1; i <= N; i++) { sp = ptr->d; for (int j = 1; j <= M; j++) { sp = sp->r; printf("%d ", sp->v); } printf(" "); ptr = ptr->d; } } node * find(int x, int y) { node * ptr = &G[0][0]; for (int i = 0; i < x; i++) { ptr = ptr->d; } for (int i = 0; i < y; i++) { ptr = ptr->r; } return ptr; } int main() { scanf("%d%d%d", &N, &M, &Q); for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) { scanf("%d", &G[i][j].v); } for (int i = 0; i <= N; i++) for (int j = 0; j <= M; j++) { G[i][j].d = &G[i + 1][j]; G[i][j].r = &G[i][j + 1]; } while (Q--) { int x1, y1, x2, y2, h, w; scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &h, &w); node * spA1 = find(x1, y1 - 1); node * spB1 = find(x2, y2 - 1); node * spA2 = find(x1 - 1, y1); node * spB2 = find(x2 - 1, y2); node * spA3 = find(x1 + h - 1, y1); node * spB3 = find(x2 + h - 1, y2); node * spA4 = find(x1, y1 + w - 1); node * spB4 = find(x2, y2 + w - 1); //(x1~x1+h-1,y1-1)<->(x2~x2+h-1,y2-1) r for (int i = 0; i < h; i++) { node * t = spA1->r; spA1->r = spB1->r; spB1->r = t; spA1 = spA1->d; spB1 = spB1->d; } //(x1-1,y1~y1+w-1)<->(x2-1,y2~y2+w-1) d for (int i = 0; i < w; i++) { node * t = spA2->d; spA2->d = spB2->d; spB2->d = t; spA2 = spA2->r; spB2 = spB2->r; } //(x1+h-1,y1~y1+w-1)<->(x2+h-1,y2~y2+w-1) d for (int i = 0; i < w; i++) { node * t = spA3->d; spA3->d = spB3->d; spB3->d = t; spA3 = spA3->r; spB3 = spB3->r; } //(x1~x1+h-1,y1+w-1)<->(x2~x2+h-1,y2+w-1) r for (int i = 0; i < h; i++) { node * t = spA4->r; spA4->r = spB4->r; spB4->r = t; spA4 = spA4->d; spB4 = spB4->d; } } show(); return 0; }