题目链接: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. 查看一个package是否在执行

    select a.type, a.owner, b.SID, b.SERIAL#, b.OSUSER, b.MACHINE, b.PROGRAM, b.MODULE, b.ACTION From db ...

  2. MySQL之查询操作

    1)使用in查询, 保持顺序 SELECT * FROM `template_data` where template_id in (7339747298123169843,7339747324194 ...

  3. Linux只gz文件格式压缩与解压缩

    压缩成gz gzip * gzip file 具体参数可使用help查看 解压缩gz gunzip * gunzip file.gz 具体也是可以用help查看

  4. nginx配置之Gzip压缩

    Nginx开启Gzip压缩功能, 可以使网站的css.js .xml.html 文件在传输时进行压缩,提高访问速度, 进而优化Nginx性能!  Web网站上的图片,视频等其它多媒体文件以及大文件,因 ...

  5. Nginx之yum安装

    使用yum进行nginx的安装 参考官网:http://nginx.org/ 1)选择对应系统,例如自己使用的是centos系统 2)配置并安装 sudo yum install yum-utils ...

  6. AI工具推荐——Cherry Studio

    Cherry Studio介绍 Cherry Studio是一款支持多模型服务的 Windows/macOS GPT 客户端. 它的主要特点如下: 多样化的大型语言模型提供商支持 ️ 主要的大型语言模 ...

  7. PM2部署DotNet应用程序

    pm2简介 PM2是一个Node.js的进程管理工具,可以帮助开发者简化Node.js应用的部署和运维.它提供了进程守护.负载均衡.日志管理等功能,可以监控应用程序的运行状态,并在发生意外情况时自动重 ...

  8. 鸿蒙UI开发快速入门 —— part05:组件的样式复用

    1. 为什么要样式复用? 如果每个组件的样式都需要单独设置,在开发过程中会出现大量代码在进行重复样式设置,虽然可以复制粘贴,但为了代码简洁性和后续方便维护,样式的复用就很有必要了. 为此,鸿蒙推出了可 ...

  9. C/C++实例汇集(1)

    1.用代码判断一个系统是16位系统还是32位系统? 以下是几种常见编程语言中判断系统是 16 位还是 32 位的代码示例 C语言: #include <stdio.h> int main( ...

  10. Spring Boot + K8S 中的滚动发布、优雅停机、弹性伸缩、应用监控、配置分离

    前言 K8s + SpringBoot实现零宕机发布:健康检查+滚动更新+优雅停机+弹性伸缩+Prometheus监控+配置分离(镜像复用) 配置 健康检查 健康检查类型:就绪探针(readiness ...