原文链接https://www.cnblogs.com/zhouzhendong/p/CF1097E.html

题解

首先我们求出 $k = f(n) = \max\{x|\frac{x(x+1)}2\leq n\}$ 。

具体构造方案是:(以 $n = 15$ 为例)

11 12 13 14 15          7 8 9 10        4 5 6        2 3       1

我们考虑如何构造。

求出当前序列的 LIS 长度(假设为 $len$)。如果 $len\geq k$ ,那么直接取出这个LIS,把问题转化成更小规模的问题: $n^\prime = n-len\leq n-k$ 而且 $k^\prime \leq k-1$ 。

否则由dilworth引理得到一定可以把序列分成 $len$ 个递减序列。考虑按照以每一个点为结尾的上升序列长度将所有分组,对于同一组的按顺序连起来,这样得到 $len$ 组就好了。证明的比较简单:如果不是递减的,那么后一个的值至少是前一个+1,他们的就不会被分到同一组。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL read(){
LL x=0;
char ch=getchar();
while (!isdigit(ch))
ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x;
}
const int N=100005;
int T,n,m;
int a[N],v[N],p[N],vis[N];
int f(int n){
int ans=0;
while (ans*(ans+1)<=n*2)
ans++;
return ans-1;
}
int c[N];
void Add(int x,int id){
for (;x<=n;x+=x&-x)
c[x]=v[c[x]]<v[id]?id:c[x];
}
int Ask(int x){
int ans=0;
for (;x;x-=x&-x)
ans=v[ans]<v[c[x]]?c[x]:ans;
return ans;
}
vector <vector <int> > ans;
vector <int> vec_empty;
void Main(){
ans.clear();
n=read();
for (int i=1;i<=n;i++)
a[i]=read();
m=n;
while (m>0){
int k=f(m);
// cout<<"k="<<k<<endl;
for (int i=0;i<=n;i++)
c[i]=0;
for (int i=1;i<=m;i++){
vis[i]=0;
p[i]=Ask(a[i]);
v[i]=v[p[i]]+1;
Add(a[i],i);
}
int tail=Ask(n),len=v[tail];
if (len>k){
ans.push_back(vec_empty);
for (int i=tail;i;i=p[i])
vis[i]=1,ans.back().push_back(a[i]);
reverse(ans.back().begin(),ans.back().end());
int _m=0;
for (int i=1;i<=m;i++)
if (!vis[i])
a[++_m]=a[i];
m=_m;
}
else {
int c=(int)ans.size()-1;
for (int i=1;i<=len;i++)
ans.push_back(vec_empty);
for (int i=1;i<=m;i++)
ans[c+v[i]].push_back(a[i]);
break;
}
}
printf("%d\n",(int)ans.size());
for (auto s : ans){
printf("%d",(int)s.size());
for (auto v : s)
printf(" %d",v);
puts("");
}
}
int main(){
T=read();
while (T--)
Main();
return 0;
}

  

Codeforces 1097E. Egor and an RPG game 构造的更多相关文章

  1. 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+ ...

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

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

  3. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  4. Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

    Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  5. Codeforces 791C. Bear and Different Names 模拟构造

    C. Bear and Different Names time limit per test:1 second memory limit per test:256 megabytes input:s ...

  6. Codeforces Round #339 (Div. 1) C. Necklace 构造题

    C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...

  7. Codeforces - 814B - An express train to reveries - 构造

    http://codeforces.com/problemset/problem/814/B 构造题烦死人,一开始我还记录一大堆信息来构造p数列,其实因为s数列只有两项相等,也正好缺了一项,那就把两种 ...

  8. Codeforces Gym101341I:Matrix God(随机化构造矩阵降维)***

    http://codeforces.com/gym/101341/problem/I 题意:给三个N*N的矩阵,问a*b是否等于c. 思路:之前遇到过差不多的题目,当时是随机行(点),然后验证,不满足 ...

  9. Codeforces 1163E Magical Permutation [线性基,构造]

    codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...

随机推荐

  1. 【ARC101F】Robots and Exits 树状数组

    题目大意 有 \(n\) 个机器人和 \(m\) 个出口. 这 \(n\) 个机器人的初始位置是 \(a_1,a_2,\ldots,a_n\),这 \(m\) 个出口的位置是 \(b_1,b_2,\l ...

  2. 测试常用Linux命令

    大家应该经常在网络上看到下图吧,虽然我们不会去执行下面图片中的命令,但是linux常用的命令对于测试人员来说,还是必须掌握的,不管是做功能测试还是性能测试,最常用的就是看日志了. sudo是linux ...

  3. Nginx禁止IP直接访问网站

    禁止别人直接通过IP访问网站,在nginx的server配置文件前面加上如下的配置,如果有通过IP直接访问的,直接拒绝连接(需要去掉别的server下的default_server). server ...

  4. input输入框自动获取焦点

    只要在该input标签后添加autofocus="autofocus"即可 代码实例: <html> <head></head> <bod ...

  5. Day041--CSS, 盒模型, 浮动

    内容回顾 表单标签 input type text 普通的文本 password 密码 radio 单选  默认选中添加checked 互斥的效果 给radio标签添加 相同的name checkbo ...

  6. vue---组件间通信

    vue中比较重要的就是组件了.而组件随处可复用的特性,使得组件通信非常重要.那么组件之间通讯方式有哪些呢? 第一种:父子组件通讯: 如果是 html页面 中全局注册的组件 和 实例中局部注册的组件 H ...

  7. springboot+mybatis+cucumber

    import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucu ...

  8. bootstrap浅谈

    学习地址:http://www.runoob.com/bootstrap/bootstrap-tutorial.html 自己练习了下 主要是使用了bootstrap中包含的class类样式,只要给相 ...

  9. Linux动态库生成与使用指南

    相关阅读: Linux静态库生成指南 Linux下动态库文件的文件名形如 libxxx.so,其中so是 Shared Object 的缩写,即可以共享的目标文件. 在链接动态库生成可执行文件时,并不 ...

  10. luogu 3166 组合与gcd(数三角形)结论

    在n*m的点格图中选取三个点满足三角形的个数 结论:点(x1,y1)和(x2,y2) 中间有gcd(x2-x1,y2-y1)+1个和两点连成的线段直线共线 那么大力枚举 x2-x1和y2-y1,然后发 ...