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\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 构造的更多相关文章
- 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+ ...
- 【CF1097E】Egor and an RPG game(动态规划,贪心)
[CF1097E]Egor and an RPG game(动态规划,贪心) 题面 洛谷 CodeForces 给定一个长度为\(n\)的排列\(a\),定义\(f(n)\)为将一个任意一个长度为\( ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- 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 ...
- Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...
- Codeforces - 814B - An express train to reveries - 构造
http://codeforces.com/problemset/problem/814/B 构造题烦死人,一开始我还记录一大堆信息来构造p数列,其实因为s数列只有两项相等,也正好缺了一项,那就把两种 ...
- Codeforces Gym101341I:Matrix God(随机化构造矩阵降维)***
http://codeforces.com/gym/101341/problem/I 题意:给三个N*N的矩阵,问a*b是否等于c. 思路:之前遇到过差不多的题目,当时是随机行(点),然后验证,不满足 ...
- Codeforces 1163E Magical Permutation [线性基,构造]
codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...
随机推荐
- mysql-python安装时EnvironmentError: mysql_config not found
mysql-python安装时EnvironmentError: mysql_config not found 在安装 mysql-python时,会出现: 复制代码 sh: mysql_config ...
- luogu1117 优秀的拆分 (后缀数组)
考虑分别计算每个位置作为AA的末尾或者BB的开头的个数 最后乘一乘就是答案 据说是套路的计算AA的方法: 首先枚举A的长度L,然后每L个字符当做一个关键点,这样的话,一个AA包含且只包含相邻两个关键点 ...
- Python多进程与单进程效率对比
运行环境:Python3 in win10 先生成200个测试文件 # generate.py i = 0 while i < 200: o = open("test\\" ...
- Mycat的读写分离
1. Mycat实现读写分离的部署: https://www.cnblogs.com/softidea/p/5447566.html springboot动态数据源的原理以及配置: Spring内置了 ...
- JavaScript数据类型 Boolean布尔类型
前言 布尔值Boolean类型可能是三种包装对象Number.String和Boolean中最简单的一种.Number和String对象拥有大量的实例属性和方法,Boolean却很少.从某种意义上说, ...
- MyBatis集成到Spring时配置MapperScannerConfigurer出错
问题描述 在web项目中同时集成了spring mvc和mybatis. 将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入. ...
- [物理学与PDEs]第3章习题4 理想磁流体的能量守恒方程
试证明: 对理想磁流体, 能量守恒方程 (4. 14) 可以写为如下形式: $$\beex \bea \cfrac{\p}{\p t}&\sex{\rho e+\cfrac{1}{2}\rho ...
- [物理学与PDEs]第3章习题1 只有一个非零分量的磁场
设磁场 ${\bf H}$ 只有一个非零分量, 试证明 $$\bex ({\bf H}\cdot\n){\bf H}={\bf 0}. \eex$$ 证明: 不妨设 ${\bf H}=(0,0,H_3 ...
- vue使用md5,base64方法
在前端加密代码虽然对安全没有提高,但是可以避免明文传输,提供用户隐私保护,还是很有必要的. 首先安装js-md5,js-base64. 在vue中引入. 之后就可以直接使用了,一般的做法是先把密码转行 ...
- Spring MVC 使用问题与解决--HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
1.HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 解决 使用jre1.7 Spring4.3 2.spr ...