这道题和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)的更多相关文章

  1. Codeforces 1296E1 - String Coloring (easy version)

    题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...

  2. E2. String Coloring (hard version)(贪心)

    E2. String Coloring (hard version) time limit per test 1 second memory limit per test 256 megabytes ...

  3. E1. String Coloring (easy version)(贪心)

    E1. String Coloring (easy version) time limit per test 1 second memory limit per test 256 megabytes ...

  4. Codeforces 1027E Inverse Coloring 【DP】

    Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...

  5. Codeforces Round #617 (Div. 3) String Coloring(E1.E2)

    (easy version): 题目链接:http://codeforces.com/contest/1296/problem/E1 题目一句话就是说,两种颜色不同的字符可以相互换位, 问,对这字符串 ...

  6. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  7. Codeforces 799D. String Game 二分

    D. String Game time limit per test:2 seconds memory limit per test:512 megabytes input:standard inpu ...

  8. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  9. CodeForces 159c String Manipulation 1.0

    String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Cod ...

随机推荐

  1. DVWA实验之Brute Force(暴力破解)- Medium

    DVWA实验之Brute Force(暴力破解)- Medium   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2046/p/10928380. ...

  2. 吴裕雄 python 神经网络——TensorFlow 变量管理

    import tensorflow as tf with tf.variable_scope("foo"): v = tf.get_variable("v", ...

  3. Cat4500升级注意事项

    第一部分:升级ROMMON 可以通过release note去查看什么Supervisor建议什么样的版本.例如:ROMMON Release Notes for the WS-X45-SUP7-E, ...

  4. 【原】简单shell练习(五)

    1.查询一个文件中某个单词出现的次数 # grep -o 'ts' ./test/txt |wc -l 2.统计当前文件夹下文件的个数 ls -l |grep "^-"|wc -l ...

  5. Oracle的TO_CHAR()格式化数字为百分数的字符串

    TO_CHAR(-0.926903249,'FM999999990.00') || '%' 说明: 1,(点号) :'99.99' : 点号,不要念为"句号",句号是个圆圈,点号只 ...

  6. cmake 环境安装与使用

    CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的 ...

  7. 洛谷 CF804B Minimum number of steps

    嗯... 题目链接:https://www.luogu.org/problemnew/show/CF804B 这道题没有什么技巧,只是一道找规律的题. 首先看到“ab”可以换成“bba”,所以首先要确 ...

  8. 「AHOI2014/JSOI2014」拼图

    「AHOI2014/JSOI2014」拼图 传送门 看到 \(n \times m \le 10^5\) ,考虑根号分治. 对于 \(n < m\) 的情况,我们可以枚举最终矩形的上下边界 \( ...

  9. SpringBoot 集成JUnit

    项目太大,不好直接测整个项目,一般都是切割成多个单元,单独测试,即单元测试. 直接在原项目上测试,会把项目改得乱七八糟的,一般是单独写测试代码. 进行单元测试,这就需要集成JUnit. (1)在pom ...

  10. DVWA靶机--简单的文件上传漏洞

    简单的文件上传漏洞(靶机安全级别:low) 事先准备好一句话木马,密码为pass 上传一句话木马,显示上传路径(一般网站是不会显示路径的,这里靶机为了方便你测试漏洞,直接显示出了路径: ../../h ...