码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #479 (Div. 3)题解

时间:2019-06-15 20:14:08      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:force   lld   reg   use   problem   git   stream   include   暴力   

罚时有点小严重,但是div.3确实快乐.

A.Wrong Subtraction

模拟,暴力模拟.

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
    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(){
    read(n),read(m);
    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;
void read(int &x) {
    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(){
    read(n);scanf("%s",s+1);
    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

小清新的判断题,sort就可以了,判断很小清新的.

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
    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(){
    read(n),read(m);
    for(rg int i=1;i<=n;i++)read(a[i]);
    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
void read(int &x) {
    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(){
    read(n);
    for(rg int i=1;i<=n;i++)read(a[i]);
    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;
void read(int &x) {
    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];
void add(int x,int y){
    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(){
    read(n),read(m);
    for(rg int i=1,x,y;i<=m;i++)
        read(x),read(y),add(x,y);
    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\)个位置结尾最长的答案子序列的长度

那么显然我们可以先离散化,然后用vector记下一个值所有出现过的位置

然后对于第\(i\)个位置,我们只需要找出大于\(i\)的第一个值等于\(a[i]+1\)的位置\(j\),更新\(f[j]\)就行了,这个过程可以二分实现

然后最大的\(f\)值就是答案

答案序列再扫一遍也可以求出

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
void read(int &x) {
    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(){
    read(n);
    for(rg int i=1;i<=n;i++){
        read(a[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]);
}

Codeforces Round #479 (Div. 3)题解

标签:force   lld   reg   use   problem   git   stream   include   暴力   

原文地址:https://www.cnblogs.com/lcxer/p/11028523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!