Codeforces Global Round 3

A. Another One Bites The Dust
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s call a string good if and only if it consists of only two types of letters — ‘a’ and ‘b’ and every two consecutive letters are distinct. For example “baba” and “aba” are good strings and “abb” is a bad string.

You have a strings “a”, b strings “b” and c strings “ab”. You want to choose some subset of these strings and concatenate them in any arbitrarily order.

What is the length of the longest good string you can obtain this way?

Input
The first line contains three positive integers a, b, c (1≤a,b,c≤109) — the number of strings “a”, “b” and “ab” respectively.

Output
Print a single number — the maximum possible length of the good string you can obtain.

Examples
inputCopy
1 1 1
outputCopy
4
inputCopy
2 1 2
outputCopy
7
inputCopy
3 5 2
outputCopy
11
inputCopy
2 2 1
outputCopy
6
inputCopy
1000000000 1000000000 1000000000
outputCopy
4000000000
Note
In the first example the optimal string is “baba”.

In the second example the optimal string is “abababa”.

In the third example the optimal string is “bababababab”.

In the fourth example the optimal string is “ababab”.

题意:定义一个好串任意两个相邻的字母都不一样,给出"a" “b” "ab"的数量num1,num2,num3,求好串的最长长度
题解:分类讨论,如果num1>num2,ans=2num2+1,如果num1num1+1,如果num1==num2,ans=2*num1

#include
using namespace std;
#define debug(x) cout<<#x<<" is "<
typedef long long ll;
int main(){
    ll a,b,c;
    cin>>a>>b>>c;
    ll x;
    if(a>b)x=2*b+1;
    else if(a<b)x=a*2+1;
    else x=a*2;
    cout<<c*2+x<<endl;
    return 0;
}

B. Born This Way
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C.

There are n flights from A to B, they depart at time moments a1, a2, a3, …, an and arrive at B ta moments later.

There are m flights from B to C, they depart at time moments b1, b2, b3, …, bm and arrive at C tb moments later.

The connection time is negligible, so one can use the i-th flight from A to B and the j-th flight from B to C if and only if bj≥ai+ta.

You can cancel at most k flights. If you cancel a flight, Arkady can not use it.

Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel k flights. If you can cancel k or less flights in such a way that it is not possible to reach C at all, print −1.

Input
The first line contains five integers n, m, ta, tb and k (1≤n,m≤2⋅105, 1≤k≤n+m, 1≤ta,tb≤109) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively.

The second line contains n distinct integers in increasing order a1, a2, a3, …, an (1≤a1

The third line contains m distinct integers in increasing order b1, b2, b3, …, bm (1≤b1

Output
If you can cancel k or less flights in such a way that it is not possible to reach C at all, print −1.

Otherwise print the earliest time Arkady can arrive at C if you cancel k flights in such a way that maximizes this time.

Examples
inputCopy
4 5 1 1 2
1 3 5 7
1 2 3 9 10
outputCopy
11
inputCopy
2 2 4 4 2
1 10
10 20
outputCopy
-1
inputCopy
4 3 2 3 1
1 999999998 999999999 1000000000
3 4 1000000000
outputCopy
1000000003
Note
Consider the first example. The flights from A to B depart at time moments 1, 3, 5, and 7 and arrive at B at time moments 2, 4, 6, 8, respectively. The flights from B to C depart at time moments 1, 2, 3, 9, and 10 and arrive at C at time moments 2, 3, 4, 10, 11, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment 4, and take the last flight from B to C arriving at C at time moment 11.

In the second example you can simply cancel all flights from A to B and you’re done.

In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.

题意:给出a->b车站各个车的发车时间ai以及b->c车站各个车的发车时间bi,已知从a->b需要行驶时间A,b->c需要行驶时间为B,你可以阻止k辆车的发车,求最晚到达时间
题解:将a->b的车辆按照发车时间排序,如果阻断a->b的车辆,那么容易发现必须阻断从1到i的连续车辆从而控制发车时间较晚,所以可以直接枚举i,然后lowerbound求出最晚时间

#include
using namespace std;
#define debug(x) cout<<#x<<" is "<
typedef long long ll;
ll w[200005],ww[200005];
int main(){
    ll n,m,a,b,k;
    scanf("%lld%lld%lld%lld%lld",&n,&m,&a,&b,&k);
    for(int i=1;i<=n;i++){
        scanf("%lld",&w[i]);
    }
    for(int i=1;i<=m;i++){
        scanf("%lld",&ww[i]);
    }
    int tot=0;
    ll ans=-1;
    for(int i=1;i<=n&&i<=k+1;i++){
        int pos=lower_bound(ww+1,ww+1+m,w[i]+a)-ww;
        if(pos+k-i+1>m){
            ans=-1;
            break;
        }
        ans=max(ww[pos+k-i+1]+b,ans);
    }
    if(k>=n||k>=m)ans=-1;
    printf("%lld\n",ans);
    return 0;
}

C. Crazy Diamond
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a permutation p of integers from 1 to n, where n is an even number.

Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:

take two indices i and j such that 2⋅|i−j|≥n and swap pi and pj.
There is no need to minimize the number of operations, however you should use no more than 5⋅n operations. One can show that it is always possible to do that.

Input
The first line contains a single integer n (2≤n≤3⋅105, n is even) — the length of the permutation.

The second line contains n distinct integers p1,p2,…,pn (1≤pi≤n) — the given permutation.

Output
On the first line print m (0≤m≤5⋅n) — the number of swaps to perform.

Each of the following m lines should contain integers ai,bi (1≤ai,bi≤n, |ai−bi|≥n2) — the indices that should be swapped in the corresponding swap.

Note that there is no need to minimize the number of operations. We can show that an answer always exists.

Examples
inputCopy
2
2 1
outputCopy
1
1 2
inputCopy
4
3 4 1 2
outputCopy
4
1 4
1 4
1 3
2 4
inputCopy
6
2 5 3 1 4 6
outputCopy
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.

In the second example, pay attention that there is no need to minimize number of swaps.

In the third example, after swapping elements on positions 1 and 5 the array becomes: [4,5,3,1,2,6]. After swapping elements on positions 2 and 5 the array becomes [4,2,3,1,5,6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1,2,3,4,5,6].

题意:模拟
题解:按照题意模拟即可

#include
using namespace std;
#define debug(x) cout<<#x<<" is "<
typedef long long ll;
int a[300005],b[300005],c[1500005],d[1500005];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[a[i]]=i;
    }
    int tot=0;
    if(b[1]!=1){
        if(abs(b[1]-1)*2>=n){
            c[++tot]=1;
            d[tot]=b[1];
        }
        else{
            c[++tot]=n;
            d[tot]=b[1];
            c[++tot]=1;
            d[tot]=n;
            c[++tot]=b[1];
            d[tot]=n;
        }
        int www=b[1];
        b[1]=1;
        b[a[1]]=www;
        a[www]=a[1];
        a[1]=1;
    }
    for(int i=2;i<=n;i++){
        if(b[i]==i)continue;
        if(abs(b[i]-1)*2>=n){
            c[++tot]=b[i];
            d[tot]=1;
            if(abs(i-1)*2>=n){
                c[++tot]=1;
                d[tot]=i;
                c[++tot]=b[i];
                d[tot]=1;
            }
            else{
                c[++tot]=n;
                d[tot]=1;
                c[++tot]=n;
                d[tot]=i;
                c[++tot]=n;
                d[tot]=1;
                c[++tot]=b[i];
                d[tot]=1;
            }
        }
        else{
            c[++tot]=b[i];
            d[tot]=n;
            if(abs(i-n)*2>=n){
                c[++tot]=n;
                d[tot]=i;
                c[++tot]=b[i];
                d[tot]=n;
            }
            else{
                c[++tot]=n;
                d[tot]=1;
                c[++tot]=1;
                d[tot]=i;
                c[++tot]=n;
                d[tot]=1;
                c[++tot]=b[i];
                d[tot]=n;
            }
        }
        int www=b[i];
        b[i]=i;
        b[a[i]]=www;
        int qwq=a[www];
        a[www]=a[i];
        a[i]=qwq;
    }
    printf("%d\n",tot);
    for(int i=1;i<=tot;i++){
        printf("%d %d\n",c[i],d[i]);
    }
    return 0;
}

D. Dirty Deeds Done Dirt Cheap
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n pairs of integers (a1,b1),(a2,b2),…,(an,bn). All of the integers in the pairs are distinct and are in the range from 1 to 2⋅n inclusive.

Let’s call a sequence of integers x1,x2,…,x2k good if either

x1x3<…x2k−1 x1>x2…>x2k−2x2k.
You need to choose a subset of distinct indices i1,i2,…,it and their order in a way that if you write down all numbers from the pairs in a single sequence (the sequence would be ai1,bi1,ai2,bi2,…,ait,bit), this sequence is good.

What is the largest subset of indices you can choose? You also need to construct the corresponding index sequence i1,i2,…,it.

Input
The first line contains single integer n (2≤n≤3⋅105) — the number of pairs.

Each of the next n lines contain two numbers — ai and bi (1≤ai,bi≤2⋅n) — the elements of the pairs.

It is guaranteed that all integers in the pairs are distinct, that is, every integer from 1 to 2⋅n is mentioned exactly once.

Output
In the first line print a single integer t — the number of pairs in the answer.

Then print t distinct integers i1,i2,…,it — the indexes of pairs in the corresponding order.

Examples
inputCopy
5
1 7
6 4
2 10
9 8
3 5
outputCopy
3
1 5 3
inputCopy
3
5 4
3 2
6 1
outputCopy
3
3 2 1
Note
The final sequence in the first example is 1<7>3<5>2<10.

The final sequence in the second example is 6>1<3>2<5>4.

题意:…
题解:排序

#include
using namespace std;
#define debug(x) cout<<#x<<" is "<
typedef long long ll;
struct pot{
    int a;
    int b;
    int id;
}p[300005];
struct pot2{
    int a;
    int b;
    int id;
}p2[300005];
bool cmp(struct pot aa,struct pot bb){
    return aa.b<bb.b;
}
bool cmp2(struct pot2 aa,struct pot2 bb){
    return aa.a>bb.a;
}
int main(){
    int n;
    scanf("%d",&n);
    int tot=0;
    int tot2=0;
    for(int i=1;i<=n;i++){
        int w,q;
        scanf("%d%d",&w,&q);
        if(w>q){
            p[++tot].a=w;
            p[tot].b=q;
            p[tot].id=i;
        }
        else if(w<q){
            p2[++tot2].a=w;
            p2[tot2].b=q;
            p2[tot2].id=i;
        }
    }
    if(tot>tot2){
        sort(p+1,p+1+tot,cmp);
        int ans=max(tot,tot2);
        printf("%d\n",ans);
        for(int i=1;i<=ans;i++){
            printf("%d",p[i].id);
            char cc=(i==ans)?'\n':' ';
            printf("%c",cc);
        }
    }
    else{
        sort(p2+1,p2+tot2+1,cmp2);
        int ans=max(tot,tot2);
        printf("%d\n",ans);
        for(int i=1;i<=ans;i++){
            printf("%d",p2[i].id);
            char cc=(i==ans)?'\n':' ';
            printf("%c",cc);
        }
    }
    return 0;
}

E. Earth Wind and Fire
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n stones arranged on an axis. Initially the i-th stone is located at the coordinate si. There may be more than one stone in a single place.

You can perform zero or more operations of the following type:

take two stones with indices i and j so that si≤sj, choose an integer d (0≤2⋅d≤sj−si), and replace the coordinate si with (si+d) and replace coordinate sj with (sj−d). In other words, draw stones closer to each other.
You want to move the stones so that they are located at positions t1,t2,…,tn. The order of the stones is not important — you just want for the multiset of the stones resulting positions to be the same as the multiset of t1,t2,…,tn.

Detect whether it is possible to move the stones this way, and if yes, construct a way to do so. You don’t need to minimize the number of moves.

Input
The first line contains a single integer n (1≤n≤3⋅105) – the number of stones.

The second line contains integers s1,s2,…,sn (1≤si≤109) — the initial positions of the stones.

The second line contains integers t1,t2,…,tn (1≤ti≤109) — the target positions of the stones.

Output
If it is impossible to move the stones this way, print “NO”.

Otherwise, on the first line print “YES”, on the second line print the number of operations m (0≤m≤5⋅n) required. You don’t have to minimize the number of operations.

Then print m lines, each containing integers i,j,d (1≤i,j≤n, si≤sj, 0≤2⋅d≤sj−si), defining the operations.

One can show that if an answer exists, there is an answer requiring no more than 5⋅n operations.

Examples
inputCopy
5
2 2 7 4 9
5 4 5 5 5
outputCopy
YES
4
4 3 1
2 3 1
2 5 2
1 5 2
inputCopy
3
1 5 10
3 5 7
outputCopy
NO
Note
Consider the first example.

After the first move the locations of stones is [2,2,6,5,9].
After the second move the locations of stones is [2,3,5,5,9].
After the third move the locations of stones is [2,5,5,5,7].
After the last move the locations of stones is [4,5,5,5,5].
题意:给出n个位置和n个特定位置,要求这n个位置经过若干次操作到达特定位置,操作为两个位置向对方方向移动x,其中x为自己设定的值且每次都不一样,题目保证如果有解最多经过5*n次操作即可完成
题解:把两组n个位置分别排序,每个位置贪心地移动到对应位置即可

#include
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<
struct pot{
    int id;
    int pos;
}p[300005],p2[300005];
int aw[1500005],bw[1500005],cw[1500005];
bool cmp(struct pot aa,struct pot bb){
    return aa.pos<bb.pos;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&p[i].pos);
        p[i].id=i;
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&p2[i].pos);
        p2[i].id=i;
    }
    sort(p+1,p+1+n,cmp);
    sort(p2+1,p2+1+n,cmp);
    int t2=2;
    int t3=2;
    int tot=0;
    for(int i=1;i<=n;i++){
        if(p[i].pos==p2[i].pos){
            continue;
        }
        else{
            if(p[i].pos<p2[i].pos){
                while(1){
                    while(t2<=n&&p[t2].pos<=p2[t2].pos)t2++;
                    if(t2==n+1){
                      //  debug(0000);
                        printf("NO\n");
                        return 0;
                    }
                    else{
                        if(p[t2].pos-p2[t2].pos<p2[i].pos-p[i].pos){
                            aw[++tot]=p[i].id;
                            bw[tot]=p[t2].id;
                            cw[tot]=(p[t2].pos-p2[t2].pos);
                            p[i].pos+=(p[t2].pos-p2[t2].pos);
                            p[t2].pos=p2[t2].pos;
                            t2--;
                        }
                        else if(p[t2].pos-p2[t2].pos==p2[i].pos-p[i].pos){
                            aw[++tot]=p[i].id;
                            bw[tot]=p[t2].id;
                            cw[tot]=(p[t2].pos-p2[t2].pos);
                            p[t2].pos=p2[t2].pos;
                            p[i].pos=p2[i].pos;
                            t2--;
                            break;
                        }
                        else{
                            aw[++tot]=p[i].id;
                            bw[tot]=p[t2].id;
                            cw[tot]=(p2[i].pos-p[i].pos);
                            p[t2].pos-=(p2[i].pos-p[i].pos);
                            p[i].pos=p2[i].pos;
                            break;
                        }
                    }
                }
            }
            else{
                printf("NO\n");
                return 0;
            }
        }
    }
    printf("YES\n");
    printf("%d\n",tot);
    for(int i=1;i<=tot;i++){
        printf("%d %d %d\n",aw[i],bw[i],cw[i]);
    }
    return 0;
}

F. Foo Fighters
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n objects. Each object has two integer properties: vali — its price — and maski. It is guaranteed that the sum of all prices is initially non-zero.

You want to select a positive integer s. All objects will be modified after that. The i-th object will be modified using the following procedure:

Consider maski and s in binary notation,
Compute the bitwise AND of s and maski (s&maski),
If (s&maski) contains an odd number of ones, replace the vali with −vali. Otherwise do nothing with the i-th object.
You need to find such an integer s that when the modification above is done the sum of all prices changes sign (if it was negative, it should become positive, and vice-versa; it is not allowed for it to become zero). The absolute value of the sum can be arbitrary.

Input
The first line contains a single integer n (1≤n≤3⋅105) — the number of objects.

The i-th of next n lines contains integers vali and maski (−109≤vali≤109, 1≤maski≤262−1) — the price of the object and its mask.

It is guaranteed that the sum of vali is initially non-zero.

Output
Print an integer s (1≤s≤262−1), such that if we modify the objects as described above, the sign of the sum of vali changes its sign.

If there are multiple such s, print any of them. One can show that there is always at least one valid s.

Examples
inputCopy
5
17 206
-6 117
-2 151
9 93
6 117
outputCopy
64
inputCopy
1
1 1
outputCopy
1
Note
In the first test sample all objects will change their prices except for the object with mask 151.

So their total sum will change its sign: initially 24, after modifications — −28.

In the second test sample the only object will change its price. So the total sum will change its sign.
题意:对于一个s,如果(s&mask[i])的值二进制下1的个数为奇数个,就可以把对应的val[i]变成-val[i],求出这样一个s,让n组数据的val[i]之和变成原来符号的相反符号,即正变成负,负变成正
题解:可以把求s的过程分解成若干个2^i 相加的过程,通过判断相应位数下的sum是否和原来符号相同来确定是否加这个 2^1,如果符号相同,为了改变符号就要 +2^i

#include
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<
ll a[300005],b[300005];
int c[300005];
int sgn(ll x){
    if(x>0)return 1;
    if(x<0)return -1;
    return 0;
}
int digt(ll x){
    int w=-1;
    while(x){
        w++;
        x>>=1;
    }
    return w;
}
int main(){
    int n;
    scanf("%d",&n);
    ll sum=0;
    for(int i=1;i<=n;i++){
        scanf("%lld%lld",&a[i],&b[i]);
        sum+=a[i];
        c[i]=digt(b[i]);
    }
    ll ans=0;
    for(int i=0;i<62;i++){
        ll x=0;
        for(int j=1;j<=n;j++){
            if(c[j]==i){
                x+=a[j];
            }
        }
        if(sgn(x)==sgn(sum)){
            ans+=(1ll<<i);
            for(int j=1;j<=n;j++){
                if((b[j]>>i)&1)a[j]=-a[j];
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

G. Gold Experience
time limit per test2 seconds
memory limit per test1024 megabytes
inputstandard input
outputstandard output
Consider an undirected graph G with n vertices. There is a value ai in each vertex.

Two vertices i and j are connected with an edge if and only if gcd(ai,aj)>1, where gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y.

Consider a set of vertices. Let’s call a vertex in this set fair if it is connected with an edge with all other vertices in this set.

You need to find a set of k vertices (where k is a given integer, 2⋅k≤n) where all vertices are fair or all vertices are not fair. One can show that such a set always exists.

Input
The first line contains integers n and k (6≤2⋅k≤n≤105) — the number of vertices and parameter k.

The second line contains n integers a1,a2,…,an (2≤ai≤107) — the values in the vertices.

Output
Print exactly k distinct integers — the indices of the vertices in the chosen set in any order.

Examples
inputCopy
6 3
6 15 10 8 14 12
outputCopy
2 4 5
inputCopy
8 4
11 15 10 6 21 15 10 6
outputCopy
5 7 1 2
inputCopy
10 5
3003 17017 3230 49742 546 41990 17765 570 21945 36465
outputCopy
1 2 4 5 6
Note
In the first test case, set {2,4,5} is an example of set where no vertices are fair. The vertex 2 does not share an edge with vertex 4 since gcd(15,8)=1. The vertex 4 does not share an edge with vertex 2. The vertex 5 does not share an edge with vertex 2.

In the second test case, set {8,5,6,4} is an example of a set where all vertices are fair.

待补

H. Holy Diver
time limit per test3 seconds
memory limit per test1024 megabytes
inputstandard input
outputstandard output
You are given an array which is initially empty. You need to perform n operations of the given format:

“a l r k”: append a to the end of the array. After that count the number of integer pairs x,y such that l≤x≤y≤r and mex(ax,ax+1,…,ay)=k.
The elements of the array are numerated from 1 in the order they are added to the array.

To make this problem more tricky we don’t say your real parameters of the queries. Instead your are given a′, l′, r′, k′. To get a, l, r, k on the i-th operation you need to perform the following:

a:=(a′+lans)mod(n+1),
l:=(l′+lans)modi+1,
r:=(r′+lans)modi+1,
if l>r swap l and r,
k:=(k′+lans)mod(n+1),
where lans is the answer to the previous operation, initially lans is equal to zero. i is the id of the operation, operations are numbered from 1.
The mex(S), where S is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, mex({2,2,3})=0 and mex({0,1,4,1,6})=2.

Input
The first line contains a single integer n (1≤n≤2⋅105) — the length of the array.

The next n lines contain the description of queries.

Each of them n lines contains four non-negative integers a′, l′, r′, k′ (0,≤a′,l′,r′,k′≤109), describing one operation.

Output
For each query print a single integer — the answer to this query.

Examples
inputCopy
5
0 0 0 1
0 1 0 5
5 2 1 0
5 2 1 0
2 4 3 3
outputCopy
1
1
2
6
3
inputCopy
5
2 0 0 2
2 0 1 1
0 0 2 0
3 2 2 0
0 2 3 0
outputCopy
0
0
3
0
0
Note
For the first example the decoded values of a, l, r, k are the following:

a1=0,l1=1,r1=1,k1=1
a2=1,l2=1,r2=2,k2=0
a3=0,l3=1,r3=3,k3=1
a4=1,l4=1,r4=4,k4=2
a5=2,l5=1,r5=5,k5=3
For the second example the decoded values of a, l, r, k are the following:

a1=2,l1=1,r1=1,k1=2
a2=2,l2=1,r2=2,k2=1
a3=0,l3=1,r3=3,k3=0
a4=0,l4=2,r4=2,k4=3
a5=0,l5=3,r5=4,k5=0

待补

你可能感兴趣的:(Codeforces Global Round 3)