CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-E
A
题解
知识点:思维,模拟。
发现 \(b\) 串第一个字符是 \(1\) 则只能使用 max , \(0\) 则只能使用 min ,随后只需要模拟到 \(a\) 串剩余 \(m\) 个字符时停止即可,然后比对两串。
时间复杂度 \(O(n-m)\)
空间复杂度 \(O(n+m)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n, m;
cin >> n >> m;
string a, b;
cin >> a >> b;
if (b[0] == '0') for (int i = 1;i <= n - m;i++) a[i] = min(a[i], a[i - 1]);
else if (b[0] == '1') for (int i = 1;i <= n - m;i++) a[i] = max(a[i], a[i - 1]);
if (a.substr(n - m, m) == b) return true;
else return false;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "NO" << '\n';
else cout << "YES" << '\n';
}
return 0;
}
B
题解
知识点:贪心。
一个一个走过去,记录走过的最大值和最小值,一旦大于两倍 \(x\) 则说明要换中心点 \(v\) ,记录换的次数即可。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n, x;
cin >> n >> x;
int mi = 2e9, mx = 0, cnt = 0;
for (int i = 1, tmp;i <= n;i++) {
cin >> tmp;
mi = min(mi, tmp);
mx = max(mx, tmp);
if (mx - mi > 2 * x) cnt++, mx = mi = tmp;
}
cout << cnt << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
C
题解
知识点:贪心。
注意到所有非感染者区间每秒都会被感染两个(从左右端点扩展,少于两个直接消失),因此考虑从大区间端点开始保护,每次更新 \(delta\) 代表每个区间减少了多少,则真实区间长度是原长减去 \(delta\)。
因此若真实区间大于等于2,则在两个端点各保护一个,答案加区间长度减一(一个端点保护完,另一个端点会感染一个),\(delta\) 加 \(4\);若区间长度等于1,则只能保护一个,直接退出;若区间已经小于等于0,没有能保护的,直接退出。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100007], l[100007];
bool solve() {
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) cin >> a[i];
sort(a + 1, a + 1 + m);
l[1] = n - (a[m] - a[1] + 1);
for (int i = 2;i <= m;i++) l[i] = a[i] - a[i - 1] - 1;
sort(l + 1, l + m + 1, [&](int a, int b) {return a > b;});
int delta = 0, ans = 0;
for (int i = 1;i <= m;i++) {
int rl = max(l[i] - delta, 0);
if (rl == 0) break;
if (rl == 1) {
ans++;
break;
}
ans += rl - 1;
delta += 4;
}
cout << n - ans << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
D
题解
知识点:枚举,前缀和,思维。
通常这种题是找不变量。
发现经过操作1以后,前缀和的总和不变;而经过一次操作2后,前缀和的总和会减少1。
因此统计每个数组的前缀和总和,找到前缀和总和最小的就是特殊数组,少了多少就是操作了多少次。
时间复杂度 \(O(nm)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll ssum[100007];
bool solve() {
int n, m;
cin >> n >> m;
for (int i = 1;i <= n;i++) {
ll sum = 0;
ssum[i] = 0;
for (int j = 1;j <= m;j++) {
ll x;
cin >> x;
sum += x;
ssum[i] += sum;
}
}
cout << min_element(ssum + 1, ssum + n + 1) - ssum << ' ';
cout << *max_element(ssum + 1, ssum + n + 1) - *min_element(ssum + 1, ssum + n + 1) << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
E
题解
知识点:图论,dp。
题目不难理解为,每秒所有点都会流出一个量到所有自己的下一个点,什么时候流完。比如,若 \(u\) 有三个下一个节点 \(v_1,v_2,v_3\) ,则一秒后 \(u\) 的值减一,而 \(v_1,v_2,v_3\) 都加一。
因为是DAG,所以图中每个点的量总是向出度为 \(0\) 的节点方向流动,最终出度为 \(0\) 的点的流完时间就是答案。
而对于一个节点的流完时间,取决于他所有上一个节点流完时间的总和加上自己节点的量,因为一个节点每秒只能流出一个量,只要上一个节点没流完,那这个节点就会一直存在量。因此,设 \(dp[i]\) 为编号 \(i\) 的节点的流出时间,则节点 \(u\) 与其上一个节点 \(v_i\) 有转移方程:
\]
但是有一种情况,如 10-0-1-0-0-null ,显然流完时间是 \(14\) ,但通过上面计算得到的是 \(11\) 。因为上述方程直接考虑从第一秒到最后一秒,整个图的量的转移是连续的,即量是持续减少,而没有考虑中间可能存在断点,但显然这是不确定的,这个例子就是。
因此需要使整个图的量堆积在出口前,使得量的流出是连续的。由于整个图有 \(n\) 个节点,至多 \(n-1\) 秒可以让量流经图中所有点(这里模拟 \(\geq n-1\) 秒都没问题),因此先模拟 \(n\) 秒,等到量都连起来了,再dp计算总时间即可。
模拟时候用正图,dfs时候反图,因此存两个图。
时间复杂度 \(O(n^2m)\)
空间复杂度 \(O(n+m)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 998244353;
vector<int> a, dp;
vector<int> g1[1007], g2[1007]; ///g1正向模拟用,g2反向dfs用
int dfs(int u) {
if (dp[u]) return dp[u];
dp[u] = a[u] % mod;
for (auto v : g2[u])
dp[u] = (0LL + dp[u] + dfs(v)) % mod;
return dp[u];
}
bool solve() {
int n, m;
cin >> n >> m;
a = dp = vector<int>(n + 1, 0);
for (int i = 1;i <= n;i++) cin >> a[i], g1[i].clear(), g2[i].clear();
for (int i = 1;i <= m;i++) {
int u, v;
cin >> u >> v;
g1[u].push_back(v);
g2[v].push_back(u);
}
if (count(a.begin() + 1, a.end(), 0) == n) {
cout << 0 << '\n';
return true;
}
for (int i = 1;i <= n;i++) {///保证所有量都在出口前靠拢,防止空节点挡在其他节点之前产生计算错误
vector<int> t = a;///临时,防止某个量持续传递
for (int u = 1;u <= n;u++) {
if (a[u]) {
t[u]--;
for (auto v : g1[u]) t[v]++;
}
}
a = t;
if (count(a.begin() + 1, a.end(), 0) == n) {
cout << i << '\n';
return true;
}
}
int root = 0;
for (int i = 1;i <= n;i++) if (!g1[i].size()) root = i;
cout << (dfs(root) + n) % mod << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-E的更多相关文章
- Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces Round #438 (Div.1+Div.2) 总结
本来兴致勃勃的想乘着这一次上紫,于是很早很早的到了机房 但是好像并没有什么用,反而rating-=47 Codeforces Round #438(Div.1+Div.2) 今天就这样匆匆的总结一下, ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Descripti ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构 [Problem ...
- Codeforces Round #792 (Div. 1 + Div. 2) A-E
Codeforces Round #792 (Div. 1 + Div. 2) A-E A 题目 https://codeforces.com/contest/1684/problem/A 题解 思路 ...
- Codeforces Round #792 (Div. 1 + Div. 2) // C ~ E
比赛链接:Dashboard - Codeforces Round #792 (Div. 1 + Div. 2) - Codeforces C. Column Swapping 题意: 给定一个n*m ...
- 【codeforces】【比赛题解】#868 CF Round #438 (Div.1+Div.2)
这次是Div.1+Div.2,所以有7题. 因为时间较早,而且正好赶上训练,所以机房开黑做. 然而我们都只做了3题.:(. 链接. [A]声控解锁 题意: Arkady的宠物狗Mu-mu有一只手机.它 ...
随机推荐
- idea maven 依赖还原不上的问题 method <init>()V not found
问题 还原项目依赖的时候报错: java.lang.RuntimeException: org.codehaus.plexus.component.repository.exception.Compo ...
- 好客租房56-props深入(3props校验-约束规则)
1常见类型:Array,bool,func,number,object,string 2React:element 3必填项:isRequred 4特定结构的想:shape({}) //导入react ...
- nginx 源码安装配置详解(./configure)
在"./configure"配置中,"--with"表示启用模块,也就是说这些模块在编译时不会自动构建,"--without"表示禁用模块, ...
- 万字+28张图带你探秘小而美的规则引擎框架LiteFlow
大家好,今天给大家介绍一款轻量.快速.稳定可编排的组件式规则引擎框架LiteFlow. 一.LiteFlow的介绍 LiteFlow官方网站和代码仓库地址 官方网站:https://yomahub.c ...
- [CF1073G]LCP问题
题意:给一个长n的字符串S,q组询问,每组给两个集合A,B.求集合A中的点和集合B中的点所有组合情况的lcp的和. 思路: 好像比较常规,可是代码能力差还是调了1.5h.主要还是虚树板子不熟(加入的时 ...
- drools决策表的简单使用
目录 一.背景 二.一个简单的决策表 1.在同一个决策表中处理多个Sheet页 2.RuleSet下方可以有哪些属性 3.RuleTable下方可以有哪些属性 4.规则属性的编写 三.需求 四.实现 ...
- 耗时半年,Eoapi 终于正式发布 API 工具的插件广场
这是我们的第一篇月报,每个月和每个来之不易的开发者分享产品故事以及产品进展. 在 5.20 这个极具中国特色的"节日"里,Eoapi 发布了 1.0.0 版,三个程序员掉了半年 ...
- ExtJS配置TabPanel可以拖拽Tab标签页
1.环境说明 ExtJS版本:7.4.0.42 Sencha Cmd: v7.5.1.20 开发工具:WebStorm 2022.1.1 PS:如果是老版本的ExtJS,引入Ext.ux.TabReo ...
- C#中常用的目录|文件|路径信息操作
更新记录 本文迁移自Panda666原博客,原发布时间:2021年5月16日. 说明 .NET的类库API设计的非常优秀,再加上文档docs.com写的非常优秀,写代码给人一种十分优雅的感觉. 获得当 ...
- 『忘了再学』Shell基础 — 32、Shell中test测试命令详解
目录 1.test测试命令 (1)test命令介绍 (2)test命令使用方式 (3)示例 2.按照文件类型进行判断 3.按照文件权限进行判断 4.两个文件之间进行比较 5.两个整数之间比较 6.字符 ...