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 ...
随机推荐
- CSP-201609-3 炉石传说
问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Warcraft,简称炉石传说)是暴雪娱乐开发的一款集换式卡牌游戏(如下图所示).游戏在一个战斗棋盘上进行 ...
- DVWA实验之Brute Force(暴力破解)- Low
DVWA实验之Brute Force-暴力破解- Low 这里开始DVWA的相关实验~ 有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2 ...
- 吴裕雄 python 神经网络——TensorFlow 图像处理函数
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile ...
- Idea 隐藏不必要的文件或文件夹 eg:(.idea,.gitignore,*.iml)
在使用Idea的时候,有一些文件是不必要的,可以将他们隐藏起来 方法:打开File–>Settings–>Editor如图,在File Types 中的 Ignore files and ...
- UIView动画的使用
下面介绍三种简单的UIView动画的使用,如果在项目中对动画没有太多“细致化”的设计要求,基本够用了. 一.首尾式动画 说明:如果只是修改控件的属性,使用首尾式动画还是很方便的,如果还需要在动画完成后 ...
- Tomcat认识
Tomcat目录结构: bin:存放启动和关闭的一些脚本 common:共享(部署在该服务器上的一些)jar包 conf:存放服务器的一些配置文件 webapps:部署文件 work:服务器运行时,产 ...
- 13 JavaScript Array对象&Boolean对象&Math对象&RegExp对象
万物皆对象,数组是对象,数组可以存储元素,函数,数组等对象 创建数组的三个方式: 常规方式:var myCars = new Array(); 简介方式:var myCars = new Arrray ...
- 【剑指Offer面试编程题】题目1507:不用加减乘除做加法--九度OJ
题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入为两个整数m和n(1<=m,n<=10 ...
- 【原】Web Polygraph 安装
1.下载 # wget http://www.web-polygraph.org/downloads/srcs/polygraph-4.3.2-src.tgz 2.解压 # tar zxvf poly ...
- 使用myeclipse搭建简单的maven工程
请点击或者复制以下链接 http://opiece.me/2016/03/17/maven-and-ssmframework/