Codeforces 1296E2. String Coloring (hard version)
这道题和HDU1257一模一样,一开始窝都用贪心直接解,没法理解为什么求一个最长下降序列,直到看了巨巨的题解,先给出一个定理,Dilworth's theorem,离散学不好,补题两行泪,该定理是说,对于任意的偏序集,其最长反链的长度与能分解的最少的链数(chain decomposition)相等,反链(anti-chain)是指该链内任意元素不可比(incomparable),链(chain)则是都可比,回到这一题,要求的是递增链的最小数目,即递增链最小分解数,转换成求其递减链的最长长度即可,转换成了dp问题,先上贪心代码
const int maxm = 2e5+;
int colors[maxm], cache[maxm];
void run_case() {
int n;
string str;
cin >> n >> str;
int res = ;
for(int i = ; i < n; ++i) {
int c = str[i] - 'a';
bool neednew = false;
for(int j = ; j <= res; ++j) {
if(colors[j] <= c) {
colors[j] = c, cache[i] = j; break;
}
if(j == res) neednew = true;
}
if(neednew) {
colors[++res] = c, cache[i] = res;
}
}
cout << res << "\n";
for(int i = ; i < n; ++i) {
cout << cache[i] << " ";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie();
run_case();
//cout.flush();
return ;
}
贪心
设dp[i]表示以i结尾的最长的decrease链,则状态转移为:
1.若str[i] < str[i-1], dp[i]=dp[i-1]+1
2.若str[i] >= str[i-1] dp[i]=1
但是,这个状态与m段最大子段和一样,i的上一个increase字符不一定是i-1,例如bca,dp[a]为2而不为1,我们就模仿m段最大子段和,设maxdp[i]为字母i结尾的最长的decrease链长度,
则dp[i] = max(1,maxdp[pre]+1), pre表示比i大的字母,然后再用dp更新maxdp即可,maxdp[str[i]] = max(maxdp[str[i]], dp[i]),那么,我们如何恢复答案呢,看dp数组更新时,若dp[i]大于了maxdp[pre],因为我们要求的是increase链,则str[i]无法接在pre(比str[i]大的字母)后面,就要用一个新的颜色,即更新了dp[i],所以dp[i]就是答案
void run_case() {
int n;
string str;
cin >> n >> str;
vector<int> maxdp();
vector<int> dp(n, );
for(int i = ; i < n; ++i) {
for(int c = ; c > str[i]-'a'; --c) {
dp[i] = max(dp[i], maxdp[c]+);
}
maxdp[str[i]-'a'] = max(maxdp[str[i]-'a'], dp[i]);
}
cout << *max_element(maxdp.begin(), maxdp.end()) << "\n";
for(int i = ; i < n; ++i)
cout << dp[i] << " ";
}
int main() {
ios::sync_with_stdio(false), cin.tie();
run_case();
//cout.flush();
return ;
}
DP
Codeforces 1296E2. String Coloring (hard version)的更多相关文章
- Codeforces 1296E1 - String Coloring (easy version)
题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...
- E2. String Coloring (hard version)(贪心)
E2. String Coloring (hard version) time limit per test 1 second memory limit per test 256 megabytes ...
- E1. String Coloring (easy version)(贪心)
E1. String Coloring (easy version) time limit per test 1 second memory limit per test 256 megabytes ...
- Codeforces 1027E Inverse Coloring 【DP】
Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...
- Codeforces Round #617 (Div. 3) String Coloring(E1.E2)
(easy version): 题目链接:http://codeforces.com/contest/1296/problem/E1 题目一句话就是说,两种颜色不同的字符可以相互换位, 问,对这字符串 ...
- CodeForces #369 C. Coloring Trees DP
题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少. K:连续的颜色为一组 ...
- Codeforces 799D. String Game 二分
D. String Game time limit per test:2 seconds memory limit per test:512 megabytes input:standard inpu ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- CodeForces 159c String Manipulation 1.0
String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Cod ...
随机推荐
- PyQt5绘图API
PyQt5绘图API大全1.绘图API:绘制文本#1.文本 2.各种图形 3.图像#QPainter painter=QPainter() painter.begin() painter.end() ...
- Vue父组件主动获取子组件的数据和方法
Vue父组件主动获取子组件的数据和方法 https://www.jianshu.com/p/bf88fc809131
- Python学习第二十七课——写一个和Django框架的自己的框架
MyWeb框架: from wsgiref.simple_server import make_server def application(environ, start_response): pri ...
- 九、web.xml理解
1.web.xml文件在每个web工程不是必须要有的: web.xml文件是用来初始化配置信息:比如Welcome页面.servlet.servlet-mapping.filter.liste ...
- sftp和FTP
sftp 是一个交互式安全文件传输协议的传输程式.它类似于 ftp也叫internet网络文件协议, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下 ...
- Centos610无桌面安装Docker-安装
1.必备环境 设定docker源yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.rep ...
- Keras的TimeDistributed层
Keras的TimeDistributed层主要用途是在时间维度上进行全连接. 比如Faster RCNN,1张图生成了16个ROI,需要对每一个ROI进行分类和回归,ROI的维度是7×7×512,长 ...
- linux 从一台机器复制文件到另一台linux机器上去
1.功能说明scp就是security copy,用于将文件或者目录从一个Linux系统拷贝到另一个Linux系统下.scp传输数据用的是SSH协议,保证了数据传输的安全,其格式如下:scp 远程用户 ...
- mapreduce课上测试
今天上课的时候进行了一个mapreduce的实验,但是由于课下对于mapreduce还有hive的理解不够透彻,因此导致了课上没能完成这次实验. 关于本次课堂上的实验的内容大致为: 1.对一个70k的 ...
- 什么是redis事务
一.什么是redis事务? 可以一次性执行多条命令,本质上是一组命令的集合.一个事务中的所有命令都会序列化,然后按顺序地串行化执行,而不会被插入其他命令 二.Redis 事务可以做什么? 一个队列中, ...