输出原序列有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. map 的使用 UVA156

    借用这一水题来熟悉map的操作,以前以为看过c++ primer,对map虽然不熟悉但用还是会用的,实际上手发现不是那么回事,还是有很多坑的 #include <iostream> #in ...

  2. element ui table 去掉边框

    // /deep/ .el-table { // thead { // .cell { // text-align: left; // table 表头 左对齐 // } // } // .delet ...

  3. webpack4配置react开发环境

    webpack4大大提高了开发效率,简化了配置复杂度,作为一个大的版本更新,作为一个对开发效率执着的爱折腾的程序员,已经忍不住要尝尝鲜了 首先是cli和webpack的分离,开发webpack应用程序 ...

  4. 读书笔记--Apache.Tomcat.6高级编程 目录

    1.Apache Tomcat 2.web的应用:Servlet.JSP及其他 3.Tomcat安装 4.Tomcat架构 5.Tomcat基本配置 6.高级Tomcat特性 7.Web应用程序配置 ...

  5. WPF绘图性能问题

    代码: /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWind ...

  6. Java问题解读系列之IO相关---Java深拷贝和浅拷贝

    前几天和棒棒童鞋讨论Java(TA学的是C++)的时候,他提到一个浅拷贝和深拷贝的问题,当时的我一脸懵圈,感觉自己学Java居然不知道这个知识点,于是今天研究了一番Java中的浅拷贝和深拷贝,下面来做 ...

  7. linux 下建立桌面快捷方式

    这段时间从windows转到了Linux,发现桌面上没有快捷方式很不适应,找了好久资料,找到解决方法,记录下来以后备用 1.首先建立一个新文件 ``` vi quick.desktop //后缀为de ...

  8. Liferay 7:portlet name

    总结自: https://web.liferay.com/zh/web/user.26526/blog/-/blogs/proper-portlet-name-for-your-portlet-com ...

  9. 稳定性专题 | StackOverFlowError 常见原因及解决方法

    导读 『StabilityGuide』是阿里多位阿里技术工程师共同发起的稳定性领域的知识库开源项目,涵盖性能压测.故障演练.JVM.应用容器.服务框架.流量调度.监控.诊断等多个技术领域,以更结构化的 ...

  10. 2018.8.10 提高B组模拟赛

    T1 阶乘 Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto ProblemSet Description 有n个正 ...