题意

构造一个字典序最小的序列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]变换序列 倒叙跑匈牙利算法的更多相关文章

  1. Luogu P1963 [NOI2009]变换序列(二分图匹配)

    P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...

  2. P1963 [NOI2009]变换序列

    对于\(N\)个整数\(0, 1, \cdots, N-1,\)一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中 \(T_i \in \{ 0,1,\cdots, N-1\}\)且 \( ...

  3. 洛谷P1963 [NOI2009]变换序列(二分图)

    传送门 我可能真的只会网络流……二分图的题一点都做不来…… 首先每个位置有两种取值,所以建一个二分图,只要有完美匹配就说明有解 考虑一下每一个位置,分别让它选择两种取值,如果都不能形成完美匹配,说明无 ...

  4. 洛谷 [P1963] [NOI2009] 变换序列

    这是一道二分图匹配的题 先%dalao博客 建图并没有什么难的,但是关键在于如何使字典序最小. 一个很显然的想法是先求出一个完美匹配,然后从x集合的第一个元素开始,如果该元素匹配的较小的一个,那么继续 ...

  5. Bzoj 1562: [NOI2009]变换序列 匈牙利算法,二分图匹配

    题目: http://cojs.tk/cogs/problem/problem.php?pid=409 409. [NOI2009]变换序列 ★★☆   输入文件:transform.in   输出文 ...

  6. 【BZOJ1562】[NOI2009] 变换序列(匈牙利算法)

    点此看题面 大致题意: 给你一个长度为\(n\)的序列\(D\),让你找到一个字典序最小的\(n\)的排列\(T\),满足\(D_i=min(|T_i-i|,n-|T_i-i|)\). 建图 我想建图 ...

  7. noi2009变换序列

    noi2009变换序列 一.题目 1843 变换序列 2009年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述  ...

  8. [Luogu 1963] NOI2009 变换序列

    [Luogu 1963] NOI2009 变换序列 先%Dalao's Blog 什么?二分图匹配?这个确定可以建图? 「没有建不成图的图论题,只有你想不出的建模方法.」 建图相当玄学,不过理解大约也 ...

  9. BZOJ 1562 [NOI2009] 变换序列

    [NOI2009] 变换序列 [题解] 就是有一个序列,每个位置可以填两个数,不可重复,问最小字典序. 显然,可以建一个二分图,判合法就是找完美匹配. 那怎么弄最小字典序呢?有好多种解法,我这里给出了 ...

随机推荐

  1. springBoot的过滤器,监听器,拦截器

    概述 在开发中,我们经常要考虑一些问题,对敏感词进行过滤,用户是否已经登录,是否需要对他的请求进行拦截,或者领导问现在在线人数有多少人?我们如何实现这些功能哪 @WebFilter package c ...

  2. SpringBoot底层原理及分析

    一,Spring Boot简介 1.什么是Spring Boot: SpringBoot是由Pivotal团队提供的框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. 该框架使用了特 ...

  3. Could not link: /usr/local/etc/bash_completion.d/brew

    用终端 brew update 或 brew install ** 时遇到的问题,详细如下: Error: Could not link: /usr/local/etc/bash_completion ...

  4. 从“n!末尾有多少个0”谈起

    在学习循环控制结构的时候,我们经常会看到这样一道例题或习题.问n!末尾有多少个0?POJ 1401就是这样的一道题. [例1]Factorial (POJ 1401). Description The ...

  5. python利用select实现的Socket Server

    # 利用python的select模块实现简单的Socket Sever #实现多用户访问,再次基础上可以实现FTP Server应用程序 # 发布目的,在于解决了客户端强行终止时,服务器端也跟着程序 ...

  6. RGB颜色 三者都是0为黑色而255是白色 解释

    问题: RGB颜色 都是0为黑色而255是白色 与日常生活的黑色白色差距怎么那么大,(与物理学中的黑色吸收光是否相悖)而且为什么要这样定义呢? 链接:https://www.zhihu.com/que ...

  7. LongAdder和AtomicLong性能对比

    jdk1.8中新原子操作封装类LongAdder和jdk1.5的AtomicLong和synchronized的性能对比,直接上代码: package com.itbac.cas; import ja ...

  8. 从JavaScript到Python之异常

    不少前端工程师看到这个标题可能会产生质问: 我js用得好好的,能后端能APP,为什么还要学习Python? 至少有下面两个理由: 学习曲线.ES6之后的JavaScript(TypeScript)的在 ...

  9. 插入Oracle数据库后返回当前主键id

    最近做一个spring版本3.0.4的老项目功能,应用场景要用到插入oracle表后返回主键ID拿来和其他表关联. 用oralce的可以一直用这种处理方式,高兼容低,搜索网上的资料都不能和这个Spri ...

  10. css实现左边高度自适应右边高度

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...