HZOJ Permutation
输出原序列有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的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 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 ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 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 ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
随机推荐
- AGC035D
AGC035D Add and Remove 题意 给出\(n\)个数,每次删除一个不在两端的数,然后把它的权值加到相邻的两个数上. 问操作\(n-2\)次后,所剩的两数之和的最小值 \(n\le18 ...
- 基于PHP的一种Cache回调与自动触发技术
$s = microtime(true); for($i=0; $iaaa($array, $array, $array); $data = a::bbb($array, $array, $array ...
- PHP1.9--数组
1.array_slice()函数作用是在数组中根据条件取出一段值并返回,如果数组有字符串键,所返回的数组将保留健名 array array_slice(array array ,int offset ...
- git与github建立链接(将本次项目与网络GitHub同步) --转存笔记
转载自:https://blog.csdn.net/qq_36529459/article/details/79047220 1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可 ...
- 前端路由的实现(三) —— History的pushState和replaceState用法
HTML5中history提供的pushState, replaceState这两个API.它们提供了操作浏览器历史栈的方法. pushState能够在不加载页面的情况下改变浏览器的URL.这个方法接 ...
- windows 标准错误重定向
最近在windows上运行tensorflow的时候,出现很多stderr 的信息,干扰了正常的输出:所以我们需要使用操作把这些输出屏蔽: 参考链接:https://support.microsoft ...
- Java问题解读系列之String相关---String类的常用方法?
今天的题目是:String类的常用方法? 首先,我们在eclipse中定义一个字符串,然后使用alt+/就会出现String类的所有方法,如下图所示: 下面我就挑选一些常用的方法进行介绍: 首先定义两 ...
- css不定高图文垂直居中的三种方法
html部分 <div class="box"> <img class="img" src="http://p2.so.qhmsg. ...
- Django项目:CRM(客户关系管理系统)--08--03PerfectCRM创建基本数据03
如果感觉本章博客对您有帮助,请尽情打赏吧!
- python安装第三方模块
1.pip 安装命令: pip install 模块名由于国外网站不稳定可能会出现超时的情况,我们可以自己指定下载源命令如下临时修改 pip install 模块名 -i https://pypi.t ...