Codeforces Round #564 (Div. 1)

A Nauuo and Cards

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

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

#include<bits/stdc++.h>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define pir pair<int,int>
#define mp(x,y) make_pair(x,y)
#define fr first
#define sc second
#define rsort(x,y) sort(x,y),reverse(x,y)
#define vic vector<int>
#define vit vic::iterator
using namespace std; char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
} template<class T>
int read(T &ans) {
T f=1;ans=0;
char ch=gc();
while(!isdigit(ch)) {
if(ch==EOF) return EOF;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
} template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)==EOF?EOF:read(b);
} template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)==EOF?EOF:read(c);
} typedef long long ll;
const int Maxn=1100000;
const int inf=0x3f3f3f3f;
const ll mod=998244353; int n,a[Maxn],b[Maxn],c[Maxn],x,ans; bool work() {
for(int i=1;i<=n;i++) c[i]=b[i];
int now=1;
for(int i=a[n]+1;i<=n;i++) {
if(!c[i]) return false;
c[a[now++]]++;
}
return true;
} int main() {
// freopen("test.in","r",stdin);
read(n);
for(int i=1;i<=n;i++) read(x),b[x]++;
for(int i=1;i<=n;i++) read(a[i]);
int sxz=0;
for(int i=1;i<=n;i++) {
if(a[i]==1) {
int flag=0;
for(int j=1;j<=n-i;j++) if(a[i+j]!=j+1) {
flag=1;
break;
}
if(!flag) sxz=i;
break;
}
}
if(sxz&&work()) return 0*printf("%d\n",n-a[n]);
else {
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) if(a[i]) c[a[i]]=i;
for(int i=1;i<=n;i++) qmax(ans,c[i]+n-i+1);
printf("%d\n",ans);
}
return 0;
}

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\)。

#include<bits/stdc++.h>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define pir pair<int,int>
#define mp(x,y) make_pair(x,y)
#define fr first
#define sc second
#define rsort(x,y) sort(x,y),reverse(x,y)
#define vic vector<int>
#define vit vic::iterator
using namespace std; char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
} template<class T>
int read(T &ans) {
T f=1;ans=0;
char ch=gc();
while(!isdigit(ch)) {
if(ch==EOF) return EOF;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
} template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)==EOF?EOF:read(b);
} template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)==EOF?EOF:read(c);
} typedef long long ll;
const int Maxn=1100000;
const int inf=0x3f3f3f3f;
const ll mod=998244353; int to[Maxn],nxt[Maxn],first[Maxn],tot=1,dp[Maxn],n,u,v; inline void add(int u,int v) {
to[tot]=v;
nxt[tot]=first[u];
first[u]=tot++;
to[tot]=u;
nxt[tot]=first[v];
first[v]=tot++;
} void dfs(int root,int fa=0) {
int x=1,y=1,temp=1;
for(int i=first[root];i;i=nxt[i]) {
if(to[i]!=fa) {
dfs(to[i],root);
x=1ll*x*temp%mod; temp++;
y=1ll*y*dp[to[i]]%mod;
}
}
x=1ll*x*temp%mod;
dp[root]=1ll*x*y%mod;
} int main() {
// freopen("test.in","r",stdin);
read(n);
for(int i=1;i<n;i++) {
read(u,v);
add(u,v);
}
int x=1,y=1,temp=1;
for(int i=first[1];i;i=nxt[i]) {
dfs(to[i],1);
x=1ll*x*temp%mod; temp++;
y=1ll*y*dp[to[i]]%mod;
}
dp[1]=1ll*x*y%mod;
printf("%I64d\n",1ll*n*dp[1]%mod);
return 0;
}

C Nauuo and Pictures

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

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

#include<bits/stdc++.h>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define pir pair<int,int>
#define mp(x,y) make_pair(x,y)
#define fr first
#define sc second
#define rsort(x,y) sort(x,y),reverse(x,y)
#define vic vector<int>
#define vit vic::iterator
using namespace std; char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
} template<class T>
int read(T &ans) {
T f=1;ans=0;
char ch=gc();
while(!isdigit(ch)) {
if(ch==EOF) return EOF;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
} template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)==EOF?EOF:read(b);
} template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)==EOF?EOF:read(c);
} typedef long long ll;
#define int long long
const int Maxn=1100000;
const int inf=0x3f3f3f3f;
const ll mod=998244353; int n,m,a[Maxn],b[Maxn],sxz,zhy,f[3100][3100],ansx,ansy; ll powp(ll a,ll b) {
ll ans=1;
while(b) {
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
} signed main() {
// freopen("test.in","r",stdin);
read(n,m);
for(int i=1;i<=n;i++) read(a[i]);
for(int i=1;i<=n;i++) read(b[i]);
for(int i=1;i<=n;i++)
if(a[i]) sxz=(sxz+b[i])%mod;
else zhy=(zhy+b[i])%mod;
f[0][0]=1;
for(int i=1;i<=m;i++)
for(int j=0;j<i;j++) {
if(i-j-1==zhy) f[j+1][i-j-1]=(f[j+1][i-j-1]+f[j][i-j-1])%mod;
else {
int x=sxz+j,y=zhy-(i-j-1),tot=powp(x+y,mod-2);
f[j+1][i-j-1]=(f[j+1][i-j-1]+1ll*f[j][i-j-1]*x%mod*tot%mod)%mod;
f[j][i-j]=(f[j][i-j]+1ll*f[j][i-j-1]*y%mod*tot%mod)%mod;
}
}
for(int i=0;i<=m;i++)
ansx=(ansx+1ll*f[i][m-i]*i%mod)%mod,ansy=(ansy+1ll*f[i][m-i]*(m-i)%mod)%mod;
int x=(sxz+ansx)%mod,y=(zhy-ansy+mod)%mod;
sxz=powp(sxz,mod-2),zhy=powp(zhy,mod-2);
for(int i=1;i<=n;i++) if(a[i]) printf("%I64d\n",sxz*b[i]%mod*x%mod);
else printf("%I64d\n",zhy*b[i]%mod*y%mod);
return 0;
}

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\),也可以把第一行和第一列去掉。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vic vector<int>
#define vit vic::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
#define rsort(x,y) sort(x,y),reverse(x,y)
using namespace std; inline char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
} template<class T>
int read(T &ans) {
ans=0;char ch=gc();T f=1;
while(!isdigit(ch)) {
if(ch==EOF) return -1;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
} template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)!=EOF&&read(b)!=EOF?2:EOF;
} template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
} typedef long long ll;
const int Maxn=1100;
const int inf=0x3f3f3f3f; int a[Maxn],b[Maxn],r[Maxn],c[Maxn];
int n,x,tot;
pair<pir,pir> ans[Maxn]; void work(int x) {
if(x==n) return ;
if(a[x]==r[x]&&b[x]==c[x]) work(x+1);
else {
int y,z;
for(int i=x;i<=n;i++) if(a[i]==r[x]) {
y=i;
break;
}
for(int i=x;i<=n;i++) if(b[i]==c[x]) {
z=i;
break;
}
swap(a[y],a[x]); swap(b[z],b[x]);
ans[++tot]=mp(mp(y,x),mp(x,z));
work(x+1);
}
} signed main() {
// freopen("test.in","r",stdin);
read(n);
for(int i=1;i<=n;i++) read(x),r[x]=i;
for(int i=1;i<=n;i++) read(x),c[x]=i;
for(int i=1;i<=n;i++) a[i]=b[i]=i;
work(1);
printf("%d\n",tot);
for(int i=1;i<=tot;i++) {
printf("%d %d %d %d\n",ans[i].fr.fr,ans[i].fr.sc,ans[i].sc.fr,ans[i].sc.sc);
}
return 0;
}

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. Elastic Search中DSL Query的常见语法

    Query DSL是一种通过request body提交搜索参数的请求方式.就是将请求头参数(?xxx=xxx)转换为请求体参数.语法格式:GET [/index_name/type_name]/_s ...

  2. Elastic Search快速上手(1):简介及安装配置

    前言 最近开始尝试学习Elastic Search,因此决定做一些简单的整理,以供后续参考,快速上手使用ES. 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多 ...

  3. nginx部署vue前端,刷新出现404或者500错误的解决方案

    在nginx配置文件的server下加上 try_files $uri $uri/ /index.html; 不加的话是404,路径错误是500,这里的路径只要照着/index.html就行,不用加上 ...

  4. redis 学习(17) -- RDB

    redis -- RDB 什么是 RDB 经过RDB之后,redis会将内存中的数据创建一份快照到硬盘中,称为RDB文件(二进制) 当redis重新启动时,会加载硬盘中的RDB文件,加载到内存中完成数 ...

  5. ccpc湘潭邀请赛 Partial Sum

    选定最多m的区间,使区间和的绝对值最大.但是左右端点不能重复选取 首先涉及到区间和的问题,就应该想到用前缀和去优化 这里对前缀和排序 然后贪心的去选取最大.次大 (比赛的时候脑子堵的很,没想出来 可惜 ...

  6. JDBC 24homework

    编写程序: 1. 创建商品信息表Goods(包含编号Code.名称Name.数量Number.单价Price) 2. 设计程序流程,由用户选择:插入.删除.修改.查询 程序效果如下: (1)根据提示输 ...

  7. 移动站Web开发图片自适应两种常见情况解决方案

    本文主要说的是Web中图片根据手机屏幕大小自适应居中显示,图片自适应两种常见情况解决方案.开始吧 在做配合手机客户端的Web wap页面时,发现文章对图片显示的需求有两种特别重要的情况,一是对于图集, ...

  8. dev chart使用

    public class LineChartHelp { #region 折线图 /// <summary> /// 创建折线图 /// </summary> public v ...

  9. O063、NFS Volume Provider(Part II)

    参考https://www.cnblogs.com/CloudMan6/p/5693771.html   本节开始创建 NFS volume ,操作方法和 LVM volume一样,唯一的区别是在 v ...

  10. python爬虫redis-ip代理池搭建几十万的ip数据--可以使用

    from bs4 import BeautifulSoupimport requests,os,sys,time,random,redisfrom lxml import etreeconn = re ...