Codeforces Round #650 (Div. 3) F1经典离散化DP
比赛链接:Here
1367A. Short Substrings
Description
一个字符串 abac,然后把所有长度为2的子串加起来变成新串,abbaac,由 ab ba ac组成。现在给出新串,找出原串。
直接模拟即可
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
string s; cin >> s;
cout << s[0];
for (int i = 1; i < s.size(); i += 2) cout << s[i];
cout << "\n";
}
}
1367B. Even Array
Description
给定一个数组。有一种操作,可以交换任意两个数的位置。要求的数组奇偶交替出现。最少需要多少次操作。
先统计一遍都多少个奇数不在位置上,和多少个偶数不在位置上。如果不相等,那么无解。反之,把他们放到对应的位置就好了。
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n; cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
int cnt0 = 0, cnt1 = 0, ans = 0;
for (int i = 0; i < n; ++i) {
cnt0 += a[i] % 2;
cnt1 += i % 2;
if (i % 2 == 1 && a[i] % 2 == 0) ans += 1;
}
if (cnt0 != cnt1) cout << "-1\n";
else cout << ans << "\n";
}
}
1367C. Social Distance
Description
给定一个01串。需要保证两个相邻的1之间的距离大于k。求原串在不违背题意的情况下,还能插入多少个1。
这个需要分情况考虑。左边连续的0单独计数。右边连续的0单独计数。中间出现的连续的0单独计数。
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, k; string s;
cin >> n >> k >> s;
int sum = 0, cnt = 0, pre = 0;
k += 1;
bool f = false;
for (int i = 0; i < n; ++i) {
if (s[i] == '0') cnt += 1;
else {
f = true;
if (pre == 0) sum += max(0, cnt / k);
else sum += max(0, (cnt - k + 1) / k);
pre = 1, cnt = 0;
}
}
sum += max(0, cnt / k);
if (!f) sum = max(1 + max(0, (n - 1) / k), sum);
cout << sum << "\n";
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, k; string s;
cin >> n >> k >> s;
int sum = 0, cnt1 = 0, cnt0 = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
cnt1 += 1;
if (cnt1 == 1)
sum += cnt0 / (k + 1);
else {
sum += (cnt0 - k) / (k + 1);
cnt1 = 1;
}
cnt0 = 0;
}
if (s[i] == '0') cnt0 += 1;
if (i == n - 1 && s[i] == '0' && cnt1 != 0)
sum += cnt0 / (k + 1);
}
if (cnt1 == 0) sum += (cnt0 + k) / (k + 1);
cout << sum << "\n";
}
}
1367D. Task On The Board
Description
给定s串,删除一些字符,重新组合,得到t串。根据t串,可以计算一个b数组。$b_i = sum(|j-i|)$ if $t_j > t_i$,也就是所有比i位置大的字符的位置差的和。现在给定s和b数组,求t串。
首先肯定是找 \(b\) 数组为 \(0\) 的位置。因为\(0\) ,就表示,没有比他更大的字符了。然后这些位置填上了字符之后,对于非 \(0\) 的位置,他们都是有贡献的。把这些贡献从 \(b\) 数组中减掉,就又会尝试新的 \(0\) 的位置。就循环模拟这个过程就好了。
const int N = 50;
int b[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
string s; int m;
cin >> s >> m;
for (int i = 0; i < m; ++i) cin >> b[i];
int cnt[30] = {};
for (auto c : s) cnt[c - 'a'] += 1;
int r = m, l = 25;
string t(m, '?');
while (r) {
int c = 0;
for (int i = 0; i < m; ++i)
if (!b[i]) c += 1;
while (cnt[l] < c) l -= 1;
for (int i = 0; i < m; ++i) {
if (!b[i]) {
t[i] = 'a' + l;
b[i] -= 1;
r -= 1;
}
}
for (int i = 0; i < m; ++i) {
if (t[i] == 'a' + l)
for (int j = 0; j < m; ++j)
if (b[j] > 0) b[j] -= abs(i - j);
}
l -= 1;
}
cout << t << "\n";
}
}
1367E. Necklace Assembly
Description
给定一堆字符。然后选出len个,围成一圈。且以k为循环节。即每转动k次,这个圈看上去是一模一样的。给定k。求最大的len。
以 \(K\) 为循环节。那么以 \(K\) 的因子 \(K\) 为循环节都可以。所以先枚举 \(K\) 的所有因子。然后对于每个循环节 \(K\) ,如果只由 \(K\) 个元素组成。那显然是可以的。但是要要枚举 \(2*k\) ,可不可以,\(3*k\) 可不可以,直到找到一个最大的 \(n*k\) 可行。那么对于循环节 \(k\) 。最大长度就是 \(n*k\) 。对所有枚举到的取 \(\max\) 就好了。
int check(int k, int cnt[]) {
int tmp = k;
for (int j = 2;; j += 1) {
int tt = 0;
for (int i = 0; i < 26; ++i) tt += cnt[i] / j;
if (tt >= k) tmp = j * k;
else return tmp;
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int cnt[100] = {};
string s;
int n, m;
cin >> n >> m >> s;
for (int i = 0; i < n; ++i) cnt[s[i] - 'a'] += 1;
sort(cnt, cnt + 100);
reverse(cnt, cnt + 100);
int ans = 1;
for (int i = 1; i <= m && i <= n; ++i) {
if (m % i == 0)
ans = max(ans, check(i, cnt));
}
cout << ans << "\n";
}
}
1367F1. Flying Sort (Easy Version)
Description
一个数组,一次操作可以选择一个数,放到数组开头,或者放到末尾。问最少多少次操作可以使得数组有序。Easy版中所有元素各不相同。且数组长度 $< 3000$。
超级经典 DP + 离散化处理,一下
考虑求不需要动的元素。先对 \(a\) 数组离散化,变成值域 \(1\sim n\) 的数组。然后不需要动的元素是差值为 \(1\) 的连续上升子序列。DP 求这个最大长度就好了。其他的数都是要移动的。并且要移动一个数。至多只移动一次就好了。所以求出最长子序列长度,再用 \(n\) 减去就是答案了。
const int N = 1e6 + 10;
int a[N], b[N], f[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n; cin >> n;
for (int i = 0; i < n; ++i) cin >> b[i], a[i] = b[i];
sort(b, b + n);
for (int i = 0; i <= n ; ++i) f[i] = 0;
for (int i = 0; i < n; ++i) a[i] = lower_bound(b, b + n, a[i]) - b + 1;
int ans = 0;
for (int i = 0; i < n; ++i) {
f[a[i]] = f[a[i] - 1] + 1;
ans = max(ans, f[a[i]]);
}
cout << n - ans << "\n";
}
}
1367F2. Flying Sort (Hard Version)
AC参考代码
const int N = 2e5 + 10;
int a[N], p[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n; cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
iota(p, p + n, 0);
sort(p, p + n, [&](int i, int j) {return a[i] < a[j] || a[i] == a[j] && i < j;});
int ans = 0;
for (int l = 0, r; l < n; l = r) {
for (r = l + 1; r < n && p[r] > p[r - 1]; ++r);
int cnt = r - l;
if (l)
for (int i = l - 1; i >= 0 && a[p[i]] == a[p[l - 1]]; i--)
if (p[i] < p[l]) cnt += 1;
if (r < n)
for (int i = r; i < n && a[p[i]] == a[p[r]]; ++i)
if (p[i] > p[r - 1]) ++ cnt;
ans = max(ans, cnt);
}
for (int l = 0, m, r; l < n; l = m) {
for (m = l; m < n && a[p[m]] == a[p[l]]; ++m);
if (m == n) break;
for (r = m; r < n && a[p[r]] == a[p[m]]; ++r);
for (int i = l, j = m; i < m; ++i) {
while (j < r && p[j] < p[i]) ++j;
ans = max(ans, i + 1 - l + r - j);
}
}
cout << n - ans << "\n";
}
}
Codeforces Round #650 (Div. 3) F1经典离散化DP的更多相关文章
- Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)
F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #215 (Div. 2) D题(离散化+hash)
D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #545 (Div. 2) C. Skyscrapers 离散化+贪心
题目链接 给你一个n∗m的矩阵res,让你输出一个n∗m的矩阵a,这个矩阵满足:给你一个n*m的矩阵res,让你输出一个n*m的矩阵a,这个矩阵满足:给你一个n∗m的矩阵res,让你输出一个n∗m的矩 ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Codeforces Round #650 (Div. 3) C. Social Distance
题目链接:https://codeforces.com/contest/1367/problem/C 题意 给出一个长为 $n$ 的 $01$字符串,两个相邻 $1$ 间距应大于 $k$,初始序列合法 ...
- Codeforces Round #650 (Div. 3) B. Even Array
题目链接:https://codeforces.com/contest/1367/problem/B 题意 有一大小为 $n$ 的数组 $a$,问能否经过交换使所有元素与下标奇偶性相同(0 - ind ...
- Codeforces Round #650 (Div. 3) A. Short Substrings
题目链接:https://codeforces.com/contest/1367/problem/A 题意 给出一个字符串 $t$,找出原字符串 $s$,$t$ 由 $s$ 从左至右的所有长为 $2$ ...
随机推荐
- vue中export default function 和 export function 的区别
export default function 和 export function 的区别 // 第一种 export default function crc32() { // 输出 // ... ...
- 【LOJ NOI Round#2 Day1 T1】单枪匹马(矩阵乘法)
题目传送门 操作二要求的东西是一个循环迭代的东西,手推相邻两项找下规律,发现相邻两项的分子分母间含有线性关系,考虑用矩阵乘法求解.对于 \([1,n]\)的询问,从后往前倒推, \(x_{n-1}=a ...
- 针对LocalDateTime日期格式化解决方案
前两天做项目,后端返回给前端的数据中日期时间格式如下: 并不是我想要的yyyy-MM-dd HH:mm:ss格式的,所以网上搜了一下有一个JSON日期LocalDateTime序列化器(如果是Date ...
- rcs群发软件系统功能设计与应用,rcs群发软件系统,rcs群发软件
随着科技的不断发展,人们对于通讯方式的需求也在不断变化,传统的短信.电话已经无法满足人们对于高效.便捷.实时的通讯需求,正是在这样的背景下,富通讯解决方案(Rich Communication Sui ...
- ElasticSearch之cat recovery API
命令样例如下: curl -X GET "https://localhost:9200/_cat/recovery?v=true&pretty" --cacert $ES_ ...
- Python笔记二之多线程
本文首发于公众号:Hunter后端 原文链接:Python笔记二之多线程 这一篇笔记介绍一下在 Python 中使用多线程. 注意:以下的操作都是在 Python 3.8 版本中试验,不同版本可能有不 ...
- 在 Walrus 上轻松集成 OpenTofu
OpenTofu 是什么? OpenTofu 是一个开源的基础设施即代码(IaC)框架,被提出作为 Terraform 的替代方案,并由 Linux 基金会管理.OpenTofu 的问世为应对 Has ...
- Java 并发编程(四)同步工具类
本文使用的 JDK 版本为 JDK 8 基本同步工具类 闭锁(CountDownLatch) 闭锁是一种工具类,可以延迟线程的进度直到其到达终止状态.闭锁的作用相当与一扇门:在闭锁的状态到达之前,这扇 ...
- RabbitMQ系列:windows、centos和docker下环境安装和使用
一.Windows环境下安装 1.erlang下载:https://www.erlang-solutions.com/resources/download.html 或者:https://www.er ...
- 面试官:String长度有限制吗?是多少?还好我看过
前言 话说Java中String是有长度限制的,听到这里很多人不禁要问,String还有长度限制?是的有,而且在JVM编译中还有规范,而且有的家人们在面试的时候也遇到了,本人就遇到过面试的时候问这个的 ...