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. Win32汇编常用算数指令

    汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操作码,用地 ...

  2. 遗传算法与Java代码简单实现

    参阅地址: https://www.jianshu.com/p/ae5157c26af9 代码实现: public class GA { private int ChrNum = 10; //染色体数 ...

  3. 怎样在 Vue 里面使用自定义事件将子组件的数据传回给父组件?

    首先, Vue 里面的组件之间的数据流动是 单向 的, 数据可以从父组件传递给子组件, 但不能从子组件传递给父组件, 因为组件和组件之间是 隔离 的. 就像两个嵌套的 黑盒子 . 能通过 props ...

  4. Aveva Marine C# 二次开发入门001

    1# 引用 C:\AVEVA\Marine\OH12.1.SP4\Aveva.ApplicationFramework.dll C:\AVEVA\Marine\OH12.1.SP4\Aveva.App ...

  5. sql使用临时表循环

    code CREATE PROCEDURE sp_Update_Blogger_Blog_ArticleCount AS BEGIN declare @account varchar(); --博主账 ...

  6. C3.js入门案例

    C3.js是基于D3.js开发的JavaScript库,它可以让开发者构建出可复用的图表,并且还提供了一系列图表上的交互行为.通过C3,只需要往generate函数中传入数据对象就可以轻松的绘制出图表 ...

  7. O026、Nova组件详解

    参考https://www.cnblogs.com/CloudMan6/p/5436855.html   本节开始,我们将详细讲解 Nova 的各个子服务.   前面架构概览一节知道 Nova 有若干 ...

  8. wpf menuitem 简约显示的 template样式

    <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}&qu ...

  9. ObjectMapper用于将java对象转换为json格式数据以及JSONObject对象解析json格式数据

    ObjectMapper objectMapper = new ObjectMapper(); //反序列化的时候如果多了其他属性,不抛出异常 objectMapper.configure(Deser ...

  10. Angular 开发环境搭建

    Angular 是一款开源 JavaScript 框架,由Google 维护,用来协助单一页面应用程序运行的.它的目标是增强基于浏览器的应用,使开发和测试变得更加容易.目前最新的 Angular 版本 ...