Codeforces Round #564 (Div. 1)

A Nauuo and Cards

首先如果牌库中最后的牌是\(1,2,\cdots, k\),那么就模拟一下能不能每次打出第\(k+i\)张牌。

然后考虑每一张牌打出后还要打多少张牌以及这张牌是什么时候入手的,分别记为\(f_i,g_i\),那么答案就是\(f_i+g_i\)的最大值。

  1. #include<bits/stdc++.h>
  2. #define qmin(x,y) (x=min(x,y))
  3. #define qmax(x,y) (x=max(x,y))
  4. #define pir pair<int,int>
  5. #define mp(x,y) make_pair(x,y)
  6. #define fr first
  7. #define sc second
  8. #define rsort(x,y) sort(x,y),reverse(x,y)
  9. #define vic vector<int>
  10. #define vit vic::iterator
  11. using namespace std;
  12. char gc() {
  13. // static char buf[100000],*p1,*p2;
  14. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  15. return getchar();
  16. }
  17. template<class T>
  18. int read(T &ans) {
  19. T f=1;ans=0;
  20. char ch=gc();
  21. while(!isdigit(ch)) {
  22. if(ch==EOF) return EOF;
  23. if(ch=='-') f=-1;
  24. ch=gc();
  25. }
  26. while(isdigit(ch))
  27. ans=ans*10+ch-'0',ch=gc();
  28. ans*=f;return 1;
  29. }
  30. template<class T1,class T2>
  31. int read(T1 &a,T2 &b) {
  32. return read(a)==EOF?EOF:read(b);
  33. }
  34. template<class T1,class T2,class T3>
  35. int read(T1 &a,T2 &b,T3 &c) {
  36. return read(a,b)==EOF?EOF:read(c);
  37. }
  38. typedef long long ll;
  39. const int Maxn=1100000;
  40. const int inf=0x3f3f3f3f;
  41. const ll mod=998244353;
  42. int n,a[Maxn],b[Maxn],c[Maxn],x,ans;
  43. bool work() {
  44. for(int i=1;i<=n;i++) c[i]=b[i];
  45. int now=1;
  46. for(int i=a[n]+1;i<=n;i++) {
  47. if(!c[i]) return false;
  48. c[a[now++]]++;
  49. }
  50. return true;
  51. }
  52. int main() {
  53. // freopen("test.in","r",stdin);
  54. read(n);
  55. for(int i=1;i<=n;i++) read(x),b[x]++;
  56. for(int i=1;i<=n;i++) read(a[i]);
  57. int sxz=0;
  58. for(int i=1;i<=n;i++) {
  59. if(a[i]==1) {
  60. int flag=0;
  61. for(int j=1;j<=n-i;j++) if(a[i+j]!=j+1) {
  62. flag=1;
  63. break;
  64. }
  65. if(!flag) sxz=i;
  66. break;
  67. }
  68. }
  69. if(sxz&&work()) return 0*printf("%d\n",n-a[n]);
  70. else {
  71. memset(c,0,sizeof(c));
  72. for(int i=1;i<=n;i++) if(a[i]) c[a[i]]=i;
  73. for(int i=1;i<=n;i++) qmax(ans,c[i]+n-i+1);
  74. printf("%d\n",ans);
  75. }
  76. return 0;
  77. }

B Nauuo and Circle

除了根以外,每一颗子树在圆上一定是一段弧,设这颗子树根节点有\(x\)个儿子,那么这颗子树的方案数为\(f_i=(x+1)!\prod_{j\in son(i)}f_j\)。因为除了要给\(x\)个儿子做排列,还要考虑到根节点插到哪个位置。

而根的区别在于根对应的是整个圆,那么就不需要考虑根要插到哪个位置,方案数即为\(f_i=x!\prod_{j\in son(i)}f_j\)。圆可以旋转,所以要再乘以一个\(n\)。

  1. #include<bits/stdc++.h>
  2. #define qmin(x,y) (x=min(x,y))
  3. #define qmax(x,y) (x=max(x,y))
  4. #define pir pair<int,int>
  5. #define mp(x,y) make_pair(x,y)
  6. #define fr first
  7. #define sc second
  8. #define rsort(x,y) sort(x,y),reverse(x,y)
  9. #define vic vector<int>
  10. #define vit vic::iterator
  11. using namespace std;
  12. char gc() {
  13. // static char buf[100000],*p1,*p2;
  14. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  15. return getchar();
  16. }
  17. template<class T>
  18. int read(T &ans) {
  19. T f=1;ans=0;
  20. char ch=gc();
  21. while(!isdigit(ch)) {
  22. if(ch==EOF) return EOF;
  23. if(ch=='-') f=-1;
  24. ch=gc();
  25. }
  26. while(isdigit(ch))
  27. ans=ans*10+ch-'0',ch=gc();
  28. ans*=f;return 1;
  29. }
  30. template<class T1,class T2>
  31. int read(T1 &a,T2 &b) {
  32. return read(a)==EOF?EOF:read(b);
  33. }
  34. template<class T1,class T2,class T3>
  35. int read(T1 &a,T2 &b,T3 &c) {
  36. return read(a,b)==EOF?EOF:read(c);
  37. }
  38. typedef long long ll;
  39. const int Maxn=1100000;
  40. const int inf=0x3f3f3f3f;
  41. const ll mod=998244353;
  42. int to[Maxn],nxt[Maxn],first[Maxn],tot=1,dp[Maxn],n,u,v;
  43. inline void add(int u,int v) {
  44. to[tot]=v;
  45. nxt[tot]=first[u];
  46. first[u]=tot++;
  47. to[tot]=u;
  48. nxt[tot]=first[v];
  49. first[v]=tot++;
  50. }
  51. void dfs(int root,int fa=0) {
  52. int x=1,y=1,temp=1;
  53. for(int i=first[root];i;i=nxt[i]) {
  54. if(to[i]!=fa) {
  55. dfs(to[i],root);
  56. x=1ll*x*temp%mod; temp++;
  57. y=1ll*y*dp[to[i]]%mod;
  58. }
  59. }
  60. x=1ll*x*temp%mod;
  61. dp[root]=1ll*x*y%mod;
  62. }
  63. int main() {
  64. // freopen("test.in","r",stdin);
  65. read(n);
  66. for(int i=1;i<n;i++) {
  67. read(u,v);
  68. add(u,v);
  69. }
  70. int x=1,y=1,temp=1;
  71. for(int i=first[1];i;i=nxt[i]) {
  72. dfs(to[i],1);
  73. x=1ll*x*temp%mod; temp++;
  74. y=1ll*y*dp[to[i]]%mod;
  75. }
  76. dp[1]=1ll*x*y%mod;
  77. printf("%I64d\n",1ll*n*dp[1]%mod);
  78. return 0;
  79. }

C Nauuo and Pictures

一开始我们猜想直接按照当前的期望值作为权重,但是这个是不对的,因为一个是加,而另一个是减,这两个会互相影响。

但是在所有要加的数里面,每个数期望占的比例是不变的,所以我们可以把所有加的合成一个,所有减的合成一个,然后就可以\(O(m^2)\)DP了,最后按照每个数初始所占的比例还原回去就好了。

  1. #include<bits/stdc++.h>
  2. #define qmin(x,y) (x=min(x,y))
  3. #define qmax(x,y) (x=max(x,y))
  4. #define pir pair<int,int>
  5. #define mp(x,y) make_pair(x,y)
  6. #define fr first
  7. #define sc second
  8. #define rsort(x,y) sort(x,y),reverse(x,y)
  9. #define vic vector<int>
  10. #define vit vic::iterator
  11. using namespace std;
  12. char gc() {
  13. // static char buf[100000],*p1,*p2;
  14. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  15. return getchar();
  16. }
  17. template<class T>
  18. int read(T &ans) {
  19. T f=1;ans=0;
  20. char ch=gc();
  21. while(!isdigit(ch)) {
  22. if(ch==EOF) return EOF;
  23. if(ch=='-') f=-1;
  24. ch=gc();
  25. }
  26. while(isdigit(ch))
  27. ans=ans*10+ch-'0',ch=gc();
  28. ans*=f;return 1;
  29. }
  30. template<class T1,class T2>
  31. int read(T1 &a,T2 &b) {
  32. return read(a)==EOF?EOF:read(b);
  33. }
  34. template<class T1,class T2,class T3>
  35. int read(T1 &a,T2 &b,T3 &c) {
  36. return read(a,b)==EOF?EOF:read(c);
  37. }
  38. typedef long long ll;
  39. #define int long long
  40. const int Maxn=1100000;
  41. const int inf=0x3f3f3f3f;
  42. const ll mod=998244353;
  43. int n,m,a[Maxn],b[Maxn],sxz,zhy,f[3100][3100],ansx,ansy;
  44. ll powp(ll a,ll b) {
  45. ll ans=1;
  46. while(b) {
  47. if(b&1) ans=ans*a%mod;
  48. a=a*a%mod;
  49. b>>=1;
  50. }
  51. return ans;
  52. }
  53. signed main() {
  54. // freopen("test.in","r",stdin);
  55. read(n,m);
  56. for(int i=1;i<=n;i++) read(a[i]);
  57. for(int i=1;i<=n;i++) read(b[i]);
  58. for(int i=1;i<=n;i++)
  59. if(a[i]) sxz=(sxz+b[i])%mod;
  60. else zhy=(zhy+b[i])%mod;
  61. f[0][0]=1;
  62. for(int i=1;i<=m;i++)
  63. for(int j=0;j<i;j++) {
  64. if(i-j-1==zhy) f[j+1][i-j-1]=(f[j+1][i-j-1]+f[j][i-j-1])%mod;
  65. else {
  66. int x=sxz+j,y=zhy-(i-j-1),tot=powp(x+y,mod-2);
  67. f[j+1][i-j-1]=(f[j+1][i-j-1]+1ll*f[j][i-j-1]*x%mod*tot%mod)%mod;
  68. f[j][i-j]=(f[j][i-j]+1ll*f[j][i-j-1]*y%mod*tot%mod)%mod;
  69. }
  70. }
  71. for(int i=0;i<=m;i++)
  72. ansx=(ansx+1ll*f[i][m-i]*i%mod)%mod,ansy=(ansy+1ll*f[i][m-i]*(m-i)%mod)%mod;
  73. int x=(sxz+ansx)%mod,y=(zhy-ansy+mod)%mod;
  74. sxz=powp(sxz,mod-2),zhy=powp(zhy,mod-2);
  75. for(int i=1;i<=n;i++) if(a[i]) printf("%I64d\n",sxz*b[i]%mod*x%mod);
  76. else printf("%I64d\n",zhy*b[i]%mod*y%mod);
  77. return 0;
  78. }

D Nauuo and Portals

考虑初始时向右走的人从上到下编号为\(a_i\),向下走的人从左到右编号为\(b_i\),而最终向右走到达第\(i\)行的终点的人的编号为\(r_i\),向下为\(c_i\)。

如果\(a_1==r_1\ and\ b_1==c_1\),那么直接把第一行和第一列去掉就好了。

那么找到\(a_x==r_1\ ,\ b_y==c_1\),那么就加上一个门:\(<(x,1),(1,y)>\),然后交换\(a_x,a_1\)和\(b_y,b_1\),也可以把第一行和第一列去掉。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<cctype>
  6. #define qmin(x,y) (x=min(x,y))
  7. #define qmax(x,y) (x=max(x,y))
  8. #define vic vector<int>
  9. #define vit vic::iterator
  10. #define pir pair<int,int>
  11. #define fr first
  12. #define sc second
  13. #define mp(x,y) make_pair(x,y)
  14. #define rsort(x,y) sort(x,y),reverse(x,y)
  15. using namespace std;
  16. inline char gc() {
  17. // static char buf[100000],*p1,*p2;
  18. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  19. return getchar();
  20. }
  21. template<class T>
  22. int read(T &ans) {
  23. ans=0;char ch=gc();T f=1;
  24. while(!isdigit(ch)) {
  25. if(ch==EOF) return -1;
  26. if(ch=='-') f=-1;
  27. ch=gc();
  28. }
  29. while(isdigit(ch))
  30. ans=ans*10+ch-'0',ch=gc();
  31. ans*=f;return 1;
  32. }
  33. template<class T1,class T2>
  34. int read(T1 &a,T2 &b) {
  35. return read(a)!=EOF&&read(b)!=EOF?2:EOF;
  36. }
  37. template<class T1,class T2,class T3>
  38. int read(T1 &a,T2 &b,T3 &c) {
  39. return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
  40. }
  41. typedef long long ll;
  42. const int Maxn=1100;
  43. const int inf=0x3f3f3f3f;
  44. int a[Maxn],b[Maxn],r[Maxn],c[Maxn];
  45. int n,x,tot;
  46. pair<pir,pir> ans[Maxn];
  47. void work(int x) {
  48. if(x==n) return ;
  49. if(a[x]==r[x]&&b[x]==c[x]) work(x+1);
  50. else {
  51. int y,z;
  52. for(int i=x;i<=n;i++) if(a[i]==r[x]) {
  53. y=i;
  54. break;
  55. }
  56. for(int i=x;i<=n;i++) if(b[i]==c[x]) {
  57. z=i;
  58. break;
  59. }
  60. swap(a[y],a[x]); swap(b[z],b[x]);
  61. ans[++tot]=mp(mp(y,x),mp(x,z));
  62. work(x+1);
  63. }
  64. }
  65. signed main() {
  66. // freopen("test.in","r",stdin);
  67. read(n);
  68. for(int i=1;i<=n;i++) read(x),r[x]=i;
  69. for(int i=1;i<=n;i++) read(x),c[x]=i;
  70. for(int i=1;i<=n;i++) a[i]=b[i]=i;
  71. work(1);
  72. printf("%d\n",tot);
  73. for(int i=1;i<=tot;i++) {
  74. printf("%d %d %d %d\n",ans[i].fr.fr,ans[i].fr.sc,ans[i].sc.fr,ans[i].sc.sc);
  75. }
  76. return 0;
  77. }

Codeforces Round #564 (Div. 1)的更多相关文章

  1. Codeforces Round #564 (Div. 2) C. Nauuo and Cards

    链接:https://codeforces.com/contest/1173/problem/C 题意: Nauuo is a girl who loves playing cards. One da ...

  2. Codeforces Round #564 (Div. 2) B. Nauuo and Chess

    链接:https://codeforces.com/contest/1173/problem/B 题意: Nauuo is a girl who loves playing chess. One da ...

  3. Codeforces Round #564 (Div. 2) A. Nauuo and Votes

    链接:https://codeforces.com/contest/1173/problem/A 题意: Nauuo is a girl who loves writing comments. One ...

  4. Codeforces Round #564 (Div. 2)B

    B. Nauuo and Chess 题目链接:http://codeforces.com/contest/1173/problem/B 题目 Nauuo is a girl who loves pl ...

  5. Codeforces Round #564 (Div. 2)A

    A. Nauuo and Votes 题目链接:http://codeforces.com/contest/1173/problem/A 题目 Nauuo is a girl who loves wr ...

  6. Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)

    D. Nauuo and Circle •参考资料 [1]:https://www.cnblogs.com/wyxdrqc/p/10990378.html •题意 给出你一个包含 n 个点的树,这 n ...

  7. Codeforces Round #564 (Div. 2)

    传送门 参考资料 [1]: the Chinese Editoria A. Nauuo and Votes •题意 x个人投赞同票,y人投反对票,z人不确定: 这 z 个人由你来决定是投赞同票还是反对 ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. 另类--kafka集群中jmx端口设置

    # 监控kafka集群 # 有一个问题,需要在kafka-server-start.sh文件中配置端口,有如下三种办法 # 第一种:复制并修改kafka目录,比如kafka-1,kafka-2,kaf ...

  2. EasyUI_前台js_分页

    1.html: <table id="DataTb" title="客户信息" class="easyui-datagrid" sty ...

  3. 【ES6 】声明变量的方式

    var function let const import class

  4. O039、Unshelve Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5529915.html   上一节我们 shelve Instance 到 Glance,本节学习如何通过 unshelv ...

  5. python对比线程,进程,携程,异步,哪个快

    目录概念介绍测试环境开始测试测试[单进程单线程]测试[多进程 并行]测试[多线程 并发]测试[协程 + 异步]结果对比绘图展示概念介绍首先简单介绍几个概念: 进程和线程进程就是一个程序在一个数据集上的 ...

  6. HBASE学习笔记(五)

    一.HBase的RowKey设计原则 1.我们知道HBase是三维有序存储的,通过RowKey(行键),ColumnKey(Column family和qualifier)和TimeStamp(时间戳 ...

  7. webpack开启本地服务器与热更新

    第一个webpack本地服务 webpack本地服务相关的一些操作指令与应用 一.第一个webpack本地服务 //工作区间 src//文件夹 index.js//入口文件 index.css//测试 ...

  8. linux工具之lsof

      1.lsof  ( list    open   files)   lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通 ...

  9. 2.java多线程_synchronized(Lock)同步

    1.synchronized同步关键词 线程安全是并发编程中的重要关注点,应该注意到的是,造成线程安全问题的主要诱因有两点,一是存在共享数据(也称临界资源),二是存在多条线程共同 操作共享数据.因此为 ...

  10. STM32/MINI