[关闭]
@jason-fan 2021-05-01T03:14:19.000000Z 字数 9039 阅读 213

省选联考 2021 A卷代码

CODE


D1T1 card

  1. #include <bits/stdc++.h>
  2. template<typename _Tp>
  3. inline void read(_Tp &x) {
  4. x=0;bool fg=0;char ch=getchar();
  5. while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
  6. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
  7. if(fg) x=-x;
  8. }
  9. template<typename _Tp,typename... Args>void read(_Tp &t,Args &... args){read(t);read(args...);}
  10. using namespace std;
  11. typedef long long ll;
  12. const int N = 1000010;
  13. int n,m,a[N],b[N];
  14. struct pp {
  15. int ps,vl,fg;
  16. bool operator<(const pp &a) const {
  17. return vl<a.vl;
  18. }
  19. }p[N*2];
  20. int sta[N*2];
  21. bool check(int gap) {
  22. for(int i=1;i<=n;++i) sta[i]=0;
  23. int used=0,cnt=0;
  24. int l,r=0;
  25. for(l=1;l<=2*n;++l) {
  26. // erase l-1
  27. if(l>1) {
  28. pp t=p[l-1];
  29. sta[t.ps]-=t.fg;
  30. if(sta[t.ps]==0) --cnt;
  31. if(t.fg==1) {
  32. if(sta[t.ps]) ++used;
  33. }else {
  34. if(sta[t.ps]==0) --used;
  35. }
  36. }
  37. while(r<2*n&&p[r+1].vl-p[l].vl<=gap) {
  38. ++r;
  39. // insert r
  40. pp t=p[r];
  41. if(sta[t.ps]==0) ++cnt;
  42. if(t.fg==1) {
  43. if(sta[t.ps]) --used;
  44. }else {
  45. if(sta[t.ps]==0) ++used;
  46. }
  47. sta[t.ps]+=t.fg;
  48. }
  49. if(cnt==n&&used<=m) return true;
  50. }
  51. return false;
  52. }
  53. int main() {
  54. read(n);read(m);
  55. for(int i=1;i<=n;++i) read(a[i]);
  56. for(int i=1;i<=n;++i) read(b[i]);
  57. for(int i=1;i<=n;++i) {
  58. p[i].ps=i;p[i].vl=a[i];p[i].fg=1;
  59. p[i+n].ps=i;p[i+n].vl=b[i];p[i+n].fg=2;
  60. }
  61. sort(p+1,p+2*n+1);
  62. int L=1,R=1e9,M,B;
  63. while(L<=R) {
  64. M=(L+R)>>1;
  65. if(check(M)) B=M,R=M-1;
  66. else L=M+1;
  67. }
  68. cout<<B<<endl;
  69. return 0;
  70. }

D1T2 matrix

  1. #include <bits/stdc++.h>
  2. #define re register
  3. template<typename _Tp>
  4. inline void read(_Tp &x) {
  5. x=0;bool fg=0;char ch=getchar();
  6. while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
  7. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
  8. if(fg) x=-x;
  9. }
  10. using namespace std;
  11. typedef long long ll;
  12. const int N = 305;
  13. const int Max = 1000000;
  14. ll a[N][N],b[N][N];
  15. int n,m,T;
  16. ll c[N][N];
  17. namespace QAQ { // 差分约束
  18. const int N = 610;
  19. const int M = 370010;
  20. ll w[N][N];
  21. ll dis[N];
  22. bool vis[N];
  23. int ct[N];
  24. // Xv-Xu<=w
  25. void adde(int u,int v,int _w) {
  26. w[u][v]=_w;
  27. }
  28. void init() { memset(w,0x3f,sizeof(w));}
  29. bool BF() { // Bellman-Ford
  30. memset(dis,0,sizeof(dis));
  31. for(int ts=0;ts<=n+m;++ts) {
  32. bool fg=0;
  33. for(int u=1;u<=n+m;++u) {
  34. for(int v=(u<=n)?n+1:1,ed=(u<=n)?n+m:n;v<=ed;++v) {
  35. if(dis[v]>dis[u]+w[u][v]) {
  36. dis[v]=dis[u]+w[u][v];
  37. fg=1;
  38. }
  39. }
  40. }
  41. if(!fg) return true;
  42. }
  43. return false;
  44. }
  45. }
  46. namespace checker {
  47. void check() {
  48. for(int i=1;i<=n;++i) {
  49. for(int j=1;j<=n;++j) {
  50. if(a[i][j]<0||a[i][j]>Max) {
  51. cerr<<"out of bound"<<endl;
  52. exit(-1);
  53. }
  54. }
  55. }
  56. for(int i=1;i<n;++i){
  57. for(int j=1;j<m;++j) {
  58. if(b[i][j]!=a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]) {
  59. cerr<<"don't match"<<endl;
  60. exit(-2);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. void solve() {
  67. for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) a[i][j]=0;
  68. #define idR(x) (x)
  69. #define idC(x) (n+x)
  70. for(re int i=n-1;i>=1;--i) {
  71. for(re int j=m-1;j>=1;--j) {
  72. a[i][j]=b[i][j]-a[i+1][j]-a[i][j+1]-a[i+1][j+1];
  73. }
  74. }
  75. QAQ::init();
  76. for(re int i=1;i<=n;++i) {
  77. for(re int j=1;j<=m;++j) {
  78. if((i+j)&1) {
  79. // 行+ 列-
  80. // 0 <= idR(i)-idC(j)+a[i][j] <= 1e6
  81. QAQ::adde(idC(j),idR(i),Max-a[i][j]);
  82. QAQ::adde(idR(i),idC(j),a[i][j]);
  83. }else {
  84. // 行- 列+
  85. // 0 <= -idR(i)+idC(j)+a[i][j] <= 1e6
  86. QAQ::adde(idR(i),idC(j),Max-a[i][j]);
  87. QAQ::adde(idC(j),idR(i),a[i][j]);
  88. }
  89. }
  90. }
  91. bool fg=QAQ::BF();
  92. if(!fg) {puts("NO");return ;}
  93. for(re int i=1;i<=n;++i) for(re int j=1;j<=m;++j) {
  94. if((i+j)&1) a[i][j]=a[i][j]-QAQ::dis[idC(j)]+QAQ::dis[idR(i)];
  95. else a[i][j]=a[i][j]+QAQ::dis[idC(j)]-QAQ::dis[idR(i)];
  96. }
  97. puts("YES");
  98. // checker::check();
  99. for(int i=1;i<=n;++i) {
  100. for(int j=1;j<=m;++j) printf("%lld ",a[i][j]);
  101. printf("\n");
  102. }
  103. }
  104. int rmain() {
  105. read(n);read(m);
  106. for(int i=1;i<n;++i) for(int j=1;j<m;++j) read(b[i][j]);
  107. solve();
  108. return 0;
  109. }
  110. int main() {
  111. read(T);
  112. while(T--) rmain();
  113. return 0;
  114. }

D1T3 graph

  1. #include <bits/stdc++.h>
  2. template<typename _Tp>
  3. inline void read(_Tp &x) {
  4. x=0;bool fg=0;char ch=getchar();
  5. while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
  6. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
  7. if(fg) x=-x;
  8. }
  9. template<typename _Tp,typename... Args>void read(_Tp &t,Args &... args){read(t);read(args...);}
  10. using namespace std;
  11. typedef long long ll;
  12. const int N = 1010;
  13. const int M = 200010;
  14. struct edge {int u,v;}e[M];
  15. int n,m;
  16. vector<int> g[N],rg[N];
  17. int ans[M];
  18. bool vis[N],rvis[N];
  19. int cnt=0;
  20. void bfs(int s) {
  21. queue<int> q;
  22. q.push(s);vis[s]=1; if(rvis[s]) ++cnt;
  23. while(!q.empty()) {
  24. int u=q.front();q.pop();
  25. for(int i=0;i<g[u].size();++i) {
  26. int v=g[u][i];
  27. if(!vis[v]) {
  28. vis[v]=1;
  29. if(rvis[v]) ++cnt;
  30. q.push(v);
  31. }
  32. }
  33. }
  34. }
  35. void rbfs(int s) {
  36. queue<int> q;
  37. q.push(s);rvis[s]=1; if(vis[s]) ++cnt;
  38. while(!q.empty()) {
  39. int u=q.front();q.pop();
  40. for(int i=0;i<rg[u].size();++i) {
  41. int v=rg[u][i];
  42. if(!rvis[v]) {
  43. rvis[v]=1;
  44. if(vis[v]) ++cnt;
  45. q.push(v);
  46. }
  47. }
  48. }
  49. }
  50. int main() {
  51. read(n);read(m);
  52. for(int i=1;i<=m;++i) {
  53. read(e[i].u);read(e[i].v);
  54. }
  55. for(int u=1;u<=n;++u) {
  56. memset(vis,0,sizeof(vis));
  57. memset(rvis,0,sizeof(rvis));
  58. for(int i=1;i<=n;++i) g[i].clear(),rg[i].clear();
  59. vis[u]=1;rvis[u]=1; ans[m+1]++;
  60. cnt=1;
  61. for(int i=m;i>=1;--i) {
  62. if(e[i].u>=u&&e[i].v>=u) {
  63. // if() {
  64. g[e[i].u].push_back(e[i].v);
  65. rg[e[i].v].push_back(e[i].u);
  66. // }
  67. if(vis[e[i].u] && !vis[e[i].v]) bfs(e[i].v);
  68. if(rvis[e[i].v] && !rvis[e[i].u]) rbfs(e[i].u);
  69. }
  70. ans[i]+=cnt;
  71. }
  72. }
  73. for(int i=1;i<=m+1;++i) {
  74. printf("%d ",ans[i]);
  75. }
  76. puts("");
  77. return 0;
  78. }

D2T1 gem

  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. template<typename _Tp>
  5. inline void read(_Tp &x) {
  6. x=0;bool fg=0;char ch=getchar();
  7. while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
  8. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
  9. if(fg) x=-x;
  10. }
  11. using namespace std;
  12. typedef long long ll;
  13. typedef vector<int> vt;
  14. const int N = 200010;
  15. int head[N],pnt[N<<1],nxt[N<<1],E;
  16. int n,m,q,c,P[N],op[N],w[N];
  17. void adde(int u,int v) {
  18. E++;pnt[E]=v;nxt[E]=head[u];head[u]=E;
  19. }
  20. namespace seg {
  21. #define ls(p) t[p].l
  22. #define rs(p) t[p].r
  23. #define mid ((l+r)>>1)
  24. struct node{
  25. int l,r,x;
  26. }t[N*23*2];
  27. int root[2][N],tot;
  28. int newnode() {
  29. ++tot;t[tot].l=t[tot].r=t[tot].x=0;return tot;
  30. }
  31. void modify(int p,int q,int ps,int vl,int l=1,int r=c) {
  32. if(l==r) {
  33. t[q].x=vl;
  34. return;
  35. }
  36. if(ps<=mid) {
  37. modify(ls(p),ls(q)=newnode(),ps,vl,l,mid);
  38. rs(q)=rs(p);
  39. }else {
  40. modify(rs(p),rs(q)=newnode(),ps,vl,mid+1,r);
  41. ls(q)=ls(p);
  42. }
  43. }
  44. int ask(int p,int ps,int l=1,int r=c) {
  45. if(ps<1||ps>c) return 0;
  46. if(!p) return 0;
  47. if(l==r) return t[p].x;
  48. if(ps<=mid) return ask(ls(p),ps,l,mid);
  49. return ask(rs(p),ps,mid+1,r);
  50. }
  51. #undef ls
  52. #undef rs
  53. #undef mid
  54. }using seg::root;
  55. namespace STD {
  56. const int M = 21;
  57. int fa[N],siz[N],son[N],dep[N],top[N];
  58. int dnxtp[M][N],unxtp[M][N];
  59. vt bots;
  60. void dfs1(int u,int f,int d) {
  61. siz[u]=1;fa[u]=f;dep[u]=d;
  62. for(int i=head[u];i;i=nxt[i]) {
  63. int v=pnt[i];
  64. if(v==f) continue;
  65. dfs1(v,u,d+1);
  66. siz[u]+=siz[v];
  67. if(siz[v]>siz[son[u]]) son[u]=v;
  68. }
  69. }
  70. void dfs2(int u,int topf) {
  71. top[u]=topf;
  72. if(son[u]) {
  73. dfs2(son[u],topf);
  74. }else bots.push_back(u);
  75. for(int i=head[u];i;i=nxt[i]) {
  76. int v=pnt[i];
  77. if(v==fa[u]||v==son[u]) continue;
  78. dfs2(v,v);
  79. }
  80. }
  81. void init() {
  82. dfs1(1,0,0);dfs2(1,1);
  83. for(int i=1;i<=n;++i) w[i]=op[w[i]];
  84. root[0][0]=seg::newnode();
  85. root[1][0]=seg::newnode();
  86. static int t[N];
  87. for(int _i=0;_i<(int)bots.size();++_i) {
  88. int cnt=0,u=bots[_i];
  89. while(true) {
  90. t[++cnt]=u;
  91. if(u==top[u]) break;
  92. u=fa[u];
  93. }
  94. t[0]=t[cnt+1]=0;
  95. map<int,int> ext;
  96. for(int i=1;i<=cnt;++i) {
  97. u=t[i];
  98. if(ext.find(w[u]+1)!=ext.end()) {
  99. dnxtp[0][u]=ext[w[u]+1];
  100. }
  101. seg::modify(root[0][t[i-1]],root[0][u]=seg::newnode(),w[u],u);
  102. ext[w[u]]=u;
  103. }
  104. for(int i=1;i<M;++i) {
  105. for(int j=1,u;j<=cnt;++j) {
  106. u=t[j];
  107. dnxtp[i][u]=dnxtp[i-1][dnxtp[i-1][u]];
  108. }
  109. }
  110. ext.clear();
  111. for(int i=cnt;i>=1;--i) {
  112. u=t[i];
  113. if(ext.find(w[u]+1)!=ext.end()) {
  114. unxtp[0][u]=ext[w[u]+1];
  115. }
  116. seg::modify(root[1][t[i+1]],root[1][u]=seg::newnode(),w[u],u);
  117. ext[w[u]]=u;
  118. }
  119. for(int i=1;i<M;++i) {
  120. for(int j=1,u;j<=cnt;++j) {
  121. u=t[j];
  122. unxtp[i][u]=unxtp[i-1][unxtp[i-1][u]];
  123. }
  124. }
  125. }
  126. }
  127. vector<pair<int,int> > up,dn;
  128. void LCA(int s,int t) {
  129. up.clear();dn.clear();
  130. int fs=top[s],ft=top[t];
  131. while(fs!=ft) {
  132. if(dep[fs]>dep[ft]) {
  133. up.push_back(make_pair(s,fs));
  134. s=fa[fs];fs=top[s];
  135. }else {
  136. dn.push_back(make_pair(t,ft));
  137. t=fa[ft];ft=top[t];
  138. }
  139. }
  140. if(dep[s]>dep[t]) {
  141. up.push_back(make_pair(s,t));
  142. }else {
  143. dn.push_back(make_pair(t,s));
  144. }
  145. }
  146. void query(int s,int t) {
  147. LCA(s,t);
  148. int x=0;
  149. for(int _i=0;_i<(int)up.size();++_i) {
  150. pair<int,int> cha=up[_i];
  151. int u=seg::ask(root[1][cha.fi],w[x]+1);
  152. if(u==0||dep[u]<dep[cha.se]) continue;
  153. x=u;
  154. for(int i=M-1;i>=0;--i) if(unxtp[i][x]&&dep[unxtp[i][x]]>=dep[cha.se]) {
  155. x=unxtp[i][x];
  156. }
  157. }
  158. for(int _i=dn.size()-1;_i>=0;--_i) {
  159. pair<int,int> cha=dn[_i];
  160. int u=seg::ask(root[0][cha.se],w[x]+1);
  161. if(u==0||dep[u]>dep[cha.fi]) continue;
  162. x=u;
  163. for(int i=M-1;i>=0;--i) if(dnxtp[i][x]&&dep[dnxtp[i][x]]<=dep[cha.fi]) {
  164. x=dnxtp[i][x];
  165. }
  166. }
  167. printf("%d\n",w[x]);
  168. }
  169. void solve() {
  170. init();
  171. for(int ts=1;ts<=q;++ts) {
  172. int s,t;read(s);read(t);
  173. query(s,t);
  174. }
  175. }
  176. }
  177. int main() {
  178. read(n);read(m);read(c);
  179. for(int i=1;i<=c;++i) read(P[i]),op[P[i]]=i;
  180. for(int i=1;i<=n;++i) read(w[i]);
  181. for(int i=1;i<n;++i) {
  182. int u,v;read(u);read(v);
  183. adde(u,v);adde(v,u);
  184. }
  185. read(q);
  186. STD::solve();
  187. return 0;
  188. }

D2T2 ranklist

  1. #include <bits/stdc++.h>
  2. template<typename _Tp>
  3. inline void read(_Tp &x) {
  4. x=0;bool fg=0;char ch=getchar();
  5. while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
  6. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
  7. if(fg) x=-x;
  8. }
  9. template<typename _Tp,typename... Args>void read(_Tp &t,Args &... args){read(t);read(args...);}
  10. using namespace std;
  11. typedef long long ll;
  12. const int N = 13;
  13. const int M = 505;
  14. const int K = 1<<13;
  15. int a[N],n,m,ful;
  16. int ct[K];
  17. ll dp[K][N][M];
  18. int main() {
  19. read(n);read(m);
  20. int bst=0;
  21. for(int i=0;i<n;++i) {
  22. read(a[i]);
  23. if(a[i]>a[bst]) bst=i;
  24. }
  25. ful=(1<<n)-1;
  26. for(int mask=1;mask<=ful;++mask) ct[mask]=ct[mask-(mask&-mask)]+1;
  27. for(int i=0;i<n;++i) {
  28. int b=a[bst]-a[i];
  29. if(i>bst) b++;
  30. if(n*b<=m) dp[1<<i][i][n*b]=1;
  31. }
  32. for(int S=1;S<ful;++S) {
  33. for(int _c=S;_c;_c-=_c&-_c) {
  34. int c=log2(_c&-_c);
  35. for(int r=0;r<=m;++r) if(dp[S][c][r]) {
  36. for(int _x=(S^ful);_x;_x-=_x&-_x) {
  37. int x=log2(_x&-_x);
  38. int dif=max(0,a[c]-a[x]);
  39. if(a[x]+dif==a[c]&&x>c) ++dif;
  40. if(r+dif*(n-ct[S])<=m)
  41. dp[S|(1<<x)][x][r+dif*(n-ct[S])]+=dp[S][c][r];
  42. }
  43. }
  44. }
  45. }
  46. ll ans=0;
  47. for(int c=0;c<n;++c) {
  48. for(int r=0;r<=m;++r) {
  49. ans+=dp[ful][c][r];
  50. }
  51. }
  52. printf("%lld\n",ans);
  53. return 0;
  54. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注