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. 2 .NET Core笔试题

    1.说说在Linux系统部署ASP.NET Core项目的步骤. 2.说说热重载是什么. 3.如何理解鉴权和授权两个词 4.说说.NET7包含了几大方向的开发? 5.如何理解云原生? 6.ASP.NE ...

  2. Hexo系列(一):Hexo简介

    作者:独笔孤行 官网:​​ ​http://anyamaze.com​​ 公众号:云实战 前言 博客网站的实现方法有很多种,常用的博客框架主要有wordpress.Z-Blog.hexo.Typech ...

  3. Ubuntu18.04安装教程

    转载csdn: Ubuntu18.04安装教程_Sunshine的博客-CSDN博客_ubuntu安装教程

  4. group by 、concat_ws()、 group_caoncat()的使用

    group系列 之前觉得这里简单不需要再进行总结了.后来发现还是需要总结巩固一下,还是有一些方法之类的之前未使用过.这里来重新整理,记录一下. group by 将表中的数据根据某个条件进行分组. 比 ...

  5. 关于dynamic类型

    动态类型是从C#4才开始有的,随着DLR一起引人的. 大部分时候的行为如同object类型,对应的IL代码也是object类型. 它的特点是编译时会忽略在dynamic上进行的操作,如调用方法,读写属 ...

  6. 三天吃透Java并发八股文!

    本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...

  7. C++调用C#DLL并调试

    使用C++ 来调用C#DLL 并且调试程序 环境:使用VS studio 2019 C#项目的设置 1.C# -> 属性 -> 应用程序 -> 目标框架 ->.NET Fram ...

  8. vue 利用xlsx、xlsx-style、file-saver实现前端导出excel表格 (包括设置单元格居中、边框等样式) antdesignvue、elementui、vxetable 等都适用

    我用的方法是在表格的根组件外层赋一个div用来导出整个表格,所以antdesignvue.elementui.vxetable 或者原生的table写法应该全都适用,此处我用的框架为antdesign ...

  9. python命令行开发--click

    目录 前言 安装 快速上手 参考文档 前言 按照官方的介绍:Click 是一个利用很少的代码以可组合的方式创造优雅命令行工具接口的 Python 库. 它是高度可配置的,但却有合理默认值的" ...

  10. leetcode 30. 串联所有单词的子串 【时间击败 90.28%】 【内存击败 97.44%】

    这道题让我从早做到晚-3--- 设len=words[0].length(). 一开始我按照words的顺序扩大区间,发现这样就依赖words的顺序.之后改成遍历s的所有长度为len*words.le ...