输出原序列有45分……

字典序最小可以和拓扑序联系起来。

根据原来的题意不是很可做,于是对原序列求逆,令q[p[i]]=i;

那么就成功将题意转化:相邻元素值的差大于等于k时可以交换,使序列字典序最小。

考虑一下$n^2$怎么做,对于$i<j$,如果$abs(q[i]-q[j])<k$,那么q[i]和q[i]的大小关系不会发生变化,那么连一条$q[i]->q[j]$的边表示q[i]在q[j]之前,跑拓扑排序就可以了。

考虑优化,其实做过很多这种题了(‘炸弹’),图中会存在$A->B,B->C,A->C$之类的边,显然$A->C$可以去掉。怎么实现呢?对于一个q[i],是向右边差小于k的连边,那么其实只需要连两条边,即离他最近的大于他的和小于他的,那么其它的点也会被他们限制。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
struct edge
{
int u,v,nxt;
#define u(x) ed[x].u
#define v(x) ed[x].v
#define n(x) ed[x].nxt
}ed[];
int first[],num_e;
#define f(x) first[x]
int n,k,p[],q[],du[];
struct TREE
{
struct tree
{
int l,r,ls,rs,minn;
#define l(x) tr[x].l
#define r(x) tr[x].r
#define ls(x) tr[x].ls
#define rs(x) tr[x].rs
#define minn(x) tr[x].minn
}tr[];
int cnt,root;
void build(int &x,int l,int r)
{
if(!x)x=++cnt;
l(x)=l,r(x)=r,minn(x)=0x7fffffff;
if(l==r)return;
int mid=(l+r)>>;
build(ls(x),l,mid);
build(rs(x),mid+,r);
}
void add(int x,int pos,int y)
{
// cout<<x<<" "<<l(x)<<" "<<r(x)<<" "<<pos<<" "<<y<<endl;
if(l(x)==r(x)){minn(x)=y;return;}
int mid=(l(x)+r(x))>>;
if(pos<=mid)add(ls(x),pos,y);
else add(rs(x),pos,y);
minn(x)=min(minn(ls(x)),minn(rs(x)));
}
int ask(int x,int l,int r)
{
if(l(x)>=l&&r(x)<=r)return minn(x);
int mid=(l(x)+r(x))>>,ans=0x7fffffff;
if(l<=mid)ans=min(ans,ask(ls(x),l,r));
if(r> mid)ans=min(ans,ask(rs(x),l,r));
return ans;
}
}T;
inline void add(int u,int v);
signed main()
{
cin>>n>>k;T.build(T.root,,n);
for(int i=;i<=n;i++)
cin>>p[i],q[p[i]]=i; // for(int i=1;i<=n;i++)cout<<q[i]<<" ";puts("");
for(int i=n;i;--i)
{
int t1=T.ask(,max(,q[i]-k+),min(n,q[i]-)),
t2=T.ask(,max(,q[i]+),min(n,q[i]+k-));
// cout<<i<<" "<<t1<<" "<<t2<<" "<<q[i]<<endl;
if(t1!=0x7fffffff)add(q[i],q[t1]),du[q[t1]]++;
if(t2!=0x7fffffff)add(q[i],q[t2]),du[q[t2]]++;
T.add(,q[i],i);
} priority_queue<int,vector<int>,greater<int> >que;
for(int i=;i<=n;i++)if(!du[i])que.push(i);
int cnt=;
while(!que.empty())
{
int x=que.top();q[++cnt]=x;que.pop();
for(int i=f(x);i;i=n(i))
{
du[v(i)]--;
if(!du[v(i)])que.push(v(i));
}
}
for(int i=;i<=n;i++)p[q[i]]=i; for(int i=;i<=n;i++)cout<<p[i]<<endl;
}
inline void add(int u,int v)
{
// cout<<"add: "<<u<<" "<<v<<endl;
++num_e;
u(num_e)=u;
v(num_e)=v;
n(num_e)=f(u);
f(u)=num_e;
}

%%mikufun权值线段树优化建边。

HZOJ Permutation的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. Permutation test: p, CI, CI of P 置换检验相关统计量的计算

    For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

随机推荐

  1. shiro+jwt+springboot理解

    转自 https://www.cnblogs.com/fengli9998/p/6676783.html https://www.jianshu.com/p/0366a1675bb6 https:// ...

  2. Leetcode113. Path Sum II路径总和2

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...

  3. ThinkPHP实现导出Excel表格

    /** * 导出数据为excel表格 *@param $data 一个二维数组,结构如同从数据库查出来的数组 *@param $title excel的第一行标题,一个数组,如果为空则没有标题 *@p ...

  4. 使用pdf.js在移动端预览pdf文档

    pdf.js 是一个技术原型主要用于在 HTML5 平台上展示 PDF 文档,无需任何本地技术支持. 在线演示地址:http://mozilla.github.com/pdf.js/web/viewe ...

  5. js中的定义变量之①用不用var

    var 是js定义变量的意思. 由于js中的变量是弱类型的,因此js中的所有变量包括number(数字型).string(字符串类型).boolean(布尔类型,true和false)等均通过var关 ...

  6. CSP-S模拟 - 20190916

    这是一套题=.= ABC D1DEF D2 过程-Process Before T1 像DP 迷茫…… T2 像二/三分 T3 不知道惹 可以DP($30\%$) During T1 先打个暴力$N^ ...

  7. JS---案例:滚动条

    案例:滚动条 html框架分为4部分,最外面的div, 放文字的div, 装滚动条的div层,以及滚动条本身放在一个div里面 <!DOCTYPE html> <html> & ...

  8. C++使用stringstream分割字符串

    在这里查看getline的函数声明如下: 可以看到,第三个参数delim是分隔符,可以指定不同的分隔符,如果不指定的话就默认是'\n'. 举个例子:

  9. windows--"git安装" 及 "使用git上传项目到github" 详细步骤

    一.下载安装包 https://git-for-windows.github.io/(放在任何一个你想放的地方(系统盘或非系统盘)). 二.开始安装 很简单,双击安装包,一直next下去,到了安装的最 ...

  10. Mac系统常用快捷键大全

    苹果Mac系统常用快捷键有很多,但是很多童鞋对于这些mac快捷键都不是很熟悉,今天小编为大家整理了一份Mac系统常用快捷键大全,大家快收藏起来吧!平时在使用mac系统的时候可以提高不少工作效率哦! M ...