题解:
第一题:水题;
#include<bits/stdc++.h> using namespace std; const int M = 2e5 + 10; int n, tot, k, r; bool tag[M]; int vis[M], a[M], b[M]; bool check(int p){ memset(vis, 0, sizeof(vis)); memset(tag, 0, sizeof(tag)); tot = 0; for(int i = 1; i <= p; i++) { vis[a[i]]++; if(!tag[a[i]] && vis[a[i]] >= b[a[i]]) tag[a[i]] = 1, tot++; } if(tot == r) return 1; for(int i = p + 1; i <= n; i++){ if(vis[a[i-p]] == b[a[i-p]]) tag[a[i-p]] = 0, tot--; vis[a[i-p]]--; vis[a[i]]++; if(!tag[a[i]] && vis[a[i]] >= b[a[i]]) tag[a[i]] = 1, tot++; if(tot == r) return 1; } return 0; } int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*=f; } int main(){ freopen("drop.in","r",stdin); freopen("drop.out","w",stdout); int T; scanf("%d", &T); while(T--){ int x, y; memset(b, 0, sizeof(b)); scanf("%d%d%d", &n, &k, &r); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); for(int i = 1; i <= r; i++) { //scanf("%d%d", &x,&y); x=read();y=read(); b[x] = y; } for(int i = 0; i <= k; i++) if(!b[i]) b[i] = 2e9; int lf = 1, rg = n, ans = n + 1; while(lf <= rg){ int mid = (lf + rg) >> 1; if(check(mid)) ans = mid, rg = mid - 1; else lf = mid + 1; } if(ans == n + 1)puts("DESTROY ALL"); else printf("%d ", ans); } }
第二题:2—SAT;思维题;
#include<bits/stdc++.h> using namespace std; const int M = 4e3 + 10; int h[M], tot; bool vis[M]; void init(){ memset(h, 0, sizeof(h)); tot = 0; } struct edge{int v, nxt;}G[M<<1]; void add(int u, int v){ G[++tot].v=v,G[tot].nxt=h[u],h[u]=tot; } void dfs(int u){ vis[u] = 1; for(int i = h[u]; i; i = G[i].nxt) if(!vis[G[i].v]) dfs(G[i].v); } int main(){ freopen("god.in","r",stdin); freopen("god.out","w",stdout); int T; scanf("%d", &T); while(T--){ init(); int n, m, x, y; scanf("%d%d", &n, &m); for(int i=1;i<=m;i++){ scanf("%d%d",&x, &y); int xx = x > 0 ? x + n : -x; int yy = y > 0 ? y + n : -y; if(x < 0) x = -x + n; if(y < 0) y = -y + n; add(x, yy); add(y, xx); } int ans = 3; for(int i = 1; i <= n; i++){ memset(vis, 0, sizeof(vis)); int ret = 3; dfs(i); if(vis[i + n]){ memset(vis, 0, sizeof(vis)); dfs(i + n); if(vis[i]){ans=0;continue;} for(int j = 1; j <= n; j++) { if(vis[j]){ret=1; break;} } } else { memset(vis, 0, sizeof(vis)); dfs(i + n); if(vis[i])ret=1; else for(int j = 1; j <= n; j++) { if(vis[j]){ret=2; break;} } } ans = min(ans, ret); } if(ans == 3)puts("No Way"); else printf("%d ", ans); } }
第三题:线段树+贪心+一些思维
60分
100分
#include<bits/stdc++.h> using namespace std; const int M = 5e5 + 10; char a[M]; int n, ans[M], st[M]; struct Info{ int sum[2]; }; struct Qu{int r, id;}; vector <Qu> q[M]; struct Node { Node *ls, *rs; Info info; }pool[M << 2], *tail = pool, *root; Info up(Info lf, Info rg){ Info now; now.sum[0] = min(rg.sum[0], rg.sum[1] + lf.sum[0]); now.sum[1] = lf.sum[1] + rg.sum[1]; return now; } Node * build(int lf = 1, int rg = n){ Node *nd=++tail; if(lf == rg){ nd->info.sum[0]=nd->info.sum[1]=0; } else { int mid = (lf + rg) >> 1; nd->ls = build(lf, mid); nd->rs = build(mid + 1, rg); nd->info = up(nd->ls->info, nd->rs->info); } return nd; } #define Ls lf, mid, nd->ls #define Rs mid+1, rg, nd->rs Info query(int pos, int lf=1, int rg=n, Node *nd = root){ if(lf == rg) return nd->info; int mid = (lf + rg) >> 1; if(pos <= mid) return query(pos, Ls); Info now = query(pos, Rs); now = up(nd->ls->info, now); return now; } void modify(int pos, int v, int lf=1, int rg=n, Node *nd = root){ if(lf == rg)nd->info.sum[0] = nd->info.sum[1] = v; else { int mid = (lf + rg) >> 1; if(pos <= mid)modify(pos, v, Ls); else modify(pos, v, Rs); nd->info = up(nd->ls->info, nd->rs->info); } } int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*=f; } int main(){ freopen("sworder.in","r",stdin); freopen("sworder.out","w",stdout); int Q; scanf("%d", &n); scanf("%s", a+1); scanf("%d", &Q); root=build(); int tp = n + 1, l, r; for(int i = 1; i <= Q; i++){ l = read(), r = read(); q[l].push_back((Qu){r, i}); } for(int i = n; i; i--){ if(a[i] == 'C'){ st[--tp] = i; } else { if(tp != n+1){ modify(st[tp], -1); st[tp] = 0; tp++; } modify(i, 1); } for(int j=0;j<q[i].size();j++){ int pos = upper_bound(st+tp, st+n+1, q[i][j].r) - st; Info ret = query(q[i][j].r); ans[q[i][j].id] = (pos - tp) - min(0, ret.sum[0]); //printf("%d %d %d %d %d %d ", i, q[i][j].r, pos, tp, ret.sum[0]); } //printf("%d ",tp); } for(int i = 1; i <= Q; i++)printf("%d ",ans[i]); }