P1963 [NOI2009]变换序列 倒叙跑匈牙利算法
题意
构造一个字典序最小的序列T,使得 Dis(i, Ti) = di,其中i是从0开始的,Dis(x,y)=min{∣x−y∣,N−∣x−y∣} ,di由题目给定。
思路
二分图匹配,把左边的看成i,右边看成Ti,对于固定的i和d,Ti是由两种可能的,连上有向边即可。
至于字典序要最小,怎么做呢,我们可以反着跑匈牙利算法,就是从n-1跑到0,这样小一点的i,可以直接拿走大一点的i刚匹配的较小的值。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e4+;
struct E{
int v,nxt;
}edge[*maxn];
int head[maxn],gtot;
void addedge(int u,int v){
edge[gtot].v = v;
edge[gtot].nxt = head[u];
head[u] = gtot++;
}
int vis[maxn],pt[maxn],py[maxn];
bool dfs(int u){
for(int i=head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v;
if(vis[v] == ){
vis[v] = ;
if(pt[v] == || dfs(pt[v])){
pt[v] = u;
py[u] = v;
return true;
}
}
}
return false;
}
int main(){
int n;
scanf("%d", &n);
memset(head, -, sizeof(head));
priority_queue<int>que;
while(!que.empty()) que.pop();
for(int i=; i<n; i++){
int d; scanf("%d", &d);
int a = i-d; if(a>=&&a<n) que.push(a);
a = i+d;
if(a>=&&a<n) que.push(a);
int b = i - (n-d);
if(b>=&&b<n) que.push(b);
b = i + (n-d);
if(b>=&&b<n) que.push(b);
while(!que.empty()){
int t = que.top(); que.pop();
addedge(i, t);
}
}
int res = n;
for(int i=n-; i>=; i--){
memset(vis, , sizeof(vis));
if(dfs(i)) res = i;
else break;
}
if(res == ) {
for(int i=; i<n; i++) printf("%d ", py[i]);
puts("");
}
else puts("No Answer");
return ;
}
P1963 [NOI2009]变换序列 倒叙跑匈牙利算法的更多相关文章
- Luogu P1963 [NOI2009]变换序列(二分图匹配)
P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...
- P1963 [NOI2009]变换序列
对于\(N\)个整数\(0, 1, \cdots, N-1,\)一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中 \(T_i \in \{ 0,1,\cdots, N-1\}\)且 \( ...
- 洛谷P1963 [NOI2009]变换序列(二分图)
传送门 我可能真的只会网络流……二分图的题一点都做不来…… 首先每个位置有两种取值,所以建一个二分图,只要有完美匹配就说明有解 考虑一下每一个位置,分别让它选择两种取值,如果都不能形成完美匹配,说明无 ...
- 洛谷 [P1963] [NOI2009] 变换序列
这是一道二分图匹配的题 先%dalao博客 建图并没有什么难的,但是关键在于如何使字典序最小. 一个很显然的想法是先求出一个完美匹配,然后从x集合的第一个元素开始,如果该元素匹配的较小的一个,那么继续 ...
- Bzoj 1562: [NOI2009]变换序列 匈牙利算法,二分图匹配
题目: http://cojs.tk/cogs/problem/problem.php?pid=409 409. [NOI2009]变换序列 ★★☆ 输入文件:transform.in 输出文 ...
- 【BZOJ1562】[NOI2009] 变换序列(匈牙利算法)
点此看题面 大致题意: 给你一个长度为\(n\)的序列\(D\),让你找到一个字典序最小的\(n\)的排列\(T\),满足\(D_i=min(|T_i-i|,n-|T_i-i|)\). 建图 我想建图 ...
- noi2009变换序列
noi2009变换序列 一.题目 1843 变换序列 2009年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 ...
- [Luogu 1963] NOI2009 变换序列
[Luogu 1963] NOI2009 变换序列 先%Dalao's Blog 什么?二分图匹配?这个确定可以建图? 「没有建不成图的图论题,只有你想不出的建模方法.」 建图相当玄学,不过理解大约也 ...
- BZOJ 1562 [NOI2009] 变换序列
[NOI2009] 变换序列 [题解] 就是有一个序列,每个位置可以填两个数,不可重复,问最小字典序. 显然,可以建一个二分图,判合法就是找完美匹配. 那怎么弄最小字典序呢?有好多种解法,我这里给出了 ...
随机推荐
- oracle查询截至到当前日期月份所在年份的所有月份
SELECT to_number(TO_CHAR(add_months(trunc(sysdate, 'yy'), ROWNUM - 1), 'MM')) as month FROM DUALCONN ...
- Presto Event Listener开发
简介 同Hive Hook一样,Presto也支持自定义实现Event Listener,用于侦听Presto引擎执行查询时发生的事件,并作出相应的处理.我们可以利用该功能实现诸如自定义日志记录.调试 ...
- 有关vs2010将c++生成exe文件时出现LINK : fatal error LNK1123: 转换到 COFF 期间失败和环境变量问题
不知怎么本来编译好好的VS2010环境,忽然出现“转换到 COFF 期间失败: 文件无效或损坏”的链接错误.花了好多天,试了好多方法,最终解决了这个问题.现在罗列一下这几种解决方案:方案1:点击“项目 ...
- springBoot的过滤器,监听器,拦截器
概述 在开发中,我们经常要考虑一些问题,对敏感词进行过滤,用户是否已经登录,是否需要对他的请求进行拦截,或者领导问现在在线人数有多少人?我们如何实现这些功能哪 @WebFilter package c ...
- SpringBoot RabbitMQ 整合使用
 ### 前提 上次写了篇文章,[<SpringBoot ...
- GitHub 用户排行榜
排行榜预览网址:Github | Githack | UNPKG | Gitee Github 中国用户排名,全球仓库 Star 最多排名,通过 Github API v3 来生成页面数据,排行榜预览 ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- Apache Flink 1.9 重大特性提前解读
今天在 Apache Flink meetup ·北京站进行 Flink 1.9 重大新特性进行了讲解,两位讲师分别是 戴资力/杨克特,zhisheng 我也从看完了整个 1.9 特性解读的直播,预计 ...
- 通过自制yum源离线安装ansible
系统环境 --CentOS release 7 python版本--Python 3.5.4 背景:在企业环境中,安装ansible的服务器往往不能访问互联网,简单的下载ansible源码安装,会 ...
- 算法与数据结构基础 - 哈希表(Hash Table)
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...