spoj2142 Arranging Flowers
题目大意
给你n和m,表示一个n*n的数独已经填完了m行,让你填出剩下几行,要求答案的字典序最小。
分析
看到这道题我首先想到的是记录每行每列使用了哪些数字,然后贪心的来填,但是发现用这种策略会在有些情况下得不到解。于是我们考虑二分图匹配,将左边n个点表示每一行n个位置,右边n个点表示对应的n个数,我们一行一行的填,每填一行,便将产生的对应关系所代表的边删掉。那我们考虑如何二分图匹配可以得到字典序最小的答案,我们先进行一边常规的二分图匹配,然后枚举1到n每一个位置,找所有现在比位置j对应的值小且位置在j之后的k替换现在j对应的值,检测是否可以替换。注意这里的used数组只需每个j清空一次,因为j是一个固定的值,所以之前不行的值,随着k的改变仍然不行。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define li long long
#define pb push_back
#define mp make_pair
#define y1 y12345678909
#define rii register int
#define pii pair<int,int>
#define r(x) scanf("%d",&x)
#define ck(x) cout<<x<<endl;
#define uli unsigned long long
#define clr(x) memset(x,0,sizeof(x))
#define sp cout<<"------------------------------------------------------"<<endl
int g[],nxt[][],pre[][],belong[],T,used[],n,m,back[];
inline void del(int x,int y){
if(g[x]==y)g[x]=nxt[x][y];
else {
nxt[x][pre[x][y]]=nxt[x][y];
pre[x][nxt[x][y]]=pre[x][y];
}
return;
}
inline bool work(int x,int lit){
for(rii i=g[x];i;i=nxt[x][i])
if(used[i]!=T){
used[i]=T;
if(!belong[i]||(belong[i]>lit&&work(belong[i],lit))){
belong[i]=x;
back[x]=i;
return ;
}
}
return ;
}
inline void go(){
for(rii i=;i<=n;++i){
T++;
work(i,);
}
for(rii j=;j<=n;++j){
T++;
for(rii k=g[j];k;k=nxt[j][k]){
if(belong[k]==j)break;
if(belong[k]<j)continue;
int x=belong[k],y=back[j];
belong[back[j]]=;
back[belong[k]]=;
belong[k]=j;
back[j]=k;
if(work(x,j))break;
back[x]=k;
belong[k]=x;
belong[y]=j;
back[j]=y;
}
}
return;
}
int main(){
int i,j,k,t,x;
r(t);
while(t--){
r(n),r(m);
clr(pre),clr(nxt);
for(rii i=;i<=n;++i){
g[i]=;
int now=;
for(rii j=;j<=n;++j){
nxt[i][now]=j;
pre[i][j]=now;
now=j;
}
}
for(rii i=;i<=m;++i)
for(rii j=;j<=n;++j){
r(x);
del(j,x);
}
ck(n-m);
for(rii i=m+;i<=n;++i){
T=;
clr(back);
clr(belong);
clr(used);
go();
for(rii j=;j<=n;++j)
del(j,back[j]);
for(rii j=;j<=n;++j)printf("%d ",back[j]);
puts("");
}
}
return ;
}
spoj2142 Arranging Flowers的更多相关文章
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)
B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...
- poj1157LITTLE SHOP OF FLOWERS
Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...
- CF459B Pashmak and Flowers (水
Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...
- 线段树或树状数组---Flowers
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- 【CodeForces 621C】Wet Shark and Flowers
题 There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such tha ...
- cf340 C. Watering Flowers
C. Watering Flowers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- UVA - 11014 Make a Crystal (莫比乌斯反演)
给定一个n*n*n的立方体(中心点为原点O),选择尽量多的点,使得对于任意两点A,B,B不在线段OA上. 可以发现,原问题可转化为三维坐标下的点(x,y,z)中有多少个点的gcd(x,y,z)=1. ...
- LeetCode 361. Bomb Enemy
原题链接在这里:https://leetcode.com/problems/bomb-enemy/description/ 题目: Given a 2D grid, each cell is eith ...
- 前端项目使用module.exports文件一定要Webpack编译吗?请问gulp可以编译这种文件吗
import引入类似这种文件,一定要用webpack去编译吗 module.pxports 是CMD规范的一个全局函数,功能是当前模块对外提供接口.require可以直接使用这个接口.例子: echo ...
- hw_module_t 加载过程
每一个HAL模块都有一个ID值,以这些ID值为参数来调用硬件抽象层提供的函数hw_get_module就可以将指定的模块加载到内存来,并且获得 一个hw_module_t接口来打开相应的设备. 函数h ...
- facebook注册不了无法打开官网的解决办法
上周有一个朋友问到我一个问题,问怎么facebook注册不了,facebook官网也无法打开?这个问题不知道有没有人遇到过,以前这个问题也困扰了我挺长时间的,其实想想也挺简单的,由于facebook, ...
- Azure VM开启资源监控
目前China的Azure VM资源监控默认是不打开的.本文将介绍如何开启VM的监控功能. 一 Azure VM 打开Azure的Portal页面https://portal.azure.cn,登录后 ...
- Azure ASM到ARM迁移 (三) Reserved IP的迁移
Azure的ASM下,很多用户的应用种域名的解析在DNS服务器种都采用A记录的方式,所以很多用户都在Azure上采用了Reserved IP. 关于Reserved IP,可以参考http://www ...
- bash的使用
转自:http://blog.csdn.net/y2888886/article/details/50535033 在上篇博文的基础上做如下修改 注意一些常见命令中间就要加 “ ” ,否则很多命令无法 ...
- QT5 地图的使用
https://blog.csdn.net/qq_28877125/article/details/80561829 博客园地址 资源下载地址: https://download.csdn.net/d ...
- C# 实现快捷键几种方法
本文讲解了三种方法实现C# button快捷键,如Alt + *(按钮快捷键),Ctrl+*及其他组合键等. 一. C# button快捷键之第一种:Alt + *(按钮快捷键) 在大家给button ...