Hướng dẫn giải của Đảo giấu vàng
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của happyboy99x
#include<algorithm> #include<iostream> using namespace std; const int X = 1e5, ABS = 3e4, LOG_X = 16, ST_SIZE = 1 << (LOG_X + 2), N = 15000; int tree[ST_SIZE], common[ST_SIZE]; pair<int, int> gold[N]; int n, s, w; void enter() { cin >> s >> w >> n; for(int i = 0; i < n; ++i) { cin >> gold[i].first >> gold[i].second; gold[i].second += ABS; } } void update(int k, int l, int r, int x, int y, int delta) { if(r <= x || r <= l || y <= l) return; if(x <= l && r <= y) tree[k] += delta, common[k] += delta; else { update(2 * k + 1, l, (l + r) / 2, x, y, delta); update(2 * k + 2, (l + r) / 2, r, x, y, delta); tree[k] = max(tree[2 * k + 1], tree[2 * k + 2]) + common[k]; } } void solve() { sort(gold, gold + n); int res = 0; for(int l = 0, r = 0; r < n; ++r) { while(gold[l].first + s < gold[r].first) update(0, 0, X, gold[l].second, gold[l].second + w + 1, -1), ++l; update(0, 0, X, gold[r].second, gold[r].second + w + 1, 1); res = max(res, tree[0]); } cout << res << '\n'; } int main() { ios::sync_with_stdio(false); enter(); solve(); return 0; }
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; const int SIZE = 60006; const int OFFSET = 30001; struct Node { int l, r; int lazy, maxV; Node *left, *right; Node(int l, int r): l(l), r(r), lazy(0), maxV(0), left(NULL), right(NULL) {} void pushDown() { if (lazy) { maxV += lazy; if (l < r) { left->lazy += lazy; right->lazy += lazy; } lazy = 0; } } void gather() { maxV = max(left->maxV, right->maxV); } void update(int i, int j, int v) { pushDown(); if (r < i || j < l) return; if (i <= l && r <= j) { lazy += v; pushDown(); return; } left->update(i, j, v); right->update(i, j, v); gather(); } }; Node* build(int l, int r) { Node *x = new Node(l, r); if (l == r) return x; x->left = build(l, l + r >> 1); x->right = build((l + r >> 1) + 1, r); return x; } Node *root; vector<int> Y[SIZE]; int n, S, W; void update(int x, int v) { for (int i = 0; i < Y[x].size(); ++i) { int y = Y[x][i]; root->update(y, y + W, v); } } int main() { ios::sync_with_stdio(false); cin >> S >> W; cin >> n; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; x += OFFSET; y += OFFSET; Y[x].push_back(y); } root = build(0, SIZE - 1); int ans = 0; for (int i = 0; i < SIZE; ++i) { update(i, 1); ans = max(ans, root->maxV); if (i >= S) update(i - S, -1); } cout << ans << endl; return 0; }
Code mẫu của RR
//Wishing myself a happy lunar new year with a lot of accept solutions {$R+,Q+} PROGRAM GOLD; CONST FINP=''; FOUT=''; maxn=15000; TYPE point=record x,y:longint; d:shortint; end; VAR kq,sl,n,s,w:longint; phu,max:array[1..maxn*3] of longint; a:array[1..maxn*2] of point; b:array[1..maxn] of longint; procedure readInput; var f:text; i,u,v:longint; begin assign(f,FINP); reset(f); readln(f,s,w); readln(f,n); for i:=1 to n do begin readln(f,u,v); with a[2*i-1] do begin x:=u; y:=v; d:=1; end; with a[2*i] do begin x:=u; y:=v+w; d:=-1;end; end; end; procedure initB; procedure sort(l,r:longint); var i,j:longint; mid,temp:longint; begin i:=l; j:=r; mid:=b[(l+r) div 2]; repeat while b[i]<mid do inc(i); while b[j]>mid do dec(j); if i<=j then begin temp:=b[i]; b[i]:=b[j]; b[j]:=temp; inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; var i,j:longint; begin for i:=1 to n do b[i]:=a[2*i].x; sort(1,n); j:=1; for i:=2 to n do if b[i]>b[j] then begin inc(j); b[j]:=b[i]; end; sl:=j; end; function nhohon(a,b:point):boolean; begin if a.y<b.y then nhohon:=true else if (a.y=b.y) and (a.d>b.d) then nhohon:=true else nhohon:=false; end; procedure sort(l,r:longint); var i,j:longint; temp,mid:point; begin i:=l; j:=r; mid:=a[(l+r) div 2]; repeat while nhohon(a[i],mid) do inc(i); while nhohon(mid,a[j]) do dec(j); if i<=j then begin temp:=a[i]; a[i]:=a[j]; a[j]:=temp; inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; procedure writeOutput; var f:text; begin assign(f,FOUT); rewrite(f); writeln(f,kq); close(f); end; function max2(a,b:longint):longint; begin if a>b then max2:=a else max2:=b; end; procedure update(i,s,t,left,right,d:longint); var mid:longint; begin if (b[s]>right) or (b[t]<left) then exit; if (left<=b[s]) and (b[t]<=right) then begin phu[i]:=phu[i]+d; max[i]:=max[i]+d; exit; end; mid:=(s+t) div 2; update(i shl 1,s,mid,left,right,d); update(i shl 1+1,mid+1,t,left,right,d); max[i]:=max2(max[i shl 1],max[i shl 1+1])+phu[i]; end; procedure process; var i:longint; begin kq:=0; for i:=1 to 2*n do begin update(1,1,sl,a[i].x,a[i].x+s,a[i].d); if max[1]>kq then kq:=max[1]; end; end; BEGIN readInput; initB; sort(1,2*n); process; writeOutput; END.
Bình luận