最少反链划分数 = 最长链。实现:每次找出所有极大元作为一个反链。

任意长度小于k * (k + 1) / 2的排列都能被划分为不多于k个单调序列。且这是一个紧的上界。

然后这题就可以切了。

题意:给定长为n的排列,设任长为n的排列都能被划分为不多于k个单调序列,把给定排列划分为不多于k个单调序列。

解:先求出k,然后求LIS,如果LIS >= k那么扔掉LIS递归。否则划分成反链即可。用链表实现。

 #include <bits/stdc++.h>

 const int N = , INF = 0x3f3f3f3f;

 int p[N], cnt;
std::vector<int> v[N]; struct Node {
int nex, pre;
}node[N]; int tp, head = N - , tail = N - ; int stk[N], top, f[N], rest, stk2[N], fr[N];
bool vis[N]; inline void del(int x) {
int nex = node[x].nex, pre = node[x].pre;
node[nex].pre = pre;
node[pre].nex = nex;
return;
} inline int getLIS() {
top = ;
for(int i = node[head].nex; i != tail; i = node[i].nex) {
int l = , r = top;
while(l < r) {
int mid = (l + r + ) >> ;
if(stk[mid] < p[i]) {
l = mid;
}
else {
r = mid - ;
}
}
f[i] = r + ;
fr[i] = stk2[r];
stk[r + ] = p[i];
stk2[r + ] = i;
if(r == top) ++top;
}
return top;
} inline void erase() {
int x = stk2[top];
++cnt;
while(x) {
del(x);
v[cnt].push_back(p[x]);
x = fr[x];
}
std::reverse(v[cnt].begin(), v[cnt].end());
return;
} inline void split() { while(rest) {
++cnt;
int last = -;
for(int i = node[tail].pre; i != head; i = node[i].pre) {
if(p[i] > last) {
v[cnt].push_back(i);
last = p[i];
//printf("inside %d \n", i);
rest--;
}
}
std::reverse(v[cnt].begin(), v[cnt].end());
int len = v[cnt].size();
for(int i = ; i < len; i++) {
del(v[cnt][i]);
//printf("now : %d \n", v[cnt][i]);
v[cnt][i] = p[v[cnt][i]];
//printf("now : %d \n", v[cnt][i]);
}
} return;
} inline void solve() {
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &p[i]);
node[i].nex = ;
if(i > ) {
node[i].pre = i - ;
node[i - ].nex = i;
}
else {
node[i].pre = head;
node[head].nex = i;
}
}
node[n].nex = tail;
node[tail].pre = n;
cnt = ;
rest = n;
int K = ;
while(K * (K + ) / <= n) {
++K;
}
//printf("K = %d \n", K);
/// use K-1
while(rest) {
int len = getLIS();
//printf("len = %d \n", len);
if(len >= K) {
erase();
K--;
rest -= len;
}
else {
split();
rest = ;
}
}
printf("%d \n", cnt);
for(int i = ; i <= cnt; i++) {
int len = v[i].size();
printf("%d ", len);
for(int j = ; j < len; j++) {
printf("%d ", v[i][j]);
}
puts("");
v[i].clear();
}
tp = ;
return;
} int main() { int T;
scanf("%d", &T);
while(T--) {
solve();
} return ;
}

AC代码

CF1097E Egor and an RPG game的更多相关文章

  1. 【CF1097E】Egor and an RPG game(动态规划,贪心)

    [CF1097E]Egor and an RPG game(动态规划,贪心) 题面 洛谷 CodeForces 给定一个长度为\(n\)的排列\(a\),定义\(f(n)\)为将一个任意一个长度为\( ...

  2. Codeforces 1097E. Egor and an RPG game 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1097E.html 题解 首先我们求出 $k = f(n) = \max\{x|\frac{x(x+1)}2 ...

  3. Codeforces 1097E. Egor and an RPG game

    传送门 首先考虑怎么算 $f(n)$ (就是题目里面那个 $f(n)$) 发现可以构造一组序列大概长这样: ${1,3,2,6,5,4,10,9,8,7,15,14,13,12,11,...,n(n+ ...

  4. Hello 2019 (D~G)

    目录 Codeforces 1097 D.Makoto and a Blackboard(DP 期望) E.Egor and an RPG game(思路 LIS Dilworth定理) F.Alex ...

  5. codeforces 1097 Hello 2019

    又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...

  6. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  7. 【经典】C++&RPG对战游戏

    博文背景: 还记大二上学期的时候看的这个C++&RPG游戏(博主大一下学期自学的php,涵盖oop内容),一个外校的同学他们大一学的C++,大二初期C++实训要求做一个程序填空,就是这个 RP ...

  8. 2013长沙赛区现场赛 J - Josephina and RPG

    J - Josephina and RPG Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. RPG的错排

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. C/S通信

    一直在考虑写一个服务端和客户端通信的框架,就现在的需求,打算走http协议. 通信方式打算用Key/Value的形式. 这里面其实还是有很多的问题的,这样的一个通信框架其实是SOA的一部分.其他 但是 ...

  2. Day 9 :初识函数

    Python函数:1.函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 2.函数能提高应用的模块性,和代码的重复利用率. Python提供了许多内建函数,比如print().但你也可 ...

  3. 火狐不支持webp格式的图片

    <!DOCTYPE html> <html lang="en"> <style> ul{list-style: none;} li{float: ...

  4. !important的用法及作用

    定义及语法 !important,作用是提高指定样式规则的应用优先权(优先级).语法格式{ cssRule !important },即写在定义的最后面,例如:box{color:red !impor ...

  5. 2018-8-10-win10-uwp-在-Canvas-放一个超过大小的元素会不会被裁剪

    title author date CreateTime categories win10 uwp 在 Canvas 放一个超过大小的元素会不会被裁剪 lindexi 2018-08-10 19:16 ...

  6. RAksmart服务器具备哪些特点?

    美国RAKsmart机房运营多年,前身是提供美国军用服务器业务,拥有着庞大的用户群体和消费者,那RAksmart服务器具备哪些特点呢? 1.美国raksmart服务器特点——硬盘超大 美国raksma ...

  7. Android欢迎页短暂白屏

    在style中application theme下添加以下代码: <item name="android:windowIsTranslucent" >true</ ...

  8. canvas像素的操作

    ###在canvas中的像素操作 到目前为止,我们尚未深入了解Canvas画布真实像素的原理,事实上, 你可以直接通过ImageData对象操纵像素数据,直接读取或将数据数组写入该对象中 ###得到场 ...

  9. 8 包含min函数的栈

    0 引言 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(). 1 抽象问题具体化 2 具体问题抽象分析 需要解 ...

  10. VC 复制移动删除重命名文件文件

    说明: 1.以下封装了4个函数_CopyFile,_DeleteFile,_MoveFile,_ReNameFile 2.每个函数都先拷贝了原来的路径,保证了路径是以2个\0\0结尾.(如果不以2个\ ...