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]\)修改为漂亮字符串所需的操作数

状态属性:数量

状态转移:

  1. 当前字符和循环节上的字符不一样 \(str[i]!=s[i][pos]\),\(f[i][j] = f[i][j-1]+1\)
  2. 当前字符和循环节上的字符相同\(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]\)成本的最小值

题解:双指针+线段树维护区间覆盖问题+贪心 : 好题目

  1. 我们先来说一说怎么快速计算出成本,并且能够在\(O(n)\)时间内遍历完所有能够覆盖[1,m]的若干线段

我们将区间按照权值升序排列,那么如果\([i,j]\)之间的区间能够覆盖[1,m],那么它的成本就是\(w_j-w_i\)

我们往往可以使用双指针来解决极值问题,那么在这个问题中,我们发现随着\(j\)指针的后移,\(i\)指针存在单调性,所以我们可以使用双指针来解决问题

  1. 那么我们如何检查现在覆盖的区间是否覆盖\([1,m]\),我们可以利用线段树维护区间最小值,对于每一个区间\([l,r]\)进行区间加1,如果[1,m]区间中的最小值<1,那就说明区间[1,m]没有被覆盖到,否则说明已经覆盖完了,所以我们只需要在进行双指针时进行区间修改即可,然后我们每次只要检查\(seg[1].val.minn\)即可

  2. 我们需要注意,在本题中,例如:[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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. 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 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. ransac拟合

    链接:https://zhuanlan.zhihu.com/p/62238520 RANSAC简介 RANSAC(Random Sample Consensus,随机采样一致)算法是从一组含有&quo ...

  2. 【KAWAKO】audiotsm-使用python对音频进行变速不变调处理

    目录 安装库 导入相关库 定义reader 定义writer 定义WSLOA算法,并运行 官方手册 源码 安装库 pip install audiotsm 导入相关库 import audiotsm ...

  3. CCRD_TOC_2015_EULAR专刊

    中信国健风湿免疫临床通讯 EULAR2015专刊●目录 脊柱关节炎专题 OP0037 ASAS-CoMoSpA研究: 评价SpA不同分类标准的表现 OP0170 NSAIDs以优化剂量治疗中轴型SpA ...

  4. MicroPython 之 PYBoard

    一.MicroPython 简介 Python,是一种面向对象的解释型计算机程序设计语言,它是纯粹的自由软件,源代码和解释器CPython遵循GPL(GNU General Public Licens ...

  5. elasticsearch 内存分配设置

    一.背景 elasticsearch版本为2.3.3 elasticsearch 默认安装后设置的内存是1GB,对于现实业务来说太小 预计在五台机器上配置elasticsearch构建集群,但是构建索 ...

  6. accessservice对于难定位的view如何定位

    private static int tabcount = -1; private static StringBuilder sb; public static void printPacketInf ...

  7. Solidity8.0-03

    对应崔棉大师 41-50课程https://www.bilibili.com/video/BV1yS4y1N7yu/?spm_id_from=333.788&vd_source=c81b130 ...

  8. cisco ios 密码恢复

    如果没有break键,使用仿真软件模仿一个break 密码恢复请执行以下步骤 1. 关闭或断开路由器电源 2.开启路由器.在通电后的前30秒内按下break键(或通过仿真程序发送一个间断序列),来中断 ...

  9. C++数组(二):二维数组

    二维数组 什么是二维数组?二维数组就是在一维数组的基础上增加一个维度. 二维数组的定义方式 数据类型 数组名[行数][列数]; int arr[2][3]; arr[0][0] = 1; arr[0] ...

  10. Windos下 java后台软件服务化(举例)-WinSW

    WinSW-软件服务化 1.1 举例:ApiWintool可执行jar ApiWintool.exe ApiWintool.jar ApiWintool.xml install.cmd uninsta ...