A. From Hero to Zero

通过取余快速运行第一步即可。由于\(a \% b (a >= b) <= \frac{a}{2}\)。所以总复杂度不超过\(O(log_2n)\)。

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
int main(){
int T; scanf("%d", &T);
while(T--){
LL n, k, ans = 0;
scanf("%lld%lld", &n, &k);
while(n){
if(n % k) ans += n % k, n -= n % k;
if(n)n /= k, ans++;
}
printf("%lld\n", ans);
}
return 0;
}

B. Catch Overflow!

循环本质其实是栈的思想,可以用\(loop\)表示这层要循环多少次。注意如果直接乘可能爆\(long\ long\)。

其实当\(loop > 2 ^ {32} - 1\),后面的只要有\(add\)都不行了,所以只要用个\(flag\)记录就行了...

#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
using namespace std;
typedef long long LL;
const LL INF = (1ll << 32) - 1;
LL n = 0, loop = 1;
stack<int> s;
string ch;
int x, flag = 0;
int main(){
ios::sync_with_stdio(false);
int T; cin >> T;
while(T--){
cin >> ch;
if(ch == "add"){
if(loop > INF){
cout << "OVERFLOW!!!" << endl;
return 0;
}
n += loop;
}else if(ch == "for"){
cin >> x;
if(loop > INF) {
flag++;
continue;
}
s.push(x);
loop *= x;
}else if(ch == "end"){
if(flag) {
flag --;
continue;
}
loop /= s.top();
s.pop();
}
if(n > INF){
cout << "OVERFLOW!!!" << endl;
return 0;
}
}
cout << n << endl;
return 0;
}

C. Electrification

注意数据是单调递增的,所以容易想到最短的一段必然是连续的,因为通过绝对值变成正的值顺序一定是\(1, 2, 3....n\),所以每次变成正的后,它可能是最小的,也有可能大于后面的一些,因为减的多了,所以整个区间会往后移动。我们可以尝试枚举每一个\([i, i + k]\)的这个区间,发现能使最小的即变为\((a[i + k] - a[i]) / 2\),也就是全部都减\((a[i + k] - a[i]) / 2\)(可以向下取整)。可以用小根堆维护最小值。由于精度问题,第一个可以不用\(/2\)。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
const int N = 200010;
int n, k, a[N];
priority_queue<PII, vector<PII>, greater<PII> > q;
int main(){
int T; scanf("%d", &T);
while(T--){
while(q.size()) q.pop();
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d", a + i);
for(int i = 1; i + k <= n; i++)
q.push(make_pair(a[i + k] - a[i], (a[i] + a[i + k]) / 2));
printf("%d\n", q.top().second);
}
return 0;
}

D. Array Splitting

非常巧妙的做法,观察到让我们求的值其实是每一段的和$ * $ 相应的段数,但其实可以发现,一个\(ans\)的组成为:

\(1 * A + 2 * B + 3 * C = (A + B + C) + (B + C) + C\)。实质上就是找到\(k\)个后缀和,将他们加起来求最大值。这样直接贪心求解即可,记得\([1, n]\)这个区间必须选。

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 300010;
typedef long long LL;
int n, k, a[N];
LL ans, sum[N];
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%d", a + i);
for(int i = n; i; i--)
sum[i] = sum[i + 1] + a[i];
sort(sum + 2, sum + 1 + n);
ans = sum[1];
for(int i = n; i >= n - k + 2; i--) ans += sum[i];
printf("%lld\n", ans);
return 0;
}

E. Minimal Segment Cover

注意到\(l\)和\(r\)的范围并不大,我们可以预处理出从\(l\)出发最远能到哪里(一条线段),注意\(l\)也可以是中间点。这样我们只需要逐个枚举即可。时间复杂度\(SIZE ^ 2\),可以用倍增的形式优化。具体方式很像\(LCA\),预处理每个\(l\)跳\(2 ^ p(0 <= p <= \lfloor log_2500000 \rfloor)\)能到哪里即可。本质上就是枚举答案的二进制位即可。

时间复杂度\(O(slog_2s)\)(\(s\)是数字范围)

注意预处理顺序,要么\(i\)倒序枚举,要么\(j\)在第一个条件,否则没有正确的转移。

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 200010, SIZE = 500010, L = 19;
int n, m, maxS = -1, f[SIZE][L];
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
int l, r; scanf("%d%d", &l, &r);
f[l][0] = max(f[l][0], r);
maxS = max(maxS, r);
}
for(int i = 1; i <= maxS; i++)
f[i][0] = max(f[i][0], f[i - 1][0]); for(int j = 1; j < L; j++)
for(int i = 0; i <= maxS; i++)
f[i][j] = f[f[i][j - 1]][j - 1]; for(int i = 1; i <= m; i++){
int x, y, ans = 0; scanf("%d%d", &x, &y);
for(int i = L - 1; ~i; i--)
if(f[x][i] < y) x = f[x][i], ans |= 1 << i;
if(f[x][0] >= y) printf("%d\n", ans + 1);
else puts("-1");
}
return 0;
}

Codeforces Edu Round 66 A-E的更多相关文章

  1. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  2. Educational Codeforces Round 66 差G

    Educational Codeforces Round 66 F 题意:长度为 n 的序列,求有多少个区间 \([l,r]\) ,使得其构成了一个 1~r-l+1 的排列. \(n \le 3*10 ...

  3. Codeforces Beta Round #61 (Div. 2)

    Codeforces Beta Round #61 (Div. 2) http://codeforces.com/contest/66 A 输入用long double #include<bit ...

  4. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  8. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  9. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

随机推荐

  1. C#高级编程之泛型一(泛型的引入、泛型的使用、何为泛型)

    为何引入泛型 当我们要对不同类型的参数执行类似的方法时:如下所示功能打印传入参数的相关信息. class CommonMethdod { /// <summary> /// show in ...

  2. 早期javac编译器优化

    学习<深入了解Java虚拟机>有一段时间了,大概理解了Java从源代码编译到执行出结果的过程,也能明确的知道Java是半解释性语言.在执行源代码时,先通过Javac编译器对源代码进行词法分 ...

  3. [原题复现]BJDCTF2020 WEB部分全部解

    简介  原题复现:https://gitee.com/xiaohua1998/BJDCTF2020_January  线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学 ...

  4. day02-多任务(进程和协程)

    一.多任务的概念       简单地说,就是操作系统可以同时运行多个任务.打个比方,你一边在用浏览器上网,一边在听MP3,一边在用Word赶作业,这就是多任务,至少同时有3个任务正在运行.还有很多任务 ...

  5. Markdown进阶

    ### 事项清单 - [x] 拖地 - [x] 擦窗 - [ ] 写作业 - [ ] 交资料 效果 事项清单 [x] 拖地 [x] 擦窗 [ ] 写作业 [ ] 交资料 流程图 graph LR A[ ...

  6. unctf2020 部分简单题题解

    unctf2020 水一波简单题..菜狗前来报道..大佬轻喷..如果有时间做题就好了呜呜呜 misc 1.baba_is_you 题目告诉我们,了解png文件格式. 下载得到一张png格式的图片. 用 ...

  7. Guitar Pro的10个非常实用的技巧(上)

    Guitar Pro 7具有许多功能和编辑选项,只需点击几下即可随时创建与编辑我们的乐谱,.以下就为大家介绍10个Guitar Pro中实用的技巧,可以大大的节省我们的时间. 在开始之前,重要的是要知 ...

  8. Sonar检测Math.abs(new Random().nextInt()) “Use the original value instead”

    今天早上旁边同事喊我看一个Sonar检测出的问题: 当时看了好几眼没觉得这个有太大问题,于是又看了下Sonar建议: 这是说Math.abs()方法使用在数字上面可能返回最小值,觉得这个挺有意思的,于 ...

  9. 实现 Application_Start 和 Application_End

    理解 ASP.NET Core: 实现 Application_Start 和 Application_End 在 ASP.NET 中两个常用的处理节点是 Application_Start() 和 ...

  10. python ddt实现数据驱动

    首先安装ddt模块,命令:pip install ddt 通常情况下,data中的数据按照一个参数传递给测试用例,如果data中含有多个数据,以元组,列表,字典等数据,需要自行在脚本中对数据进行分解或 ...