Educational Codeforces Round 112 (Rated for Div
Educational Codeforces Round 112 (Rated for Div. 2)
CodeForces - 1555D Say No to Palindromes
如果一个字符串中不包含长度2以上的回文子串,我们就说这个字符串是漂亮的,现在一个字符串只有a,b,c三个小写字母组成,每次操作可以修改任意一个字母为a,b,c,存在q次询问,每次询问给定区间\([l,r]\),你需要求出将这个区间子串变为漂亮字符串的最小操作数
题解:计数DP+思维(字符串循环节)
我们模拟后发现如果想要使得一个字符串漂亮,那么我们这个字符串的样子应该为:abcabcabc...那么这样的循环节一共有6种,我们只要对于每一种循环节求操作数即可,我们先预处理完后再回答询问即可
状态表示:\(f[i][j]\):对于第i个循环节,将字符串\([1,j]\)修改为漂亮字符串所需的操作数
状态属性:数量
状态转移:
- 当前字符和循环节上的字符不一样 \(str[i]!=s[i][pos]\),\(f[i][j] = f[i][j-1]+1\)
- 当前字符和循环节上的字符相同\(str[i]==s[i][pos]\),\(f[i][j] = f[i][j-1]\)
那么对于\([l,r]\)之间的最小操作数,我们只能选择一种循环节将它变成漂亮字符串,那么答案就是\(\sum _{i=1}^{6} {min(f[i][r]-f[i][l-1])}\)
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10, M = 4e5 + 10;
string s[6] = {"#abc", "#acb", "#bac", "#bca", "#cab", "#cba"};
int f[6][N];
int n, q;
void solve()
{
cin >> n >> q;
string str;
cin >> str;
str = " " + str;
for (int i = 0; i < 6; ++i)
{
for (int j = 1; j <= n; ++j)
{
int pos = j % 3;
if (pos == 0)
pos = 3;
if (str[j] != s[i][pos])
f[i][j] = f[i][j - 1] + 1;
else
f[i][j] = f[i][j - 1];
}
}
while (q--)
{
int l, r;
cin >> l >> r;
int ans = INF;
for (int i = 0; i < 6; ++i)
ans = min(ans, f[i][r] - f[i][l - 1]);
cout << ans << endl;
}
}
signed main(void)
{
Zeoy;
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
CodeForces - 1555E Boring Segments
给定\(n\)个区间\([l,r]\),每个区间都有权值\(w\),又给定\(m\),求如果若干个区间能够覆盖\([1,m]\),那么成本就是这些区间中的最大权值和最小权值之差,现在保证一定能有区间能够覆盖\([1,m]\),求出覆盖\([1,m]\)成本的最小值
题解:双指针+线段树维护区间覆盖问题+贪心 : 好题目
- 我们先来说一说怎么快速计算出成本,并且能够在\(O(n)\)时间内遍历完所有能够覆盖[1,m]的若干线段
我们将区间按照权值升序排列,那么如果\([i,j]\)之间的区间能够覆盖[1,m],那么它的成本就是\(w_j-w_i\)
我们往往可以使用双指针来解决极值问题,那么在这个问题中,我们发现随着\(j\)指针的后移,\(i\)指针存在单调性,所以我们可以使用双指针来解决问题
那么我们如何检查现在覆盖的区间是否覆盖\([1,m]\),我们可以利用线段树维护区间最小值,对于每一个区间\([l,r]\)进行区间加1,如果[1,m]区间中的最小值<1,那就说明区间[1,m]没有被覆盖到,否则说明已经覆盖完了,所以我们只需要在进行双指针时进行区间修改即可,然后我们每次只要检查\(seg[1].val.minn\)即可
我们需要注意,在本题中,例如:[1,5],[6,m]这样是不算覆盖m的,只有区间重合才算,所以我们为了解决这个问题,对于[l,r]我们可以在每次修改的时候只修改\([l,r-1]\),那么最后我们建树建立的区间只需要建立\([1,m-1]\)即可
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 3e5 + 10, M = 1e6 + 10;
int n, m;
struct info
{
int minn;
};
struct node
{
int l, r, lazy;
info val;
} seg[M << 2];
info operator+(const info &a, const info &b)
{
info c;
c.minn = min(a.minn, b.minn);
return c;
}
void settag(int id, int tag)
{
seg[id].val.minn += tag;
seg[id].lazy += tag;
}
void up(int id)
{
seg[id].val = seg[lson].val + seg[rson].val;
}
void down(int id)
{
if (seg[id].lazy == 0)
return;
settag(lson, seg[id].lazy);
settag(rson, seg[id].lazy);
seg[id].lazy = 0;
}
void build(int id, int l, int r)
{
seg[id].l = l, seg[id].r = r;
int mid = l + r >> 1;
if (l == r)
{
seg[id].val.minn = 0;
return;
}
build(lson, l, mid);
build(rson, mid + 1, r);
up(id);
}
void modify(int id, int ql, int qr, int val)
{
int l = seg[id].l, r = seg[id].r;
if (ql <= l && r <= qr)
{
settag(id, val);
return;
}
down(id);
int mid = (l + r) >> 1;
if (qr <= mid)
modify(lson, ql, qr, val);
else if (ql > mid)
modify(rson, ql, qr, val);
else
{
modify(lson, ql, qr, val);
modify(rson, ql, qr, val);
}
up(id);
}
struct range
{
int l, r, w;
bool operator<(const range &t) const
{
return w < t.w;
}
} a[N];
void solve()
{
cin >> n >> m;
build(1, 1, m - 1);
for (int i = 1; i <= n; ++i)
cin >> a[i].l >> a[i].r >> a[i].w;
sort(a + 1, a + n + 1);
int ans = INF;
for (int i = 1, j = 1; j <= n; ++j)
{
modify(1, a[j].l, a[j].r - 1, 1);
while (i <= j && seg[1].val.minn > 0)
{
ans = min(ans, a[j].w - a[i].w);
modify(1, a[i].l, a[i].r - 1, -1);
i++;
}
}
cout << ans << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
Educational Codeforces Round 112 (Rated for Div的更多相关文章
- 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 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- Windows 串口代码
#pragma once #include <Windows.h> #define DEFAULT_THREAD_TERMINATED_TIME 2000 class CAutoThrea ...
- pandas连接msyql报(1115, "Unknown character set: 'utf8mb4'")错误
代码如下: 报错如下: 首先,为什么会出现这个错误: 分析如下: 随着智能手机的普及,我们开始经常使用表情符号.��更好的帮助我们进行交流.但是mysql的utf8编码只支持3字节的数据,而移动端的表 ...
- nvm管理node和npm
安装nvm 下载地址:https://github.com/coreybutler/nvm-windows/releases 下载前卸载调node,安装时注意记住所在路径,傻瓜式安装.安装后nvm - ...
- LG P3768 简单的数学题
\(\text{Problem}\) 求 \[\left(\sum_{i=1}^n \sum_{j=1}^n i j \gcd(i,j)\right) \bmod p \] \(n \le 10^{1 ...
- ubuntu 20.04 远程桌面(win10 控制 Ubuntu 20.04)
转载csdn: https://blog.csdn.net/lucky7213/article/details/107008246/
- K3S系列文章-使用AutoK3s在腾讯云上安装高可用K3S集群
开篇 <K3s 系列文章> <Rancher 系列文章> 方案 在腾讯云上安装 K3S 后续会在这套 K3S 集群上安装 Rancher 方案目标 高可用 3 台master ...
- key对象转换数组title
before <!DOCTYPE HTML> <html> <head> <title>key对象转换数组title</title> < ...
- .gitattributes 作用详细讲解
https://blog.csdn.net/qq_35425070/article/details/106883833 *.fbx filter=lfs diff=lfs merge=lfs -tex ...
- 四,redis6版本的使用部署
继第三章(https://www.cnblogs.com/123456likun/p/13841540.html) 官网发布最新的6版本,有新的数据类型出现,给了小编我极大的动力,决定写几张关于最新的 ...
- 第七周作业-N67044-张铭扬
1. 说明自动化运维的路径,原理,实践方法. 所谓自动化运维是指通过将日常IT运维中大量的重复性工作(小到简单的日常检查.配置变更和软件安装,大到整个变更流程的组织调度)由过去的手工执行转为标准化.流 ...