这道题和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. MDC 输出线程信息帮助定位问题

    log4j中的%x ---NDC,%X---MDC 即%x NDC.clear();NDC.push(this.toString());%X{first} %X{last}MDC.put(" ...

  2. spring boot jpa 复杂查询 动态查询 连接and和or 模糊查询 分页查询

    最近项目中用到了jpa,刚接触的时候有些激动,以前的到层忽然不用写sql不用去自己实现了,只是取个方法名就实现了,太惊艳了,惊为天人,但是慢慢的就发现不是这么回事了,在动态查询的时候,不知道怎么操作了 ...

  3. netty实现websocket客户端(附:测试服务端代码)

    1,客户端启动类 package test3; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import ...

  4. 推荐一款好用的博客离线编辑工具——OpenLiveWriter

    1.前言 我们自己一般在写博客的时候都是在博客官网后台写的,但是如果要在多个平台发布博客的话,那就要复制好前面写好的博客,然后再去其它博客平台发布,可见非常的麻烦. 这里推荐一款好用的离线多功能,多平 ...

  5. HDFS核心类FileSystem的使用

    一.导入jar包 本次使用的是eclipse操作的,所以需要手动导入jar包 在Hadoop.7.7/share/hadoop里有几个文件夹 common为核心类,此次需要引入common和hdfs两 ...

  6. 「JSOI2010」旅行

    「JSOI2010」旅行 传送门 比较妙的一道 \(\text{DP}\) 题,思维瓶颈应该就是如何确定状态. 首先将边按边权排序. 如果我们用 \(01\) 串来表示 \(m\) 条边是否在路径上, ...

  7. 操作系统OS,Python - 生产者消费者模型

    1. 缓冲区(此处用阻塞队列充当),解决消费者和生产者强耦合问题.(生产者和消费者不直接通信) 2. 通过平衡生产者线程和消费者线程,来提高程序整体处理数据速度. 3. 在并发编程中该模式能解决大多数 ...

  8. 【协作式原创】自己动手写docker之run代码解析

    linux预备知识 urfave cli预备知识 准备工作 阿里云抢占式实例:centos7.4 每次实例释放后都要重新安装go wget https://dl.google.com/go/go1.1 ...

  9. T-SQL常用的函数

    http://blog.sina.com.cn/s/blog_4af01cd50100hsac.html

  10. c# 字符串比较优化

    一,优化举例 二,浅谈StringComparison 三,C# CultureInfo 类 各国语言对应的区域性名称 一,优化举例 我们在写程序的时候,经常会用到字符串对比.例如:if(IsChec ...