输出原序列有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. 2018-8-10-WPF-使用不安全代码快速从数组转-WriteableBitmap

    title author date CreateTime categories WPF 使用不安全代码快速从数组转 WriteableBitmap lindexi 2018-08-10 19:16:5 ...

  2. Ubuntu修改mysql编码格式

    今天在Ubuntu系统上部署了第一个net core的web网站,遇到了mysql入库数据乱码的情况.无奈,ubuntu系统不熟悉,mysql命令不熟悉,只得在网上查找各种资料.还是老规矩,主要参考的 ...

  3. win10系统下安装打印机驱动

    以前安装过一次打印机的驱动,当时是从网上下载的,今天按照以前的方法安装打印机驱动,发现并不能使用,而且并不知道驱动还能自动安装. 首先在系统图标下选择设置-设备和打印机-添加打印机-搜索打印机,如果没 ...

  4. Django项目:CRM(客户关系管理系统)--22--14PerfectCRM实现King_admin分页的省略显示

    {#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...

  5. java并发系列(七)-----ConcurrentHashMap原理分析(JDK1.8)

    JDK1.8的实现已经摒弃了Segment的概念,而是直接用Node数组+链表+红黑树的数据结构来实现,并发控制使用Synchronized和CAS来操作,整个看起来就像是优化过且线程安全的HashM ...

  6. Mac Eclipse上Android SDK manager闪退的问题!!

    最近想自学一下Android,也没啥人指导,安装的过程中就花了一整天....安装完ADT,安装完SDK,所有步骤都照着网上来,可是一打开SDK manager就闪退!网上所有方法都找了,可是几乎全是w ...

  7. LINUX访问文件配置

    访问文件 /etc/host.conf 告诉网络域名服务器如何查找主机名.(通常是 /etc/hosts,然后就是名称服务器:可通过 netconf 对其进行更改) /etc/hosts 包含(本地网 ...

  8. Liferay 7.1发布啦

    下载地址: https://cdn.lfrs.sl/releases.liferay.com/portal/7.1.0-m1/liferay-ce-portal-tomcat-7.1-m1-20180 ...

  9. 火狐下button标签子元素无法点击

    button下元素点击事件:在chrome和safari下每个a标签可以点击,在火狐下a标签无法点击. <button> <a href="javascript:;&quo ...

  10. FPGA按键功能

    1.如何判断按键成功按下? 2.在什么时候采集数据? 按键在按下的过程中会产生大约2ms-3ms抖动,如果此时此刻采集数据来判断按键是不准确的,那么为了采集到准确的数据需要设置一个大约10ms左右的计 ...