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

任意长度小于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. iOS开发之UIMenuController

    1.简介 用于剪切.复制.粘贴.选择.选择ALL和DELETE命令的菜单界面. UITextField.UIWebView.UITextView自带有这种UIMenuController效果: 2.相 ...

  2. webstorm快捷键、支持vue文件等部分使用技巧

    转载:https://www.cnblogs.com/seven077/p/9771474.html 1.常用快捷键 shift+↑ 向上选取代码块shift+↓ 向下选取代码块ctrl+/ 注释/取 ...

  3. Redis基于主从复制的RCE(redis4.x 5.x)复现

    使用docker建立redis 拉取镜像 运行 查看 可以连接,存在未授权 https://github.com/Ridter/redis-rce 发送poc i:正向连接 r:反弹 反弹成功

  4. 剑指offer——10跳台阶演变

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   题解: 纯找规律题:   class Solution { public: ...

  5. 20130318 word2013 mathtype

    1.word2013 下如何安装mathtype 1.word2013已经装好 2.下载mathtype6.9 3.   公式编辑器Mathtype安装后无法加载到word的解决办法http://w5 ...

  6. Victor and String HDU - 5421 双向回文树

    题意: 有n种操作,开始给你一个空串,给你4中操作 1 c  在字符串的首部添加字符c 2 c  在字符串的尾部添加字符c 3  询问字符中的本质不同的回文串的个数 4 询问字符串中回文串的个数 思路 ...

  7. STM32---初学者用库函数好还是直接对寄存器操作比较好

    引用:http://blog.csdn.net/u010349006/article/details/416 首先,两个都是C语言.从51过渡过来的话,就先说寄存器操作.每个MCU都有自己的寄存器,5 ...

  8. mysql 12章_MySQL数据库的高级管理

    一. 用户管理 Root用户是MySQL数据库管理系统中的系统管理员,但在实际开发过程中通常需要根据不同的开发者分配不同的用户,这样有利于用户的管理和维护. . 用户的创建: ) 方式1:使用MySQ ...

  9. iOS开发系列-Lock

    概述 我们在使用多线程的时候多个线程可能会访问同一块资源,这样就很容易引发数据错乱和数据安全等问题,这时候就需要我们保证每次只有一个线程访问这一块资源,锁 应运而生. iOS中锁之前的性能的图标排行: ...

  10. nginx 知识

    nginx如何实现高并发? 启动nginx服务器后,输入 ps -ef |grep nginx,会发现nginx有一个master进程 和若干个worker进程, 这些worker进程是平等的,都是被 ...