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 ...
随机推荐
- nyoj-115-城市平乱(dijkstra算法)
题目链接 /* Name:nyoj-115-城市平乱 Copyright: Author: Date: 2018/4/25 17:28:06 Description: dijkstra模板题 枚举从 ...
- CodeForces - 891C: Envy(可撤销的并查集&最小生成树)
For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that con ...
- Linux命令学习(18):route命令
版权声明更新:2017-05-20博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的route命令. ...
- python 图形化(Tkinter)
python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.T ...
- (转载)Windows: "net use" command introduction
1)建立空连接: net use ""IP"ipc$ "" /user:"" (一定要注意:这一行命令中包含了3个空格) 2)建立 ...
- Python 设计一个简单的计算器
设计目标 实现加减乘除及拓号优先级解析 用户输入'1 - 2 * ( (6-3 +(-5/5)*(9-2*3/3 + 7/3*7/4*12 +10 * 5/5 )) - (-4*3)/ (12-3*2 ...
- JavaScript函数的默认参数(default parameter)
JavaScript函数的默认参数(default parameter) js函数参数的默认值都是undefined, ES5里,不支持直接在形参里写默认值.所以,要设置默认值,就要检测参数是否为un ...
- jenkins插件
构建maven项目:Maven Release Plug-in Plug-in
- fastjson --JSONObject 和JSONArray 转换
fastjson解析:resultValue=[ { "total": 1, "saleLists": [ ...
- 【Linux网络编程】基于TCP流 I/O多路转接(poll) 的高性能http服务器
服务器比较简陋,为了学习poll的使用,只向客户端回写一条html语句.启动服务器后,浏览器发起请求,服务端向浏览器写回html,响应字符串,然后可以看到,浏览器解析并显示 Hello Poll!. ...