8-4 Fabled Rooks uva11134
题意:你的任务是在n*n的棋盘上放 n 小于5000 个车 使得任意两个车不互相攻击 且第i个车在一个给定的矩形ri之内 给出该矩形左上角坐标和右下角坐标四个点 必须满足放车的位置在矩形内 边上也行 如果无解输出IMPSSIBLE
行与列是独立的 所以可以分割成两个一模一样的子问题 贪心
要以右边界升序排序 我一开始按照左边界排序错了 举个例子 1-1 1-3 2-2 这样的话就会错 1-1 2-2 1-3才对
还有就是注意细节 sort 从一开始的话 都要加一,,,
#include<bits/stdc++.h>
using namespace std;
#define N 5010
int n,k;
int vis[N];
struct node
{
int id;
int x,y;
int x1,x2,y1,y2; }chess[N]; bool cmp1(node a,node b)
{
return a.x2<b.x2||(a.x2==b.x2&&a.x1<b.x1);
}
bool cmp2(node a,node b)
{
return a.y2<b.y2||(a.y2==b.y2&&a.y1<b.y1);
}
bool cmp3(node a,node b)
{
return a.id<b.id;
}
int main()
{
while(cin>>n,n)
{
int flag=;
for(int i=;i<n;i++)
{
chess[i].id=i;
scanf("%d%d%d%d",&chess[i].x1,&chess[i].y1,&chess[i].x2,&chess[i].y2);
}
sort(chess,chess+n,cmp1);
memset(vis,,sizeof vis); for(int i=;i<n;i++)
{
int ok=;
for(int j=chess[i].x1;j<=chess[i].x2;j++)
{
if(!vis[j]){ok=;vis[j]=;chess[i].x=j;break; }//把chess里面的i写成了j 强行将自己dubug了半个小时。。。
}
if(!ok){flag=;}
}
sort(chess,chess+n,cmp2);
memset(vis,,sizeof vis);
for(int i=;i<n;i++)
{
int ok=;
for(int j=chess[i].y1;j<=chess[i].y2;j++)
{
if(!vis[j]){ok=;vis[j]=;chess[i].y=j;break; }
}
if(!ok){flag=;}
}
sort(chess,chess+n,cmp3);
if(flag)
for(int i=;i<n;i++)
printf("%d %d\n",chess[i].x,chess[i].y);
else printf("IMPOSSIBLE\n");
}
}
LRJ的代码 更慢
#include<cstdio>
#include<cstring>
#include <algorithm>
using namespace std; // solve 1-D problem: find c so that a[i] <= c[i] <= b[i] (0 <= i < n)
bool solve(int *a, int *b, int *c, int n) {
fill(c, c+n, -);
for(int col = ; col <= n; col++) {
// find a rook with smalleset b that is not yet assigned
int rook = -, minb = n+;
for(int i = ; i < n; i++)
if(c[i] < && b[i] < minb && col >= a[i]) { rook = i; minb = b[i]; }
if(rook < || col > minb) return false;
c[rook] = col;
}
return true;
} const int maxn = + ;
int n, x1[maxn], y1[maxn], x2[maxn], y2[maxn], x[maxn], y[maxn]; int main() {
while(scanf("%d", &n) == && n) {
for (int i = ; i < n; i++)
scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
if(solve(x1, x2, x, n) && solve(y1, y2, y, n))
for (int i = ; i < n; i++) printf("%d %d\n", x[i], y[i]);
else
printf("IMPOSSIBLE\n");
}
return ;
}
8-4 Fabled Rooks uva11134的更多相关文章
- uva11134 - Fabled Rooks(问题分解,贪心法)
这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...
- UVA - 11134 Fabled Rooks[贪心 问题分解]
UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to t ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- L - Fabled Rooks(中途相遇法和贪心)
Problem F: Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the ...
- 贪心 uvaoj 11134 Fabled Rooks
Problem F: Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the ...
- uva 11134 - Fabled Rooks(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- UVA-11134 Fabled Rooks 贪心问题(区间贪心)
题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...
- Uva11134 Fabled Rooks
普通的贪心题. 虽然图是二维的,但可以把横向和纵向分开处理. 将区间按右端点排序,然后从区间左端点到右端点找第一个空位置放棋子即可. /*by SilverN*/ #include<algori ...
- uva 11134 fabled rooks (贪心)——yhx
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...
随机推荐
- 内存操作函数memmove,memcpy,memset
通过字符串的学习,我们知道字符串操作函数的操作对象是字符串,并且它的结束标志是结束符\0,当然这个说的是不 受限制的字符串函数.然而当我们想要将一段内存的数据复制到另一块内存时,我们不能使用字符串操作 ...
- srpingboot2 session过期时间设置
springboot2 设置session过期的配置 server.servlet.session.timeout = 1800 而不再是 server.session.timeout=1800
- C#: Delegate and Event
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SpringBoot 线程池配置 实现AsyncConfigurer接口方法
目的是: 通过实现AsyncConfigurer自定义线程池,包含异常处理 实现AsyncConfigurer接口对异常线程池更加细粒度的控制 *a) 创建线程自己的线程池 b) 对void ...
- Django-Form表单(验证、定制、错误信息、Select)
Django form 流程 1.创建类,继承form.Form 2.页面根据类的对象自动创建html标签 3.提交,request.POST 封装到类的对象里,obj=UserInf ...
- C# XML序列化和反序列化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- word文档下划线无法显示的解决方法
在编辑文档的时候经常会遇到下划线无法显示的情况,如图: 如果遇到不能在姓名后面加下划线的情况,我们该怎么做? 请看下面的图解: 1.首先点击左上角的office图标 2.点击右下角“word选项” 3 ...
- mysql高可用架构 -> MHA简介-01
作者简介 松信嘉範:MySQL/Linux专家2001年索尼公司入职2001年开始使用oracle2004年开始使用MySQL2006年9月-2010年8月MySQL从事顾问2010年-2012年 D ...
- if(a==1) & if(1==a) 区别
[前提] 在公司参加项目时,看到前辈写if比较数值是否相等,经常会写 if(1==a) ,觉得有些奇怪,如是乎,将调查结果写下来记录一下. [结果] if(a==1) 与 if(1==a)是没有什么区 ...
- Linux 获取网关地址
route命令的用法:操作或者显示IP路由表route:DESCRIPTION Route manipulates the kernel's IP routing tables. Its primar ...