Educational Codeforces Round 128 (Rated for Div. 2) A-C+E
Educational Codeforces Round 128 (Rated for Div. 2) A-C+E
A
题目
https://codeforces.com/contest/1680/problem/A
题解
思路
知识点:思维。
如果 \([l1,r1],[l2,r2]\) 有交集可以是相同的数字,那么取 \(min(l1,l2)\) ;如果 \([l1,r1],[l2,r2]\) 没有交集,说明最大值最小值不能是相同的数字,那么取 \(l1+l2\) 。
直接判断端点可能太多,可以利用 \(swap\) 考虑固定 \(l1<l2\) ,就剩下两种情况。
时间复杂度 \(O(1)\)
空间复杂度 \(O(1)\)
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
if(l2<l1){
swap(l1,l2);
swap(r1,r2);
}
if(r1>=l2) cout<<l2<<'\n';
else cout<<l1+l2<<'\n';
}
return 0;
}
B
题目
https://codeforces.com/contest/1680/problem/B
题解
思路
知识点:思维。
找到机器人中最靠左上的行列坐标(不一定要在同一个机器人身上),如此坐标表示了机器人阵列移动多少次就会出现爆炸。
如果这组行列坐标的位置没有机器人,说明移动到爆炸极限之前都没有机器人会到达左上角,因此不可行,否则可行。
时间复杂度 \(O(nm)\)
空间复杂度 \(O(mn)\)
代码
#include <bits/stdc++.h>
using namespace std;
bool bot[7][7];
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
int minx = 10,miny = 10;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
char tmp;
cin>>tmp;
if(tmp == 'R'){
bot[i][j] = 1;
minx = min(minx,i);
miny = min(miny,j);
}
else bot[i][j] = 0;
}
}
if(bot[minx][miny]) cout<<"YES"<<'\n';
else cout<<"NO"<<'\n';
}
return 0;
}
C
题目
https://codeforces.com/contest/1680/problem/C
题解
思路
方法1
知识点:尺取法。
注意到 \(0,1\) 变化具有单调性,左端点变大一定导致 \(0\) 在子串的数量 \(A\) 变少且移除 \(1\) 的数量 \(B\) 变多,而右端点变大则相反。那么对于一个固定了左端点的区间,可以将右端点变大,使 \(A,B\) 分别从较小和较大的值向某个极值靠拢,直到 \(A = B\) 即达到当前左端点的区间的最小 \(cost\) 。此时可以将左端点加一,此举一定会让 \(A \neq B\) ,于是可以继续移动右端点到达新的左端点的最优区间。在上面思考的基础下,枚举左端点即可以,移动右端点到达最优,取每次最小 \(cost\) 的最小值即可。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
方法2
知识点:数学,前缀和。
设子串留有 \(0\) 的数量为 \(A\) ,留有 \(1\) 的数量为 \(B\) ,\(1\) 的总数为 \(sum\) ,则有 \(cost = max(A,sum - B)\)。
再设子串长度为 \(len\) ,则有 \(cost = max(A+B,sum) - B = max(len,sum) - B\)
考虑 \(len \leq sum\) ,则有 \(cost = sum - B\),显然如果 \(len\) 增加,那么 \(B\) 是不减的,因此我们考虑取 \(len = sum\)。
考虑 \(len \geq sum\) ,则有 \(cost = len - B = A\) ,显然如果 \(len\) 减少,那么 \(A\) 是不增的,因此我们考虑取 \(len = sum\)。
综上,最优的子串长度一定为 \(sum\) ,因此枚举左端点,取长度为 \(sum\) 的子串计算每个 \(cost = A\) 取最小值即可。而 \(A\) 可通过前缀和预处理。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
方法1可是基础捏,一定要会哟qwq。
代码
方法1
///尺取法
#include <bits/stdc++.h>
using namespace std;
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
string s;
cin>>s;
int n = s.length();
int cnt0 = 0,cnt1 = 0;
for(int i = 0;i<n;i++){
if(s[i] == '1') cnt1++;
}
int ans = cnt1;
int l = 0,r = 0;
while(l<n){
while(r<n && cnt0!=cnt1){
if(s[r] == '0') cnt0++;
else if(s[r] == '1') cnt1--;
r++;
}
ans = min(ans,max(cnt0,cnt1));///最后r<n,cnt0和cnt1就不一定平衡了
if(s[l] == '0') cnt0--;
else if(s[l] == '1') cnt1++;
l++;
}
cout<<ans<<'\n';
}
return 0;
}
方法2
///结论,取长度为len,费用即字串0的个数
#include <bits/stdc++.h>
using namespace std;
int pre[200007];
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
string s;
cin>>s;
int n = s.length();
s = "?" + s;
for(int i = 1;i<=n;i++) pre[i] = pre[i-1] + (s[i] == '0');
int cnt1 = count(s.begin(),s.end(),'1');
int ans = cnt1;
for(int i = cnt1;i<=n;i++) ans = min(ans,pre[i] - pre[i-cnt1]);
cout<<ans<<'\n';
}
return 0;
}
E
题目
https://codeforces.com/problemset/problem/1680/E
题解
思路
知识点:状压DP。
假设存在一个 \(*\) 的最优最后位置,那么所有 \(*\) 移动到这个位置的路径长度就是最小操作数,同时在这些路径上任取一点作为 \(*\) 的最后位置是不改变路径长度的,因此这些位置也是最优的最后位置,所以可以确定最后一列 \(*\) 的位置也是一个最优位置且是最右的最优位置,所以一开始可以把地图最后一列 \(*\) 之后列都清除,并修改 \(n\) 为最后一列。接下来考虑如何得到所有 \(*\) 到最后一列 \(*\) 的最小路径长度。
设 \(dp[i][j]\) 表示第 \(i\) 列且 \(*\) 的状态为 \(j\) 的最小操作数,\(j = 0/1/2/3\) 分别代表没有 \(*\) ,仅第一行有 \(*\) ,仅第二行有 \(*\) ,两行都有 \(*\) (对应二进制位)。
先考虑将第 \(i-1\) 列状态水平转移到第 \(i\) 列,假设第 \(i\) 列的 \(*\) 状态是 \(state\) ,第 \(i-1\) 列的 \(*\) 状态是 \(j\) ,那么有水平状态转移方程:
\]
其中 \(j|state\) 指将第 \(i-1\) 列和第 \(i\) 列的 \(*\) 水平合并的状态, \((j\&1) + ((j>>1)\&1)\) 指合并状态需要的操作次数,即 \(j\) 二进制位 \(1\) 的数量。
再考虑将第 \(i\) 列状态垂直转移,\(j = 3\) 的情况可以合并成 \(j = 1/2\) 的情况,\(j = 1/2\) 的情况可以垂直移动变为 \(j = 2/1\) 的情况,\(j = 0\) 的情况在同列没有下一种可能情况,因此最后有两个状态可以被转移 \(j = 1/2\) :
dp[i][2] = min({dp[i][2],dp[i][1] + 1,dp[i][3] + 1})
\]
最后递推到最后一个 \(*\) 出现的列 \(n\) ,答案即为 \(min(dp[n][1],dp[n][2])\) 。
注意开始时要把 \([1,n]\) 的所有状态设为无穷大(1e9就行),设 \(dp[0][0 \cdots 3] = 0\) 。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int dp[200007][4+7];
int main(){
std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s1,s2;
cin>>s1>>s2;
s1 = "?" + s1;
s2 = "?" + s2;
while(s1.back() == '.' && s2.back() == '.') n--,s1.pop_back(),s2.pop_back();///找到最后一列有*的
for(int i = 1;i<=n;i++) for(int j = 0;j<4;j++) dp[i][j] = 1e9;///初始化都设为无穷大
for(int i = 1;i<=n;i++){
int state = 0;
if(s1[i] == '*') state |= 1;
if(s2[i] == '*') state |= 2;
for(int j = 0;j<4;j++){///水平移动的转移
dp[i][j | state] = min(dp[i][j | state],dp[i-1][j] + (j&1) + ((j>>1)&1));
}
///垂直移动的转移
dp[i][1] = min({dp[i][1],dp[i][2] + 1,dp[i][3] + 1});
dp[i][2] = min({dp[i][2],dp[i][1] + 1,dp[i][3] + 1});
}
cout<<min(dp[n][1],dp[n][2])<<'\n';
}
return 0;
}
Educational Codeforces Round 128 (Rated for Div. 2) A-C+E的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
随机推荐
- 1. 初识 JavaScript
1.1 JavaScript 是什么 布兰登·艾奇(Brendan Eich,1961年-). 神奇的大哥用10天完成 JavaScript 设计. 最初命名为 LiveScript,后来在与 Sun ...
- Python学习笔记: 通过type annotation来伪指定变量类型
简介 通过annotation像强类型language那样指定变量类型,包括参数和返回值的类型 因为Python是弱类型语言,这种指定实际上无效的.所以这种写法叫annotation,就是个注释参考的 ...
- 【第二课】从零开始学习Linux(学习笔记)
之前工作会接触Linux,会常用的命令和服务,看过一些书,缺乏系统的学习和整理,现在放空自己,从零开始学习. 每日学习打卡: 2022-04-04
- ChCore Lab3 用户进程和异常处理 实验笔记
本文为上海交大 ipads 研究所陈海波老师等人所著的<现代操作系统:原理与实现>的课程实验(LAB)的学习笔记的第三篇:用户进程与异常处理.所有章节的笔记可在此处查看:chcore | ...
- Spring注解开发_Spring容器创建概述
浅尝Spring注解开发_Spring容器创建概述 浅尝Spring注解开发,基于Spring 4.3.12 概述Spring容器创建的过程,包括12个方法的执行 浅尝Spring注解开发_自定义注册 ...
- CVE-2021-35042
CVE-2021-35042 漏洞介绍 Django 是 Python 语言驱动的一个开源模型-视图-控制器(MVC)风格的 Web 应用程序框架. 漏洞影响版本:django 3.1.3.2 202 ...
- form表单与CSS选择器和样式操作
form表单 """获取前端用户数据并发送给后端服务器""" <form action=""></fo ...
- logging、openpyxl、第三方模块下载
### 日志模块的组成部分 ```pythonimport logging# 1.logger对象:产生日志logger = logging.getLogger('转账记录')# 2.filter对象 ...
- 简易版 vue实现
Vue-mini 完整的Demo示例:git@github.com:xsk-walter/Vue-mini.git 一.Vue实例 构造函数: $option\ $el\ $data 判断是否存在 通 ...
- 资讯:IEEE1
IEEE 2020 年 12 大技术趋势:边缘计算.量子计算.AI.数字孪生等 2020-02-06 以下是对2020年12大技术趋势的预测.IEEE计算机协会自2015年以来一直在预测技术趋势,其年 ...