# Codeforces Round #479 (Div. 3)题解

### A.Wrong Subtraction

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;
int n,m;
int main(){
while(m--){
if(n%10==0)n/=10;
else n--;
}
printf("%d\n",n);
}

### B.Two-gram

$O(n^2)$暴力枚举.

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;
int n,m,ans;char s[maxn];
int main(){
for(rg int i=1;i<n;i++){
int now=0;
for(rg int j=1;j<n;j++)
if(s[i]==s[j]&&s[i+1]==s[j+1])now++;
if(now>m)ans=i,m=now;
}
printf("%c%c",s[ans],s[ans+1]);
}

### C.Less or Equal

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=3e5+10;
int n,m,ans,a[maxn];
int main(){
sort(a+1,a+n+1);
if(a[1]==1&&m==0)return printf("-1\n"),0;
if(m==0)return printf("1\n"),0;
if(a[m]==a[m+1])printf("-1\n");
else printf("%d\n",a[m]);
}

### D.Divide by three, multiply by two

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=3e5+10;
int l[maxn],flag,r[maxn],n,m,ans,a[maxn],used[maxn],b[maxn];
void dfs(int x,int now){
used[now]=1;b[x]=a[now];
if(x==n){flag=1;return ;}
if(l[now]&&!used[l[now]])dfs(x+1,l[now]),used[l[now]]=0;
if(flag)return ;
if(r[now]&&!used[r[now]])dfs(x+1,r[now]),used[r[now]]=0;
if(flag)return ;
}
signed main(){
for(rg int i=1;i<=n;i++)
for(rg int j=1;j<=n;j++){
if(a[j]==a[i]*2)l[i]=j;
if(a[j]*3==a[i])r[i]=j;
}
for(rg int i=1;i<=n;i++){
used[i]=1;b[1]=a[i];
if(l[i]&&!used[l[i]])dfs(2,l[i]),used[l[i]]=0;
if(flag)break;
if(r[i]&&!used[r[i]])dfs(2,r[i]),used[r[i]]=0;
if(flag)break;
used[i]=0;
}
for(rg int i=1;i<=n;i++)printf("%lld ",b[i]);
}

### E.Cyclic Components

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=4e5+10;
int n,m,now,sum,pre[maxn*2],ans,nxt[maxn*2],h[maxn],cnt=1,flag;
bool vis[maxn],used[maxn*2];
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt;
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt;
}
void dfs(int x){
if(!vis[x])sum++,vis[x]=1;int d=0;
for(rg int i=h[x];i;i=nxt[i]){d++;
if(!used[i])used[i]=used[i^1]=1,now++,dfs(pre[i]);
}
if(d!=2)flag=1;
}
int main(){
for(rg int i=1,x,y;i<=m;i++)
for(rg int i=1;i<=200000;i++){
now=sum=flag=0;
if(!vis[i]){
dfs(i);
if(now==sum&&!flag)ans++;
}
}
printf("%d\n",ans);
}

### F.Consecutive Subsequence

$f[i]$表示以第$i$个位置结尾最长的答案子序列的长度

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=3e5+10;
int n,b[maxn],a[maxn],ans,tot,f[maxn],id[maxn];
map<int,int>mp;
vector<int>d[maxn];
vector<int>::iterator it;
int main(){
for(rg int i=1;i<=n;i++){
if(!mp[a[i]])mp[a[i]]=++tot;
d[mp[a[i]]].push_back(i);
}
for(rg int i=1;i<=n;i++)f[i]=1;
for(rg int i=1;i<=n;i++){
int now=mp[a[i]+1];
it=lower_bound(d[now].begin(),d[now].end(),i);
if(it!=d[now].end())f[*it]=max(f[*it],f[i]+1);
}
for(rg int i=1;i<=n;i++)ans=max(ans,f[i]);
printf("%d\n",ans);int g=ans;
for(rg int i=n;i>=1;i--){
if(f[i]==g)b[g]=i,ans=g-1;
else if(f[i]==ans&&a[i]==a[b[ans+1]]-1)b[ans]=i,ans--;
}
for(rg int i=1;i<=g;i++)printf("%d ",b[i]);
}
posted @ 2019-06-15 18:57 蒟蒻--lichenxi 阅读(...) 评论(...) 编辑 收藏