题目链接:Educational Codeforces Round 168 (Rated for Div. 2)

总结:题目较简单,但是发挥很一般。A,B题一直读假题,卡了半个小时;C题用char存int,难绷了。

A. Strong Password

tag:模拟

void solve() {
string s;
cin >> s; for (int i = 1; i < s.size(); i ++) {
if (s[i] == s[i - 1]) {
cout << s.substr(0, i) << (s[i] == 'a' ? 'b' : 'a') << s.substr(i) << "\n";
return;
}
}
s += s.back() == 'a' ? 'b' : 'a';
cout << s << "\n";
}

B. Make Three Regions

tag:模拟

void solve() {
int n;
cin >> n; string s[2];
cin >> s[0] >> s[1]; int ans = 0;
for (int i = 1; i < n - 1; i ++) {
for (int j = 0; j < 2; j ++) {
if (s[j][i] == '.' && s[j][i - 1] == '.' && s[j][i + 1] == '.' && s[j ^ 1][i] == '.' && s[j ^ 1][i - 1] == 'x' && s[j ^ 1][i + 1] == 'x') {
ans ++;
}
}
}
cout << ans << "\n";
}

C. Even Positions

tag:思维

Description:给定一个括号序列,奇数位用\(\_\)表示,对于一个括号序列的花费为\((xxxx)\):\(r - l\),你需要将其还原为花费最少的合法序列。

Solution:先考虑如何还原,我们定义\(x\)为当前\((\)的数量,\(y\)为\()\)的数量,当\(x <= y\)时,我们填\((\),否则填\()\)。

  • 考虑如何计算答案,使用\(stack\),存储\((\)的下标,遇到\()\)时,弹出栈顶比计算答案。
void solve(){
cin >> n;
string s;
cin >> s;
int x = 0, y = 0;
s = "$" + s;
for (int i = 1; i <= n; i ++){
if (s[i] == '(')
x ++;
else if (s[i] == ')')
y ++;
else{
if (x <= y)
s[i] = '(', x ++;
else
s[i] = ')', y ++;
}
}
int ans = 0;
stack<int> st;
for (int i = 1; i <= n; i ++){
if (s[i] == '('){
st.ep(i);
continue;
}
else{
int t = st.top();
st.pop();
ans += (i - t);
}
} cout << ans << endl;
}

D. Maximize the Root

tag:dfs

Description:给定一个由\(n\)个节点组成的树,每个点的权值为\(a_i\)。可以执行任意次一下操作:

  • 选择一个有子节点的节点\(v\),将\(v\)加\(1\),其余所有子节点\(-1\)。但是所有值必须大于等于零。
  • 求根节点的最大可能值。

Solution:模拟操作发现,对于非根节点的最大值

  • 如果其值大于所有子节点,那么该值的贡献为子节点中的最小值。

  • 如果该值小于子节点中的最小值,那么该值最大贡献为\((a_i + mi) / 2\)。

  • 对于根节点,只需要加上其子节点的最小值即可。

void solve(){
cin >> n;
vector g(n + 1, vector<int>());
vector<int> a(n + 1); for (int i = 1; i <= n; i ++)
cin >> a[i];
for (int i = 2; i <= n; i ++){
int x;
cin >> x;
g[x].eb(i);
g[i].eb(x);
} auto dfs = [&](auto dfs, int v, int u) -> int{
int res = a[v];
int t = -1;
for (auto i : g[v]){
if (i == u)
continue;
if (t == -1)
t = dfs(dfs, i, v);
else
t = min(t, dfs(dfs, i, v));
}
if (v == 1){
return a[1] += t;
}
if (t == -1)
return res;
if (t <= res)
return a[v] = t;
else{
return a[v] = (res + t) / 2;
}
};
cout << dfs(dfs, 1, -1) << endl;
}

E. Level Up

tag:二分 + 树状数组

Description:有一个长度为\(n\)的序列\(a\),和整数\(x, k\),初始时\(x == 1\)。

  • 一次操作为:从\(1 -> n\)一次判断,每\(k\)次满足\(a_i >= x\),\(x ++\)。

  • 现在有\(q\)次询问,每次询问给定\(i, t\),求当\(k == t\),操作执行到\(i\)时,\(a_i\)是否大于等于\(x\)。

  • \(1 <= n, q <= 2*10^5, 1 <= a_i <= 2*10^5\)

Solution:没有修改操作,首先考虑预处理

  • 我们先考虑\(k\)与\(x\)的关系,显然\(k\)越小\(x\)越大,那么对于每一个\(a_i\),我们可以二分得到一个最小的\(b_i\),使得\(k == b_i\)时,\(a_i >= x\)。但是对每个数处理的时间复杂度为\(O(nlogn)\),总的时间复杂度为\(O(n^2logn)\)
  • 考虑如果优化check函数,对于每一个\(mid\),我们需要知道前面\(i - 1\)个数执行的操作次数。并且我们已经得到\(b_1到b_{i - 1}\),如果\(mid >= b_j\),那么在第\(j\)个数就需要执行操作。因此我们使用树状数组维护前\(i - 1\)个怪物的\(b_i\)值。
  • 对于每一个\(mid\),我们只需要知道前面有多少个\(b_i\)小于等于\(mid\)即可。
int tr[N];

int lowbit(int x){
return x & -x;
} void add(int x, int c){
for (int i = x; i <= n; i += lowbit(i))
tr[i] += c;
} LL sum(int x){
LL res = 0;
for (int i = x; i > 0; i -= lowbit(i))
res += tr[i];
return res;
} LL aks(int l, int r){
return sum(r) - sum(l - 1);
} void solve(){
int q;
cin >> n >> q;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i ++){
cin >> a[i];
} for (int i = 1; i <= n; i ++){ // 初始化
int l = 0, r = i + 1;
while (l + 1 < r){
int mid = l + r >> 1;
if (sum(mid) / mid + 1 > a[i])
l = mid;
else
r = mid;
}
b[i] = r;
add(b[i], 1);
} while (q --){
int i, x;
cin >> i >> x;
if (x < b[i])
cout << "NO\n";
else
cout << "YES\n";
}
}

Educational Codeforces Round 168 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. 霍夫丁(Hoeffding)不等式证明

    马尔可夫不等式 结论 对于任意非负随机变量$X$,$\forall \epsilon>0$,有: $\displaystyle P(X\ge\epsilon)\le\frac{E(X)}{\ep ...

  2. Liunx-Shell脚本

    shell可以理解为对命令行的一个解释器,命令行输入命令,shell执行,linux系统输出结果 1. shell脚本格式 开头: #!/bin/bash #!告诉系统其后路径所指定的程序即是解释此脚 ...

  3. python之常用开发包

    1.passlib (https://passlib.readthedocs.io/en/stable/) passlib 目前常见的不可逆加密算法有以下几种: 一次MD5(使用率很高) 将密码与一个 ...

  4. Python3之常用包汇总

    Python包网站: https://pypi.org/ 1. 繁体与简体转换(https://github.com/berniey/hanziconv.git) pip install hanzic ...

  5. 记一次 .NET某hdp智能柜系统 卡死分析

    一:背景 1. 讲故事 停了一个月时间没有更新博客了,主要是这段时间有些许事情导致心神不宁,我这个人也比较浮躁所以无法潜心修炼,事情如下: 被狗咬了 也不知道是不是出门没看黄历,在小区门口店里买烟,被 ...

  6. 小白PDF阅读器重排版时的自动提取背景色功能介绍及实现

    小白PDF阅读器在1.35之前的版本对于有深色背景的页面重拍版时并不太完美.对于深色背景区域主要表现在不能分割排版和重排后页面元素割裂感明显.小白PDF阅读器在1.35版本主要针对这两个问题进行了优化 ...

  7. js 实现可缓存方法

    1.概述 有些场景下,如果一些函数需要大量的运算,但是他们的传入的参数是一样的,这个时候,我们可以将这些运算缓存下来,之后的运算就可以不用重复计算了. 2.实现方法 <script> // ...

  8. .NET Core 锁(Lock)底层原理浅谈

    CPU原子操作 原子操作,指一段逻辑要么全部成功,要么全部失败.概念上类似数据库事物(Transaction). CPU能够保证单条汇编的原子性,但不保证多条汇编的原子性 那么在这种情况下,那么CPU ...

  9. 百度地图 自定义弹窗 InfoBox

    infoBox文档地址: https://api.map.baidu.com/library/InfoBox/1.2/docs/symbols/BMapLib.InfoBox.html infobox ...

  10. PDFsharp 1.50

    PDFsharp 1.50 Preview Information - PDFsharp & MigraDoc PDFShapr 1.50 修复与改进 支持 Object Streams - ...