Codeforces Global Round 3

本文介绍了编程竞赛中的一系列问题及其解决方案。首先是一个关于如何构造最长好字符串的问题,涉及字符串处理和组合优化。接着是关于航班连接时间的优化问题,需要在限制取消航班数量的情况下找到最晚到达时间。接下来是一个关于排序和操作模拟的问题,要求在不超过一定操作次数的条件下,使排列变得有序。然后是另一道排序题目,需要根据特定条件调整序列。最后是关于图形操作的题目,需要找到一种方式改变物体价格的符号。每个问题都提供了题意、题解和样例输入输出,展示了如何运用不同的算法和策略解决竞赛中的挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,如果num1<num2,ans=2num1+1,如果num1==num2,ans=2*num1

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
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<a2<…<an≤109) — the times the flights from A to B depart.

The third line contains m distinct integers in increasing order b1, b2, b3, …, bm (1≤b1<b2<…<bm≤109) — the times the flights from B to C depart.

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<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
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<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
int a[300005],b[300005],c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值