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 ...
随机推荐
- 微信小程序wx.navigateTo跳转参数大小超出限制问题
微信小程序的跳转方式 wx.navigateTo(Object):保留当前页面,跳转到应用内的某个页面,使用 wx.navigateBack 可以返回到原页(新页面入栈) wx.redirectTo( ...
- 基于jib-maven-plugin快速构建微服务docker镜像
一.说明 本文介绍基于 Maven 插件 jib-maven-plugin 实现快速构建 Spring Boot 程序镜像,并推送到远程仓库中,且 无需安装 Docker 环境 . Jib 是 Goo ...
- 【USACO 2021 US Open, Platinum】United Cows of Farmer John
\(\text{Solution}\) 简要的口胡 线段树维护合法左端点数量 正序枚举 \(i\),将其视为右端点,去除不合法的左端点,统计贡献 再将其视为中间点,由它产生的左端点在线段树上区间加 注 ...
- 图文指南 篇二:虚拟机ESXi6.7安装黑群晖教程
转载:什么值得买 (ESXI虚拟机是非常好用的多开虚拟机是VM系列非常强的) https://post.smzdm.com/p/agd8l34w/#:~:text=7%E5%AE%89%E8%A3%8 ...
- echarts 各个配置项详细说明总结
theme = { // 全图默认背景 // backgroundColor: 'rgba(0,0,0,0)', // 默认色板 color: ['#ff7f50','#87cefa','#da70d ...
- Python elasticsearch_dsl 报错及解决方法
Exception:maxClauseCount is set to 1024 原因:报错原因是Search限制一个bool查询中最多只能有1024个值或子查询,当超过1024时,会抛出异常. 解决办 ...
- Android获取获取悬浮窗一下的view办法
getwindows可以获取当前手机屏幕所有有交互功能的view getactitywindows只能获取最顶层有交互的view
- marker的存储组---layerGroup
1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="U ...
- spring RedisTemplate用法
1.maven依赖 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --&g ...
- PHP 网页 apache24+php8 yii basic
PHP官网下载 https://windows.php.net/download/ 在PHP官网点击Download下载时不管选择哪个版本的都有两个类型 : Non Thread Safe(非线程安全 ...