天天看點

Codeforces Round #379 (Div. 2) 題解Anton and DanikAnton and DigitsAnton and Making PotionsAnton and ChessAnton and TreeAnton and School

Anton and Danik

模拟

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
int main()
{
//  freopen("A.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read();
    char s[];
    cin>>s;
    int p=,l=strlen(s);
    Rep(i,l) if (s[i]=='A') ++p;else --p;
    if (p>) puts("Anton");
    else if (p<) puts("Danik");
    else puts("Friendship");
    return ;
}
           

Anton and Digits

貪心

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
int main()
{
//  freopen("B.in","r",stdin);
//  freopen(".out","w",stdout);
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    int t=min(a,min(c,d));
    int ans=t*;
    a-=t,c-=t,d-=t;
    t=min(a,b);
    cout<<ans+t*<<endl;


    return ;
}
           

Anton and Making Potions

二分

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,m,k;
ll x,s;
ll a[],b[],c[],d[];
int main()
{
//  freopen("C.in","r",stdin);
//  freopen(".out","w",stdout);

    cin>>n>>m>>k>>x>>s;
    Rep(i,m) cin>>a[i];
    Rep(i,m) cin>>b[i];
    Rep(i,k) cin>>c[i];
    Rep(i,k) cin>>d[i];

    ll ans=(ll)n*x;
    Rep(i,m) if (s>=b[i]) {
        s-=b[i];
        int t=upper_bound(d,d+k,s)-d;
        t--;
        if (t>=) {
            ans=min(ans,(ll)max(LL,(ll)n-c[t])*a[i]);
        }
        else ans=min(ans,(ll)n*a[i]);
        s+=b[i];
    }
    int t=upper_bound(d,d+k,s)-d;
    t--;
    if (t>=) {
        ans=min(ans,(ll)max(LL,n-c[t])*x);
    }
    cout<<ans;



    return ;
}
           

Anton and Chess

找出8個方向最近的8個點

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (550000)
int n,x_,y_;
struct node{
    int typ,x,y;
    node(){}
    node(int typ,int a,int b):typ(typ),x(a),y(b){}
}a[MAXN];
bool cmp(node a,node b){
    return a.x<b.x||a.x==b.x&&a.y<b.y;
}
int main()
{
//  freopen("D.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>x_>>y_;
    Rep(i,n) {
        char s[];
        int x,y;
        cin>>s>>x>>y;
        a[i]=node(s[],x,y);
    }
    int mi=--,ma=-mi;
    Rep(i,n) if (a[i].x==x_) {
        if (a[i].y<y_) mi=max(mi,a[i].y);
        else ma=min(ma,a[i].y);
    }
    Rep(i,n) if (a[i].x==x_&&(a[i].y==mi || a[i].y==ma) && a[i].typ!='B') {
        puts("YES"); return ;
    } 

    mi=--,ma=-mi;
    Rep(i,n) if (a[i].y==y_) {
        if (a[i].x<x_) mi=max(mi,a[i].x);
        else ma=min(ma,a[i].x);
    }
    Rep(i,n) if (a[i].y==y_&&(a[i].x==mi || a[i].x==ma) && a[i].typ!='B') {
        puts("YES"); return ;
    } 

    Rep(i,n) {
        int p=a[i].x,q=a[i].y;
        a[i].x=p+q,a[i].y=p-q;
    }
    int p=x_,q=y_;
    x_=p+q,y_=p-q;

    mi=--,ma=-mi;
    Rep(i,n) if (a[i].x==x_) {
        if (a[i].y<y_) mi=max(mi,a[i].y);
        else ma=min(ma,a[i].y);
    }
    Rep(i,n) if (a[i].x==x_&&(a[i].y==mi || a[i].y==ma) && a[i].typ!='R') {
        puts("YES"); return ;
    } 

    mi=--,ma=-mi;
    Rep(i,n) if (a[i].y==y_) {
        if (a[i].x<x_) mi=max(mi,a[i].x);
        else ma=min(ma,a[i].x);
    }
    Rep(i,n) if (a[i].y==y_&&(a[i].x==mi || a[i].x==ma) && a[i].typ!='R') {
        puts("YES"); return ;
    } 
    puts("NO");


    return ;
}
           

Anton and Tree

同顔色連通塊縮點後,樹的直徑/2(向下取整)

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (201000)
int n,a[MAXN],f[MAXN];
vi edges[MAXN];
void dfs(int x,int fa) {
    if (!fa) f[x]=;
    else f[x]=f[fa]+(a[x]^a[fa]);
    Rep(i,SI(edges[x])) {
        int v=edges[x][i];
        if (v==fa) continue;
        dfs(v,x);
    }
} 
int main()
{
//  freopen("E.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n;
    For(i,n) cin>>a[i];
    For(i,n-) {
        int u=read(),v=read();
        edges[u].pb(v);
        edges[v].pb(u);
    }
    dfs(,);
    int p=;
    For(i,n) if (f[i]>f[p]) p=i;
    dfs(p,);
    p=;
    For(i,n) if (f[i]>f[p]) p=i;
    cout<<ceil((f[p]-)*)<<endl;
    return ;
}
           

Anton and School

(a and b)+(a or b)=a+b

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=,f=; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
    while(isdigit(ch)) { x=x*+ch-'0'; ch=getchar();}
    return x*f;
} 
int n;
ll a[],b[],c[];
ll f[];
int main()
{
//  freopen("F.in","r",stdin);
//  freopen(".out","w",stdout);

    cin>>n;
    For(i,n) cin>>b[i];
    For(i,n) cin>>c[i];

    ll tot=;
    For(i,n) tot+=b[i]+c[i];
    tot/=*n;
    For(i,n) {
        a[i]=b[i]+c[i]-tot;
        if (a[i]%n) {
            puts("-1"); return ;
        }
        a[i]/=n;
    }
    For(i,n) Rep(j,) f[j]+=&(a[i]>>j);
    For(i,n) {
        ll g=;
        Rep(j,) if (&(a[i]>>j)) g+=f[j]*(LL<<j);
        if (b[i]^g) {
            puts("-1"); return ;
        }
    }

    For(i,n-) cout<<a[i]<<' ';cout<<a[n];

    return ;
}