[luoguP1963] [NOI2009]变换序列(二分图最大匹配)
根据公式xjb推一下,然后就可以连边。
考虑到字典序最小,和匈牙利算法的实现过程,要倒序匹配。
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 40001 using namespace std; int n, cnt;
int head[N], to[N], nex[N], belong[N], a[N];
bool vis[N];
vector <int> vec; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline bool check(int x, int y, int d)
{
return 0 <= x && x < n && min(abs(x - y), n - abs(x - y)) == d;
} inline void add(int x, int y)
{
to[cnt] = y;
nex[cnt] = head[x];
head[x] = cnt++;
} inline bool dfs(int u)
{
int i, v;
for(i = head[u]; ~i; i = nex[i])
{
v = to[i];
if(!vis[v])
{
vis[v] = 1;
if(!belong[v] || dfs(belong[v]))
{
belong[v] = u;
return 1;
}
}
}
return 0;
} inline bool solve()
{
int i, ans = 0;
for(i = n - 1; i >= 0; i--)
{
memset(vis, 0, sizeof(vis));
ans += dfs(i);
}
return ans == n;
} int main()
{
int i, j, x, d;
n = read();
memset(head, -1, sizeof(head));
for(i = 0; i < n; i++)
{
d = read();
vec.clear();
x = d + i;
if(check(x, i, d)) vec.push_back(x);
x = n - d + i;
if(check(x, i, d)) vec.push_back(x);
x = i - d;
if(check(x, i, d)) vec.push_back(x);
x = i - n + d;
if(check(x, i, d)) vec.push_back(x);
sort(vec.begin(), vec.end());
if(vec.size()) for(j = vec.size() - 1; j >= 0; j--) add(i, vec[j]);
}
if(!solve()) puts("No Answer");
else
{
for(i = 0; i < n; i++) a[belong[i]] = i;
for(i = 0; i < n; i++) printf("%d ", a[i]);
}
return 0;
}
[luoguP1963] [NOI2009]变换序列(二分图最大匹配)的更多相关文章
- Luogu P1963 [NOI2009]变换序列(二分图匹配)
P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...
- 【BZOJ1562】【jzyzOJ1730】【COGS409】NOI2009变换序列 二分图匹配
[问题描述] 对于N个整数0, 1, ……, N-1,一个变换序列T可以将i变成Ti,其中 定义x和y之间的距离.给定每个i和Ti之间的距离D(i,Ti), 你需要求出一个满足要求的变换 ...
- BZOJ1562: [NOI2009]变换序列(二分图 匈牙利)
Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30%的数据中N≤50:60%的数据中N≤ ...
- Bzoj 1562: [NOI2009]变换序列 匈牙利算法,二分图匹配
题目: http://cojs.tk/cogs/problem/problem.php?pid=409 409. [NOI2009]变换序列 ★★☆ 输入文件:transform.in 输出文 ...
- noi2009变换序列
noi2009变换序列 一.题目 1843 变换序列 2009年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 ...
- BZOJ 1562 [NOI2009] 变换序列
[NOI2009] 变换序列 [题解] 就是有一个序列,每个位置可以填两个数,不可重复,问最小字典序. 显然,可以建一个二分图,判合法就是找完美匹配. 那怎么弄最小字典序呢?有好多种解法,我这里给出了 ...
- [Luogu 1963] NOI2009 变换序列
[Luogu 1963] NOI2009 变换序列 先%Dalao's Blog 什么?二分图匹配?这个确定可以建图? 「没有建不成图的图论题,只有你想不出的建模方法.」 建图相当玄学,不过理解大约也 ...
- 【bzoj1562】 NOI2009—变换序列
http://www.lydsy.com/JudgeOnline/problem.php?id=1562 (题目链接) 题意 给出一个序列(0~n-1),这个序列经过某个变换会成为另外一个序列,但是其 ...
- bzoj1562[NOI2009]变换序列——2016——3——12
任意门:http://www.lydsy.com/JudgeOnline/problem.php?id=1562 题目: 对于0,1,…,N-1的N个整数,给定一个距离序列D0,D1,…,DN-1,定 ...
随机推荐
- 10.1 plan
1951 [Sdoi2010]古代猪文 Sdoi2010 Contest2 807 1928 1566 [NOI2009]管道取珠 806 1429 2756 [S ...
- ubuntu开放端口
1.安装iptables(一般情况,ubuntu安装好的时候,iptables会被安装上),使用以下命令: $apt-get update $apt-get install iptables 2.安装 ...
- SC || 解决在git中上传过大文件的问题(如何将提交过的彻底删除
就在我在ddl前续命的时候……不知道怎么想不开,把v2的压力测试的日志(500多M)也往github上传 之前听说过好多因为传了大文件的锅…… 我竟然还想不开的往上传…… 真实又傻又蠢又自闭(T T ...
- Python中文编码问题(字符串前面加'u')
中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...
- javaweb基础(12)_session详解
一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...
- java基础—哈希编码
- VMware虚拟网卡介绍和使用说明
介绍VMware三种网络连接模式的详细配置及规则 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/9455391 ...
- Python使用gevent实现协程
Python中多任务的实现可以使用进程和线程,也可以使用协程. 一.协程介绍 协程,又称微线程.英文名Coroutine.协程是Python语言中所特有的,在其他语言中没有. 协程是python中另外 ...
- 补之前 如何改变jupyter打开文件的路径
目录 如何改变jupyter打开文件的路径 第一种方法: 第二种方法 第三种方法 如何改变jupyter打开文件的路径 当我们直接打开jupyter时,直接加载的是我们的C盘文件 现在我们想打开其他盘 ...
- Applied Nonparametric Statistics-lec7
Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/9 经过前面的步骤,我们已经可以判断几个样本之间是否 ...