Codeforces Global Round 3 题解

The game let me on the orange.

The first three questions are flood problem, do not say.

Fourth Question think a bit difficult, even thought there is no guarantee right. (D so that the following practices could be wrong)

Difficulty E is $ 2300 $, but the feeling is very simple ah? ? ? He says good crooked nuts good structure it? ? ?

This extraordinary play overdone, next time will certainly be out QwQ.


A

Water problem. But Category talk a little uncomfortable, 8min result was handed in by others dumped by a mile.

Time complexity $ O (1) $.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=100010;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
int a,b,c;
int main(){
    a=read();b=read();c=read();
    ll ans=2* C;
    if (a> b) and + = 2 * b + 1 ;
    else  if (a <b) and + = 2 * a + 1 ;
    else years + = 2 * a; 
    cout << years; 
}
View Code

B

Water problem. One kind of enum delete several routes in the former, obviously the smallest number you want to delete. Then separate the two in this case can take the second route in which the first, and then delete a few on this basis.

Special attention to the situation sentenced the first route can be deleted finished.

Time complexity $ O (n \ log n) $. (In fact, you can do $ O (n) $, but will be a little difficult to play, as the B title is unnecessary)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=200020;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
int n,m,ta,tb,k,a[maxn],b[maxn],ans;
int main(){
    n=read();m=read();ta=read();tb=read();k=read();
    if(k>=min(n,m)) return puts("-1"),0;
    FOR(i,1,n) a[i]=read()+ta;
    FOR(i,1,m) b[i]=read();
    sort(a+1,a+n+1);sort(b+1,b+m+1);
    FOR(i,1,k+1){
        int p=lower_bound(b+1,b+m+1,a[i])-b;
        if(p+k-i+1>m) return puts("-1"),0;
        ans=max(ans,b[p+k-i+1]+tb);
    }
    printf("%d\n",ans);
}
View Code

C

Water problem. Simple configuration problem. (The following approach is to be able to $ 5n $ card and stinking long konjac practice, there should be more concise or better practice)

Consider how each number $ i $ by not more than $ 5 $ times the exchange to position $ i $.

  1. If you are already, do not bother. $ 0 $ times.
  2. If the direct exchange (from $ \ ge \ frac {n} {2} $), direct exchange. $ 1 $ times.
  3. If $ i \ le \ frac {n} {2} $ and $ at_i \ le \ frac {n} {2} $, $ a $ P_n as an intermediary variable. $ 3 $ times.
  4. If $ i> \ frac {n} {2} $ and $ at_i> \ frac {n} {2} $, $ a $ P_1 as an intermediary variable. $ 3 $ times.
  5. If not satisfied, then a $ \ le \ frac {n} {2} $, a $> \ frac {n} {2} $. To the left with $ p_n $ exchange, to the right of the $ p_1 $ exchange. Then you can put $ p_1, p_n $ exchange. Finally the left with $ p_n $ exchange, to the right of the $ p_1 $ exchange. $ 5 $ times.

(If you can not understand, it is recommended to manually simulate what)

Time complexity $ O (n) $.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=300030;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
int n,p[maxn],id[maxn],m,a[maxn*5],b[maxn*5];
int tp(int x){return x<=n/2?0:1;}
int main(){
    n=read();
    FOR(i,1,n) id[p[i]=read()]=i;
    FOR(i,1,n){
        if(p[i]==i) continue;
        int x=i,y=id[i],t1=tp(x),t2=tp(y);
        if(abs(x-y)*2>=n){
            m++;
            a[m]=x;b[m]=y;
        }
        else if(t1==t2){
            int ot=t1?1:n;
            m++;
            a[m]=x;b[m]=ot;
            m++;
            a[m]=y;b[m]=ot;
            m++;
            a[m]=x;b[m]=ot;
        }
        else{
            if(t1>t2) swap(x,y),swap(t1,t2);
            m++;
            a[m]=x;b[m]=n;
            m++;
            a[m]=1;b[m]=y;
            m++;
            a[m]=1;b[m]=n;
            m++;
            a[m]=1;b[m]=y;
            m++;
            a[m]=x;b[m]=n;
        }
        swap(p[x],p[y]);
        id[p[x]]=x;
        id[p[y]]=y;
    }
    printf("%d\n",m);
    FOR(i,1,m) printf("%d %d\n",a[i],b[i]);
}
View Code

D

Two types of discussions, the first one is $ a_1> b_1 <a_2> b_2 \ cdots $, the second is $% #! * & ^ @ !.

Here in a first example.

Here it is not. Then press $ b $ from small to large, greedy selected. pass? ? ?

And other official solution to a problem and then I come out of this pit (pot?) Mend it.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=2222222;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
struct item{
    int a,b,id;
    bool operator<(const item &i)const{return b<i.b;}
}t1[maxn],t2[maxn]; 
int n,m1,m2,a[maxn],b[maxn],al1,ans1[maxn],al2,ans2[maxn];
int main(){
    n=read();
    FOR(i,1,n) a[i]=read(),b[i]=read();
    FOR(i,1,n) if(a[i]>b[i]) t1[++m1]=(item){a[i],b[i],i};
    sort(t1+1,t1+m1+1);
    FOR(i,1,m1) if(t1[i].a>t1[ans1[al1]].b) ans1[++al1]=i;
    FOR(i,1,n) if(a[i]<b[i]) t2[++m2]=(item){a[i],b[i],i};
    sort(t2+1,t2+m2+1);
    t2[0].b=1e9;
    ROF(i,m2,1) if(t2[i].a<t2[ans2[al2]].b) ans2[++al2]=i;
    if(al1>al2){
        printf("%d\n",al1);
        FOR(i,1,al1) printf("%d ",t1[ans1[i]].id);
    }
    else{
        printf("%d\n",al2);
        FOR(i,1,al2) printf("%d ",t2[ans2[i]].id);
    }
}
View Code

E

Very simple structure problem, why even so many crooked nuts will not ......

First discovered $ 0 \ le 2d \ le s_j-s_i $ equivalent change in the relationship between the size of $ i $ and $ j $ a. Below we construct in size as long as the same conditions.

So you can put $ s $ and $ t $ from small to large.

To obtain a change value of each element. ($ D_i = s_i-t_i $) If the value of the sum of $ 0 $ is not no solution.

From small to large changes in the value of the enumeration. A change in the value of each enumeration we thought to turn it into $ 0 $.

First, if $ d_i $ is negative, due to a previous $ d $ is $ 0 $, so the change in value can no longer be increased. No solution.

Otherwise, find the first negative behind this. You can operate these two numbers.

Repeat until date = $ d_i 0 $.

The first location found a negative did not fall. Can be pushed back by a non-stop pointer.

Time complexity $ O (n \ log n) $. Since each enumerator or a number pointer are pushed back by up to $ 1 $ operations, so $ m \ le 2n $.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=1500150;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
struct item{
    int s,id;
    bool operator<(const item &i)const{return s<i.s;}
}it[maxn];
int n,s[maxn],t[maxn],d[maxn],cur=1,m,a[maxn],b[maxn],c[maxn];
ll sum;
int main(){
    n=read();
    FOR(i,1,n) s[i]=read(),it[i]=(item){s[i],i};
    FOR(i,1,n) t[i]=read();
    sort(it+1,it+n+1);sort(t+1,t+n+1);
    FOR(i,1,n) d[i]=t[i]-it[i].s,sum+=d[i];
    if(sum) return puts("NO"),0;
    FOR(i,1,n){
        if(d[i]<0) return puts("NO"),0;
        while(d[i]){
            while(cur<=n && d[cur]>=0) cur++;
            int x=min(d[i],-d[cur]);
            d[i]-=x;d[cur]+=x;
            a[++m]=it[i].id;b[m]=it[cur].id;c[m]=x;
        }
    }
    printf("YES\n%d\n",m);
    FOR(i,1,m) printf("%d %d %d\n",a[i],b[i],c[i]);
}
View Code

F,G

$ A $ 2500, a $ 3600 $ (3600 $ $ first-ever title track), do not complete. Then later fill it.

 

Guess you like

Origin www.cnblogs.com/1000Suns/p/10961877.html