Codeforces Gym 101623A - 动态规划
考虑将相邻的相等的数缩成一个数。
假设没有分成了$n$段,考虑最少能够减少多少划分。
我们将这个序列排序,对于权值相同的一段数可以任意交换它们,每两个相邻数在原序列的位置中如果是$i, i + 1$,那么划分的段数就可以减少1.
每次转移我们考虑添加值相同的一段。
每次转移能不能将减少的段数加一取决于当前考虑的数在前一段内有没有出现以及有没有作为最左端点。
因此我们记录一个决策与最优解不同的次优解就能转移了。
Code
/**
* Codeforces
* Gym#101623A
* Accepted
* Time: 171ms
* Memory: 18300k
*/
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef bool boolean; #define pii pair<int, int>
#define fi first
#define sc second ostream& operator << (ostream& out, pii x) {
out << "(" << x.fi << ", " << x.sc << ")";
return out;
} template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} template <typename T>
void pcopy(T* pst, const T* ped, T *pval) {
for ( ; pst != ped; *(pst++) = *(pval++));
} int n;
int *ar;
pii *ps; inline void init() {
scanf("%d", &n);
ar = new int[(n + )];
for (int i = ; i <= n; i++)
scanf("%d", ar + i);
} int *ss, *st;
boolean *exi;
inline void solve() {
ps = new pii[(n + )];
ss = new int[(n + )];
st = new int[(n + )];
exi = new boolean[(n + )];
pfill(exi, exi + n + , false);
int m = , diff = ;
for (int i = ; i <= n; i++)
if (i == || ar[i] != ar[i - ])
ps[++m] = pii(ar[i], ++diff);
sort(ps + , ps + (n = m) + );
// for (int i = 1; i <= m; i++)
// cerr << ps[i] << endl;
ss[] = ;
for (int i = ; i <= n; i++)
ss[i] = ((ps[i - ].first == ps[i].first) ? (ss[i - ]) : (i));
st[n] = n;
for (int i = n - ; i; i--)
st[i] = ((ps[i + ].first == ps[i].first) ? (st[i + ]) : (i)); ss[] = st[] = ;
pii f(, -), g(-, -), cf(-, -), cg(-, -);
for (int i = ; i <= n; i++) {
if (ss[i] != i)
continue;
for (int j = max(ss[i - ], ); j <= st[i - ]; j++)
exi[ps[j].second] = true;
for (int j = ss[i], x, uval; j <= st[i]; j++) {
x = ps[j].second;
if (exi[x - ]) {
if (x - == f.second && st[i - ] > ss[i - ])
assert(x - != g.second), uval = g.first + ;
else
uval = f.first + ;
uval = max(uval, f.first);
} else
uval = f.first;
if (uval > cf.first)
cg = cf, cf = pii(uval, x);
else if (x != cg.second && uval > cg.first)
cg = pii(uval, x);
}
for (int j = max(ss[i - ], ); j <= st[i - ]; j++)
exi[ps[j].second] = false;
swap(cf, f);
swap(cg, g);
cf = pii(-, -), cg = pii(-, -);
// cerr << f << " " << g << endl;
}
printf("%d\n", n - f.first - );
} int main() {
init();
solve();
return ;
}
Codeforces Gym 101623A - 动态规划的更多相关文章
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
随机推荐
- js 获取某个某个区间内的随机整数
//获取某个某个区间内的随机整数 ,获取到的值域为[min,max)function get_random_num(min,max){ if(/^-?\d+$/.test(min) && ...
- odoo按钮触发下载文件
测试环境:Odoo8.0 1.在你的模块中创建一个方法,返回url 举个例子, @api.multi def get_stock_file(self): return{'type':'ir.actio ...
- 读高性能MySql笔记
1.1 MySQL逻辑架构 MySql服务器逻辑架构图 1.连接管理与安全性 每个客户端连接都会在服务器进程中拥有一个线程,这个连接的查询只会在这个单独的线程中执行,该线程只能轮流在某个CPU核心或者 ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- MongoDB3.2新特性之部分索引
官方介绍:https://docs.mongodb.org/manual/core/index-partial/ mongodb3.2支持对某个集合的部分数据创建索引.如给年龄大于十八岁的数据创建索引 ...
- 接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告
这套框架的报告是自己封装的 由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类 ...
- ZIP压缩指定路径
using Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using Quartz.Impl; us ...
- nuxt 2
原文出处:
- JS中将json字符串转为json对象的三种方式
第一种:利用JSON的parse方法,即jsonObj=JSON.parse(jsonStr); 第二种:jsonObj = eval('(' + jsonStr+ ')'); 第三种:比较难理解:j ...
- Liunx系统升级自带的Python版本
一.查看系统信息 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.4 (Final) [root@localhost ~]# ...