CF1097E Egor and an RPG game
最少反链划分数 = 最长链。实现:每次找出所有极大元作为一个反链。
任意长度小于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的更多相关文章
- 【CF1097E】Egor and an RPG game(动态规划,贪心)
[CF1097E]Egor and an RPG game(动态规划,贪心) 题面 洛谷 CodeForces 给定一个长度为\(n\)的排列\(a\),定义\(f(n)\)为将一个任意一个长度为\( ...
- 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 ...
- 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+ ...
- Hello 2019 (D~G)
目录 Codeforces 1097 D.Makoto and a Blackboard(DP 期望) E.Egor and an RPG game(思路 LIS Dilworth定理) F.Alex ...
- codeforces 1097 Hello 2019
又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...
- 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 ...
- 【经典】C++&RPG对战游戏
博文背景: 还记大二上学期的时候看的这个C++&RPG游戏(博主大一下学期自学的php,涵盖oop内容),一个外校的同学他们大一学的C++,大二初期C++实训要求做一个程序填空,就是这个 RP ...
- 2013长沙赛区现场赛 J - Josephina and RPG
J - Josephina and RPG Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- RPG的错排
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
随机推荐
- HDU1595-find the longest of the shortest-dijkstra+记录路径
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do ...
- 20130318 word2013 mathtype
1.word2013 下如何安装mathtype 1.word2013已经装好 2.下载mathtype6.9 3. 公式编辑器Mathtype安装后无法加载到word的解决办法http://w5 ...
- Mybatis 使用的 9 种设计模式,真是太有用了~
Java技术栈 ) { name = fullname.substring(0, delim); children = fullname.substring(delim + 1); ...
- JAVA读取PROPERTIES文件方式一
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- java OOP第二章_封装
一. 封装: 属性通过private访问修饰符将其设置为私有的,只有当前类中可以访问,同时提供通过public访问修饰符的公共方法可以给任何类中访问. 通常针对属性提供公共的setter方法进行赋值, ...
- Codeforces 553E Kyoya and Train
题目大意 链接:CF533E 给一张\(n\)个点,\(m\)条边的图,起点\(1\)终点\(n\),如果不能在\(T\)的时间内到达则需支付\(X\)的代价. 走每条边都会支付一定代价,经过一条边\ ...
- double to string 损失精度的问题
https://blog.csdn.net/magieclarence/article/details/6792511?utm_source=blogxgwz0 类似于这样 double 的精度是一个 ...
- [JZOJ6344] 【NOIP2019模拟2019.9.7】Huge Counting
题目 题目大意自己看题去-- 正解 比赛时在刚第二题,所以根本没有时间思考-- 模型可以转化为从\((x_1,x_2,..,x_n)\)出发到\((1,1)\)的方案数模\(2\). 方案数就用有重复 ...
- 匈牙利算法dfs模板 [二分图][二分图最大匹配]
最近学了二分图最大匹配,bfs模板却死活打不出来?我可能学了假的bfs 于是用到了dfs模板 寻找二分图最大匹配的算法是匈牙利算法 匈牙利算法的主要程序是寻找增广路 寻找增光路是过程是:从一个未经配对 ...
- Responder对象
Responder对象 响应者是一个对象,它可以响应事件并处理它们.所有响应者对象是类的,最终从UIResponder的( IOS)或NSResponder ( OS X)继承实例.这些类声明一个编程 ...