AtCoder Beginner Contest 131 Solution

Foreword

The ABC is still a little difficult now.

TaskA Security

Solution

Direct Simulation enough.

Code

/*
  mail: [email protected]
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
char s[100010];
int main(){
    scanf("%s",s+1);
    for(int i=1;i<strlen(s+1);i++){
        if(s[i]==s[i+1])return puts("Bad"),0;
    }
    puts("Good");
    return 0;
}

TaskB Bite Eating

Solution

Direct enumeration then delete the judge on the line.

Code

/*
  mail: [email protected]
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
int n,L,a[210];
int main(){
    n=gi();L=gi();int Sum=0;
    for(int i=1;i<=n;i++)a[i]=L+i-1,Sum+=a[i];
    int Min=1e9+10,ans;
    for(int i=1;i<=n;i++){
        int sum=0;
        for(int j=1;j<=n;j++)if(i!=j)sum+=a[j];
        
        if(abs(Sum-sum)<Min){Min=abs(Sum-sum);ans=sum;}
    }
    printf("%d\n",ans);
    return 0;
}

TaskC Anti-Division

Solution

Seeking a \ (gcd \) inclusion and exclusion on the line.

Code

/*
  mail: [email protected]
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define int ll
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
ll get(ll a,ll b,ll c){
    return (b-a+1)-(b/c-(a-1)/c);
}
ll gcd(ll a,ll b){
    if(!b)return a;
    return gcd(b,a%b);
}
ll a,b,sumc,sumd,sumcd;int c,d;
signed main(){
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    printf("%lld\n",get(a,b,c)+get(a,b,d)-get(a,b,1ll*c*d/gcd(c,d)));
    return 0;
}

TaskD Megalomania

Solution

Direct then sorted according to the latest finish line simulation.

Code

/*
  mail: [email protected]
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define int ll
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
struct node{int x,y;}job[200010];
int n;
bool operator<(const node a,const node b){
    return a.y<b.y || (a.y==b.y && a.x<b.x);
}
signed main(){
    n=gi();
    for(int i=1;i<=n;i++)job[i].x=gi(),job[i].y=gi();
    sort(&job[1],&job[n+1]);int now=0;
    for(int i=1;i<=n;i++){
        now+=job[i].x;
        if(now>job[i].y)return puts("No"),0;
    }
    puts("Yes");
    return 0;
}

Bag Friendships

Solution

Consider chrysanthemum figure is up, then step by step to daisy drawing inside edged on the line.

Code

/*
  mail: [email protected]
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
int n,k;
struct node{int x,y;}edge[1000010];
int main(){
    n=gi();k=gi();
    int tot=(n-2)*(n-1)/2,m=n-1;
    for(int i=1;i<=m;i++)edge[i].x=n,edge[i].y=i;
    if(k>tot)return puts("-1"),0;int now=2,Q=1;
    n--;
    while(tot!=k){
        tot--;
        edge[++m]=(node){now,(now+Q-1)%n+1};
        now++;now=(now-1)%n+1;
        if(now==2)Q++;
    }
    printf("%d\n",m);
    for(int i=1;i<=m;i++)
        printf("%d %d\n",edge[i].x,edge[i].y);
    return 0;
}

TaskF Must Be Rectangular!

Solution

\ (yyb \) is the god !!!
consider a point \ (x, y \) division, then it is equivalent to \ (the X-\) -> \ (the y-\) even side, then there is considered a block Unicom can certainly mess complete graph, then the complete graph of the number of edges that point ...
the last direct subtract the original number of points gone.

Code

/*
  mail: [email protected]
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define MAX 100100
inline int gi(){
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
int n,f[MAX<<1];
int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
int sz1[MAX<<1],sz2[MAX<<1];
ll ans;
int main(){
    n=gi();
    for(int i=1;i<MAX+MAX;++i)f[i]=i;
    for(int i=1;i<=n;++i)
    {
        int u=gi(),v=gi();
        f[getf(u)]=getf(v+MAX);
    }
    for(int i=1;i<=MAX;++i)
        sz1[getf(i)]+=1;
    for(int i=MAX+1;i<MAX+MAX;++i)
        sz2[getf(i)]+=1;
    for(int i=1;i<MAX+MAX;++i)ans+=1ll*sz1[i]*sz2[i];
    cout<<ans-n<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/mle-world/p/11071739.html