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 ...
随机推荐
- python list的extend和append方法
append: Appends object at the end. x = [1, 2, 3] x.append([4, 5]) print (x) gives you: [1, 2, 3, [4, ...
- 2017-2018-1 20179215《Linux内核原理与分析》第十周作业
第17章 设备与模块 一.设备类型 除了以上3种典型的设备之外,其实Linux中还有一些其他的设备类型,其中见的较多的应该算是"伪设备".所谓"伪设备",其实 ...
- Jenkins之构建触发器配置(转载)
构建触发器配置,当你在文本框中输入配置的时间后,文本框下方会有时间解释,这样可以很好的看到自己配置的时间对不对. 可以清晰看到我的配置第一个运行时间是周五上午10点执行,第二次是星期六上午10点. ...
- [Luogu3852][TJOI2007]小朋友
luogu 题意 求弦图的最大独立集. sol 按照完美消除序列一个个贪心选即可. code #include<cstdio> #include<algorithm> #inc ...
- javascript之面试题精讲
from:http://blog.csdn.net/q121516340/article/details/51332454 1,检测数组的几种方式: Array.isArray(); es5 toSt ...
- 微信小程序switch组件尺寸控制
1.修改switch组件的属性值 /* switch */ .wx-switch-input{ width: 82rpx!important; height: 40rpx!important; } / ...
- BZOJ4605:崂山白花蛇草水
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- 关于打包后提示无法连接到mongodb的情况
昨天晚上要和前端联调. 打完jar包后发现无法连接到测试环境的数据库. 就很尴尬,最后发现问题在于mongodb的URI写错了: 正确的URI格式:mongodb://url:port/dbName ...
- keepalived 检测脑裂切换脚本
#!/bin/bash count=0 run1=`curl -I 192.168.30.12:8000 | grep "200 OK" | wc -l` run2=`curl - ...
- jenkins jacoco
1.pom中加jacoco依赖 <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoc ...