A:模擬輾轉相除法時記錄答案
B:3種情況:能減少2,能減少1,不能減少分別考慮清楚
C:利用一個set和一個multiset,把行列分開考慮,利用set自帶的排序和查詢,每次把相應的塊拿出來分成兩塊插入回去,然後行列分別取最大相乘的作為這次詢問的答案
D:一個區間覆蓋問題的變形,注意公式的話,很容易發現其實x,w對應的就是一個[x - w, x + w]的區間,然後求最多不重合區間即可
代碼:
#include#include #include using namespace std; typedef long long ll; ll a, b, ans; ll gcd(ll a, ll b) { if (!b) return a; ans += a / b; return gcd(b, a % b); } int main() { scanf("%lld%lld", &a, &b); gcd(a, b); printf("%lld\n", ans); return 0; }
#include#include #include using namespace std; const int N = 200005; int n; char a[N], b[N]; int g[30][30]; int vis[30]; int main() { scanf("%d%s%s", &n, a + 1, b + 1); int sum = 0; for (int i = 1; i <= n; i++) if (a[i] != b[i]) sum++; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { if (g[b[i] - 'a'][a[i] - 'a']) { printf("%d\n%d %d\n", sum - 2, i, g[b[i] - 'a'][a[i] - 'a']); return 0; } g[a[i] - 'a'][b[i] - 'a'] = i; } } for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { vis[b[i] - 'a'] = i; } } for (int i = 1; i <= n; i++) { if (a[i] != b[i] && vis[a[i] - 'a']) { printf("%d\n%d %d\n", sum - 1, i, vis[a[i] - 'a']); return 0; } } printf("%d\n-1 -1\n", sum); return 0; }
#include#include #include #include using namespace std; int w, h, n; set x[2]; multiset xx[2]; char op[2]; int v; set ::iterator it, l, r; multiset ::iterator tmp; long long gao(int tp) { x[tp].insert(v); it = x[tp].find(v); l = it; l--; r = it; r++; xx[tp].erase(xx[tp].find(*r - *l)); xx[tp].insert(v - *l); xx[tp].insert(*r - v); long long ans = 1; tmp = xx[tp].end(); tmp--; ans *= *tmp; tmp = xx[!tp].end(); tmp--; ans *= *tmp; return ans; } int main() { scanf("%d%d%d", &w, &h, &n); x[0].insert(0); x[0].insert(w); x[1].insert(0); x[1].insert(h); xx[0].insert(w); xx[1].insert(h); while (n--) { scanf("%s%d", op, &v); printf("%lld\n", gao(op[0] == 'H')); } return 0; }
#include#include #include using namespace std; const int N = 200005; const int INF = 0x3f3f3f3f; struct Seg { int l, r; Seg() {} Seg(int l, int r) { this->l = l; this->r = r; } } seg[N]; int n, x, w; bool cmp(Seg a, Seg b) { return a.r < b.r; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &x, &w); seg[i] = Seg(x - w, x + w); } sort(seg, seg + n, cmp); int ans = 0; int r = -INF; for (int i = 0; i < n; i++) { if (seg[i].l >= r) { r = seg[i].r; ans++; } } printf("%d\n", ans); return 0; }