# Codeforces Round #541 (Div. 2)题解

https://codeforces.com/contest/1131

### D

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rt register int
#define ll long long
using namespace std;
ll x = 0; char zf = 1; char ch = getchar();
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int i,j,k,m,n,x,y,z,cnt,sum;
char c[1010][1010];int fa[2010];
}
void unite(int x,int y){
if(p!=q)fa[q]=p;
}
struct query{
int x,y;
}q[2000010];int t;
int F[2000010],L[2000010],N[2000010],a[2000010],du[2000010],ans[2000010];
a[++k]=y;du[y]++;
if(!F[x])F[x]=k;
else N[L[x]]=k;
L[x]=k;
}
int qu[2000010],h;bool vis[2000010];
int main(){
for(rt i=1;i<=n+m;i++)fa[i]=i;
for(rt i=1;i<=n;i++){
scanf("%s",c[i]+1);
}
for(rt i=1;i<=n;i++)
for(rt j=1;j<=m;j++)if(c[i][j]=='=')unite(i,j+n);
else if(c[i][j]=='<')q[++t]={i,j+n};
else q[++t]={j+n,i};
for(rt i=1;i<=t;i++){
if(x==y){
puts("No");
return 0;
}
}
h=t=0;
while(h<t){
x=qu[++h];vis[x]=1;
for(rt i=F[x];i;i=N[i]){
ans[a[i]]=max(ans[a[i]],ans[x]+1);
if(!--du[a[i]])qu[++t]=a[i];
}
}
puts("No");return 0;
}
puts("Yes");
return 0;
}

### E

$f_{i,j}$表示前$i$个字符串的积字符$j$的最长连续子串长度

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int k,m,n,x,y,z,cnt,ans,len;
char c[100010];
int f[100010][30];//最长子串
int pre[30],succ[30],mx[30];
void chk(){
for(rt i='a';i<='z';i++){
for(rt j=1;j<=len+1;j++)if(c[j]!=i||j>len){
pre[i-'a']=j-1;
break;
}
for(rt j=len;j>=0;j--)if(c[j]!=i||j<=0){
succ[i-'a']=len-j;
break;
}
int now=0;mx[i-'a']=0;
for(rt j=1;j<=len;j++){
if(c[j]==i)now++;
if(j==len||c[j]!=i){
mx[i-'a']=max(mx[i-'a'],now);
now=0;
}
}
}
}
int main(){
for(rt i=1;i<=n;i++){
scanf("%s",c+1);len=strlen(c+1);chk();
if(i==1)for(rt j='a';j<='z';j++)f[1][j-'a']=mx[j-'a'];
else {
for(rt j='a';j<='z';j++)if(f[i-1][j-'a'])f[i][j-'a']=1;
for(rt j='a';j<='z';j++)if(f[i-1][j-'a']){
if(pre[j-'a']==len)
f[i][j-'a']=(f[i-1][j-'a']+1)*len+f[i-1][j-'a'];
else f[i][j-'a']=1+pre[j-'a']+succ[j-'a'];
}
}
for(rt j='a';j<='z';j++)f[i][j-'a']=max(f[i][j-'a'],mx[j-'a']);
}
int ans=0;
for(rt i='a';i<='z';i++)ans=max(ans,f[n][i-'a']);
cout<<ans;
return 0;
}

### F

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rt register int
#define ll long long
#define p 998244353
using namespace std;
ll x = 0; char zf = 1; char ch = getchar();
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int i,j,k,m,n,x,y,z,cnt,sum;
int F[450010],N[450010],L[450010],a[450010],fa[450010];
a[++k]=y;fa[y]=x;
if(!F[x])F[x]=k;
else N[L[x]]=k;
L[x]=k;
}
}
void dfs(int x){
if(x<=n)write(x),putchar(' ');
for(rt i=F[x];i;i=N[i])dfs(a[i]);
}
int main(){
for(rt i=1;i<=n;i++)fa[i]=i;
cnt=n;
for(rt i=1;i<n;i++){
cnt++;fa[cnt]=cnt;
}
dfs(cnt);
return 0;
}

### G

（有好多人带$\log$都过了

#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define rt register int
#define ll long long
using namespace std;
ll x=0;char zf=1;char ch=getchar();
while(ch!='-'&&!isdigit(ch))ch=getchar();
if(ch=='-')zf=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf;
}
void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);}
void writeln(const ll y){write(y);putchar('\n');}
int m,n,x,y,z,cnt,ans;
vector<int>a[250010],b[250010];
int k[250010],h[10000010],t;ll c[10000010];
int L[10000010],R[10000010];
int sta[10000010],top;ll dp[10000010],fy[10000010];
int main(){
for(rt i=1;i<=m;i++){
a[i].resize(k[i]);b[i].resize(k[i]);
}
for(rt i=0;i<k[id];i++)t++,h[t]=a[id][i],c[t]=1ll*b[id][i]*ct;
}
for(rt i=1;i<=n;i++)L[i]=R[i]=i;
sta[top=0]=0;
for(rt i=1;i<=n;i++){
while(i-sta[top]<h[i]&&top)top--;
L[i]=sta[top];sta[++top]=i;
}
sta[top=0]=n+1;
for(rt i=n+1;i>=1;i--){
while(sta[top]-i<h[i]&&top)top--;
R[i]=sta[top];sta[++top]=i;
}
dp[0]=0;
sta[top=0]=0;
for(rt i=1;i<=n;i++){
dp[i]=dp[L[i]]+c[i];
while(R[sta[top]]<=i&&top)top--;
if(top)dp[i]=min(dp[i],fy[sta[top]]);fy[i]=dp[i-1]+c[i];
while(top&&R[sta[top]]==R[i]&&fy[i]<=fy[sta[top]])top--;
if(top==0||fy[i]<fy[sta[top]])sta[++top]=i;
}
cout<<dp[n];
return 0;
}

posted @ 2019-02-25 20:58  Kananix  阅读(104)  评论(1编辑  收藏