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] 变换序列 [题解] 就是有一个序列,每个位置可以填两个数,不可重复,问最小字典序. 显然,可以建一个二分图,判合法就是找完美匹配. 那怎么弄最小字典序呢?有好多种解法,我这里给出了 ...
随机推荐
- 异步请求xhr、ajax、axios与fetch的区别比较
目录 1. XMLHttpRequest对象 2. jQuery ajax 3. axios 4. fetch 参考 why: 为什么会出现不同的方法呢? what: 这些都是异步请求数据的方法.在不 ...
- iOS开发 8小时时差问题
今天调试遇到时间计算的问题,发现怎么算都会有差别,后来仔细观察,发现有8小时的时差…… 这篇文章解释的很好,用到了,因此记之. ios有关时间打印出来差8小时的问题
- HttpClientFactory 使用说明 及 对 HttpClient 的回顾和对比
目录 HttpClient 日常使用及坑点: HttpClientFactory 优势: HttpClientFactory 使用方法: 实战用法1:常规用法 1. 在 Startup.cs 中进行注 ...
- 手动编译PHP开发环境
目录 手动编译PHP开发环境 问题复盘 部署环境及配置 目标环境 安装部署环境开始 首先安装PHP 安装mysql 安装nginx 手动编译PHP开发环境 这是一篇来自深夜加班的手稿 问题复盘 你有没 ...
- 自定义ItemToggleView
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- 理解MySQL(一)--MySQL介绍
一.Mysql逻辑架构: 1. 第一层:服务器层的服务,连接\线程处理. 2. 第二层:查询执行引擎,MySQL的核心服务功能,包括查询解析.分析.优化和缓存,所有跨存储引擎的功能都在这一层实现. 3 ...
- 用 bat 文件实现 excel 周报复制
又要写周报???? 写周报就算了每次都要改这一大堆的日期,什么鬼嘛,最骚的我还总是有的忘记改.... 作为一个正儿八经的程序员,固定每周某天干重复的一件事,哦~~ 这是机器人 程序应 ...
- 变量Variable
变量Variable 内存 #conding:utf-8 a = 1 #conding:utf-8 a = 1 b = a #conding:utf-8 a = 1 b = a a = 2 命名规则 ...
- 「求助」关于MacOS 适配不了SOIL的问题 以及我自己愚蠢的解决办法
我的环境 macOS High Sierra 10.13.6 (2018) 我的SOIL源是通过 终端 git clone https://github.com/DeVaukz/SOIL 直接从gay ...
- imageloader+图片压缩
public class MainActivity extends AppCompatActivity { private ImageView ivIcon; @Override protected ...