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
随机推荐
- c++使用优先队列时自定义优先出队顺序(和sort)
优先队列也是一种先进先出的数据结构,元素从队尾入队,从队头出队,但是优先队列相较一般队列多了一个判断优先级的功能,在当前队列中,优先级最高的元素将被第一个删除. 先看一下优先队列的定义 templat ...
- Luogu P1730 最小密度路径(最短路径+dp)
P1730 最小密度路径 题面 题目描述 给出一张有 \(N\) 个点 \(M\) 条边的加权有向无环图,接下来有 \(Q\) 个询问,每个询问包括 \(2\) 个节点 \(X\) 和 \(Y\) , ...
- 02Redis入门指南笔记(基本数据类型)
一:热身 获得符合规则的健名列表:keys pattern pattern支持glob风格的通配符,具体规则如下表: Redis命令不区分大小写.keys命令需要遍历Redis中的所有健,当键的数量 ...
- copyTo和clone的区别/制作mask的fillpoly函数(有问题)
OpenCV中mat::copyto( )函数使用方法 OpenCV的fillPoly函数 使用OpenCV库进行图像处理时,经常会用到clone和copyTo函数,这里对两个函数进行介绍. copy ...
- 利用jquery模拟select效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Asio与Boost.Asio
译自http://think-async.com/Asio/AsioAndBoostAsio Asio有两种变体:(非Boost)Asio和Boost.Asio.本文概要描述二者的不同. 1. 源代码 ...
- Django REST Framework之分页器
Django REST Framework提供了三种分页器: PageNumberPagination.基于Django Paginator封装,使得操作更方便,只需要做一些配置即可.分页方式:根据页 ...
- python 单元测试之初次尝试
python 语言中有很多单元测试框架和工具,而unittest单元测试框架作为标准python语言中的一个模块.是其他框架和工具的基础.想要进行单元测试,我们需要使用到unittest框架中的功能. ...
- 推荐一个 Laravel admin 后台管理插件
如何优雅的写代码,我想是每位程序员的心声.自从15年初第一次接触 Laravel 4.2 开始,我就迷上使用 Laravel 框架了.我一直都想找个时间好好写写有关 Laravel 的使用文章,由浅入 ...
- 全球城市群Megalopolis
Megalopolis From Wikipedia, the free encyclopedia (Redirected from Megalopolis (city type)) &quo ...