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 ...
随机推荐
- (转)关于Linux核心转储文件 core dump
所谓核心转储文件是内含进程终止时内存映像的一个文件.产生条件:特定的信号会引发进程创建一个核心转储文件并终止运行. 包括哪些特定信号,请参见http://man7.org/linux/man-page ...
- Java使用指南(1)—— Java下载和安装
Java下载 1.在Oracle的官网中找到相应的
- jquery 获取所有父元素
最终结果: 代码: <!DOCTYPE html> <html> <head> <style> b, span, p, html body { padd ...
- MySQL5 LOAD DATA 的使用
MySQL5 LOAD DATA 的使用 数据库中,最常见的写入数据方式是通过SQL INSERT来写入,另外就是通过备份文件恢复数据库,这种备份文件在MySQL中是SQL脚本,实际上执行的还是在 ...
- 牛客网 PAT乙级(Basic Level)练习题 1023 考新郎
题目描述 过年期间,老家举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做“考新郎”,具体的操作是这样的: 1. 首先,给每位新娘打扮得几乎一模一样,并盖上大大 ...
- shell while的用法
1. #!/bin/shint=1while (( "$int < 10" ))doecho "$int"let "int++"don ...
- 深入理解Spring IOC
转载自 http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概 ...
- GMT时间
GMT:格林尼标准时间 北京时间=GMT时间+8小时
- 一:HTML文本编译器 kindeditor-4.1.10 的使用 SpringMVC+jsp的实现
一:我用的kindeditor版本是4.1.10 下载完成打开目录结构如下: 二:下面是工程目录也很重要, 三: 好了,准备工作已经做好了,现在就直接上代码了. 首先是页面JSP代码 ...
- MySQL 5.6 date 与 string 的转换和比较
我们有张表,表中有一个字段 dpt_date ,SQL 类型为 date,表示离开日期. 我们将 dpt_date 与字符串 ‘2016-03-09’ 进行比较,发现效率低于 dpt_date 转换为 ...