Codeforces Global Round 3

本文解析了两道算法竞赛题目,包括字符串组合问题和航班调度优化问题。提供了详细的解题思路和AC代码,对于理解字符串操作和贪心算法具有一定的帮助。

同样还是补题 老年人晚上熬不起夜(其实是那天去找女朋友玩了回来晚了

A. Another One Bites The Dust

题意 给出a个"a" b个"b" c个"ab" 然后能组成如"ababa"形串的最大长度

水题 如果a == b的话 就刚好按若干个ab的顺序排 即最长长度为(a + c) * 2

a != b的话 也是按若干个ab排 但多出来的一个a可以排在最后一个b后面

同理 多出来的一个b可以排在最先一个a前面 即最长长度(min(a, b) + c) * 2 + 1

AC代码:

 1 #include<bits/stdc++.h>
 2 #define pi acos(-1)
 3 typedef long long ll;
 4 typedef unsigned long long ull;
 5 using namespace std;
 6 
 7 namespace io {
 8     const int SIZE = 1e7 + 10;
 9     char inbuff[SIZE];
10     char *l, *r;
11     inline void init() {
12         l = inbuff;
13         r = inbuff + fread(inbuff, 1, SIZE, stdin);
14     }
15     inline char gc() {
16         if (l == r) init();
17         return (l != r) ? *(l++) : EOF;
18     }
19     void read(int &x) {
20         x = 0; char ch = gc();
21         while(!isdigit(ch)) ch = gc();
22         while(isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
23     }
24 } using io::read;
25 
26 bool cmp(const int &a, const int &b){
27     return a > b;
28 }
29 
30 int main(){
31     ll a, b, c;
32     cin>>a>>b>>c;
33     if (a == b) cout<<(a + c) * 2<<endl;
34     else cout<<(min(a, b) + c) * 2 + 1<<endl;
35     return 0;
36 }

 

B. Born This Way

题意 某个人要买张从A到C的机票 但是上帝并不想让他这么顺利(瞎编的)

他只能从A飞到B 再从B飞到C

给出n趟从A到B的飞机起飞时刻与m趟从B到C的飞机起飞时刻还有A到B的飞机飞行时间ta和B到C的飞机飞行时间tb

你现在要千方百计让这个人最晚时间到达目的地 你能取消小于等于k趟航班

若取消后不能到达 输出-1 否则 输出最晚的时间

先直接把a数组都加上ta 再跟b数组的每一项比较 注意特判n <= k或m <= k时的情况

AC代码:

 1 #include<bits/stdc++.h>
 2 #define pi acos(-1)
 3 typedef long long ll;
 4 typedef unsigned long long ull;
 5 using namespace std;
 6 
 7 namespace io {
 8     const int SIZE = 1e7 + 10;
 9     char inbuff[SIZE];
10     char *l, *r;
11     inline void init() {
12         l = inbuff;
13         r = inbuff + fread(inbuff, 1, SIZE, stdin);
14     }
15     inline char gc() {
16         if (l == r) init();
17         return (l != r) ? *(l++) : EOF;
18     }
19     void read(int &x) {
20         x = 0; char ch = gc();
21         while(!isdigit(ch)) ch = gc();
22         while(isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
23     }
24 } using io::read;
25 
26 bool cmp(const int &a, const int &b){
27     return a > b;
28 }
29 
30 const int N = 2e5 + 5;
31 ll n, m, ta, tb, k;
32 ll a[N], b[N];
33 ll ans;
34 
35 int main(){
36     cin>>n>>m>>ta>>tb>>k;
37     bool flag = true;
38     for (int i = 1; i <= n; i++){
39         cin>>a[i];
40         a[i] += ta;
41     }
42     for (int i = 1; i <= m; i++) cin>>b[i];
43     for (int i = 0; i <= k; i++){
44         int t = lower_bound(b + 1, b + 1 + m, a[i + 1]) - b + k - i;
45         if (t > m){
46             flag = false;
47             break;
48         }
49         ans = max(ans, b[t] + tb);
50     }
51     if (n <= k || m <= k) flag = false;
52     if (flag) cout<<ans<<endl;
53     else cout<<-1<<endl;
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/Misuchii/p/10969610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值