传送门

根据公式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]变换序列(二分图最大匹配)的更多相关文章

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

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

  2. 【BZOJ1562】【jzyzOJ1730】【COGS409】NOI2009变换序列 二分图匹配

    [问题描述]        对于N个整数0, 1, ……, N-1,一个变换序列T可以将i变成Ti,其中 定义x和y之间的距离.给定每个i和Ti之间的距离D(i,Ti), 你需要求出一个满足要求的变换 ...

  3. 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≤ ...

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

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

  5. noi2009变换序列

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

  6. BZOJ 1562 [NOI2009] 变换序列

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

  7. [Luogu 1963] NOI2009 变换序列

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

  8. 【bzoj1562】 NOI2009—变换序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=1562 (题目链接) 题意 给出一个序列(0~n-1),这个序列经过某个变换会成为另外一个序列,但是其 ...

  9. bzoj1562[NOI2009]变换序列——2016——3——12

    任意门:http://www.lydsy.com/JudgeOnline/problem.php?id=1562 题目: 对于0,1,…,N-1的N个整数,给定一个距离序列D0,D1,…,DN-1,定 ...

随机推荐

  1. Predicate和Consumer接口的使用

    //  Predicate   判断是否拥有资格,Consumer  改变输入的值 案例 public static MyTest2 getV(MyTest2 a, Predicate<MyTe ...

  2. 1003: Redraiment的遭遇

    1003: Redraiment的遭遇 Time Limit: 1000 Sec  Memory Limit: 128 MBSubmit: 456  Solved: 158[Submit][Statu ...

  3. python_80_模块定义导入优化实例

            运行结果 __import__作用: 同import语句同样的功能,但__import__是一个函数,并且只接收字符串作为参数,所以它的作用就可想而知了.其实import语句就是调用这 ...

  4. Spring boot 集成 Dubbo 快速搭建

    架构: 1.ZooKeeper:服务注册中心 2.api工程:提供对外暴露的服务API 3.provider:服务提供者 4.consumer:服务消费者 示例如下: (一)新建 Maven 项目 a ...

  5. springmvc的第一个程序

    文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: <?xml version="1.0" encoding=" ...

  6. 音频框架TheAmazingAudioEngine实现音效

    TheAmazingAudioEngine是Michael Tyson开源的iOS第三方音频框架.很多音频类APP应用这个框架作开发. 应用这个框架,可以比较方便地实现iOS音频开发中的各种音效的实现 ...

  7. mysql 的 case when 用法

    正确的格式: case when condition then result when condition then result when condition then result else re ...

  8. tomcat如何登录Server Status、Manager App、Host Manager

    启动tomcat后,访问127.0.0.1会进入如下页面 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址:https://www.cnblogs.com/poterliu/p/9602 ...

  9. 【js】window.onscroll 无效问题

    body 设置为height:100% 导致window.onscroll 无效

  10. python-for循环与while循环

    while 循环 格式: while 条件 为 True: 代码块 while True: rayn_age = 18 age = input('请输入你的年龄:') age = int(age) i ...