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 ...
随机推荐
- KVO与KVC的使用(转)
转自http://www.cnblogs.com/mybkn/archive/2013/05/29/3104166.html KVO: 在看KVO之前,有必要先了解下KVC,即,Key-Value C ...
- HDU2665Kth number (主席树+离散)
Give you a sequence and ask you the kth big number of a inteval. InputThe first line is the number o ...
- curl获取图片
<?php set_time_limit(0); //执行30秒超时后继续执行 header("Content-type:text/html;charset=utf-8"); ...
- 基于JQ的多选/全选/反选及获取选中的值
<!-- author:青芒 --> <!DOCTYPE html> <html lang="en"> <head> <met ...
- Cloudera API访问
多租户管理页面(admin)操作 cloudera 管理页面页面操作多租户是这样的: 进入到YARN的服务页面,点击Resource Pool,你将会看到已经存在的资源池,然后再点击资源池表格右上角的 ...
- 蓝桥杯 算法训练 ALGO-145 4-1打印下述图形
算法训练 4-1打印下述图形 时间限制:1.0s 内存限制:256.0MB 问题描述 使用循环结构打印下述图形,打印行数n由用户输入.打印空格时使用"%s"格式,向pri ...
- 机器学习:集成学习(集成学习思想、scikit-learn 中的集成分类器)
一.集成学习的思想 集成学习的思路:一个问题(如分类问题),让多种算法参与预测(如下图中的算法都可以解决分类问题),在多个预测结果中,选择出现最多的预测类别做为该样本的最终预测类别: 生活中的集成思维 ...
- JavaScript,Dom,jQuery
JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript 语言的规则编写相应代码,浏览器可以解释出相应的处理. 注 ...
- 侯捷STL学习(五)--allocator和容器之间的实现关系
第十一节 分配器 STL源码学习----内存管理 分配器的好坏影响到容器的性能 operator new()里面调用malloc D:\Program Files (x86)\Microsoft Vi ...
- C# Lambda快速深度拷贝
背景:今天上班在班车上和一个同事讨论有关C#拷贝效率的问题,聊到了多种深度拷贝方法,其中就提到了一种Lambda表达式拷贝的方法,这位同事说这种深度拷贝快是快但是如果对象里面再嵌入对象就不能深度拷贝了 ...