Codeforces Round#495

一场有趣的经历。
CF1004A - Sonya and Hotels(模拟)
CF1004B - Sonya and Exhibition(构造)
CF1004C - Sonya and Robots(枚举+计数)
CF1004D - Sonya and Matrix(构造+枚举)
CF1004E - Sonya and Ice Cream(树的直径,贪心,单调队列)

A

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
inline char gc(){
    static char buf[1<<16],*S,*T;   
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}

B

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 111
inline char gc(){
    static char buf[1<<16],*S,*T;   
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m;
int main(){
//  freopen("b.in","r",stdin);
    n=read();m=read();int now=1;
    for (int i=1;i<=m;++i) read(),read();
    for (int i=1;i<=n;++i) printf("%d",now),now^=1;
    return 0;
}

C

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
inline char gc(){
    static char buf[1<<16],*S,*T;   
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,a[N],cnt[N],ans=0;ll res=0;
bool used[N];
int main(){
//  freopen("a.in","r",stdin);
    n=read();
    for(int i=1;i<=n;++i){
        a[i]=read();if(++cnt[a[i]]==1) ++ans;
    }for(int i=1;i<=n;++i){
        if(--cnt[a[i]]==0) --ans;
        if(used[a[i]]) continue;used[a[i]]=1;
        res+=ans;
    }printf("%lld\n",res);
    return 0;
}

D

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 1000010
inline char gc(){
    static char buf[1<<16],*S,*T;   
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
inline int abs(int x) {return x>0?x:-x;}
int nm,cnt[N];
int tmp[N];
bool check(int n,int m,int x,int y){
    memset(tmp,0,sizeof tmp);
    for(int c=1;c<=nm;c++){
        int i=(c-1)/m+1,j=(c-1)%m+1;
        int d=abs(x-i)+abs(y-j);
        if(++tmp[d]>cnt[d]) return 0;
    }printf("%d %d\n%d %d\n",n,m,x,y);
    return 1;
}
int main()
{
  //  freopen("cf.in","r",stdin);
    nm=read();
    for(int i=1;i<=nm;i++) cnt[read()]++;
    int k=0;
    while(cnt[k+1]==(k+1)<<2) k++;
    for(int n=k+k+1;n<=nm;n++){
        if(nm%n) continue;
        int m=nm/n; if(m<k+k+1) continue;
        int i,y=0;
        for(i=k+1;k+1+i<=n;i++){
            int miss=(i-k)*2-1;
            if(cnt[i]<i*4-miss) {y=i; break;}
        }
        if(y) if(check(n,m,k+1,y)) return 0;
        for(i;i<=(m+1)>>1;i++){
            int miss=(i-k)*2-1+(i-(n-k-1))*2-1;
            if(cnt[i]<i*4-miss) {y=i; break;}
        }if(y) if(check(n,m,k+1,y)) return 0;
    }puts("-1");
    return 0;
}

E

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,s,fa[N],a[N],m,h[N],num=0,mx,rt,q[N];//a--直径
ll dis[N],mxd[N],ans=1LL<<60;bool mark[N];
struct edge{
    int to,next,val;
}data[N<<1];
inline void dfs(int x){
    if(dis[x]>dis[mx]) mx=x; 
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==fa[x]) continue;
        fa[y]=x;dis[y]=dis[x]+data[i].val;dfs(y);
    }
}
inline void dfs2(int x){
    if(mxd[x]>mxd[mx]) mx=x;
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==fa[x]||mark[y]) continue;
        mxd[y]=mxd[x]+data[i].val;dfs2(y);
    }
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();s=read();
    for(int i=1;i<n;++i){
        int x=read(),y=read(),val=read();
        data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
        data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=val;
    }mx=1;dfs(1);rt=mx;fa[rt]=0;dis[rt]=0;dfs(rt);
    while(mx) a[++m]=mx,mark[mx]=1,mx=fa[mx];
    for(int i=1;i<=m;++i){
        mx=a[i];dfs2(a[i]);mxd[a[i]]=mxd[mx];
    }int l=1,r=1,h=1,t=0;
    for(;l<=m;++l){
        while(h<=t&&q[h]<l) ++h;
        while(r<=m&&r-l<s){
            while(h<=t&&mxd[a[r]]>=mxd[a[q[t]]]) --t; 
            q[++t]=r;++r;
        }ans=min(ans,max(max(dis[a[1]]-dis[a[l]],dis[a[r-1]]),mxd[a[q[h]]]));
    }printf("%I64d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/icefox_zhx/article/details/80947453