题意:你的任务是在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的更多相关文章

  1. uva11134 - Fabled Rooks(问题分解,贪心法)

    这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...

  2. 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 ...

  3. 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...

  4. 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 ...

  5. 贪心 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 ...

  6. uva 11134 - Fabled Rooks(问题转换+优先队列)

    题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...

  7. UVA-11134 Fabled Rooks 贪心问题(区间贪心)

    题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...

  8. Uva11134 Fabled Rooks

    普通的贪心题. 虽然图是二维的,但可以把横向和纵向分开处理. 将区间按右端点排序,然后从区间左端点到右端点找第一个空位置放棋子即可. /*by SilverN*/ #include<algori ...

  9. 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 ...

随机推荐

  1. Nginx跳转Tomcat

    conf配置: server {         listen       80;         server_name www.-------.com;         server_name_i ...

  2. 3.Filter和interceptor的区别

    https://blog.csdn.net/qq_36411874/article/details/53996873

  3. Linux 常见文件及目录

    文件/etc//etc/passwd用户基本信息/etc/group用户组基本信息/etc/shadow/etc/passwd 文件的补充/etc/gshadow阴影口令套组中的组配置文件/etc/l ...

  4. Grep学习笔记

    Grep(Global search Regular Expression and Print out the line)是一种强大的文本搜索工具. 1. 正则表达式的基本组成部分 正则表达式 描述 ...

  5. Druid.io通过NiFi摄取流数据

    NiFi是一个易于使用,功能强大且可靠的系统来处理和分发数据. 本文讲述如何用NiFi将Http的Json数据传到Druid.国外的一篇文章讲到如何用NiFi将推文传到Druid,https://co ...

  6. 20145202马超 2016-2017-2 《Java程序设计》第5周学习总结

    20145202马超 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 异常:程序在运行的时候出现不正正常的情况 由来:问题也是可以通过java对不正常情况进行 ...

  7. 并发编程(三) IO模型

    五 IO模型 常用的IO模型有4种: 阻塞IO 非阻塞IO IO多路复用 异步IO 不常用的有: 驱动信号 5.1 阻塞IO.非阻塞IO 阻塞IO:进程不能做其他的事情 非阻塞IO:等待数据无阻塞 阻 ...

  8. HDU 4720 Naive and Silly Muggles 平面几何

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720 解题报告:给出一个三角形的三个顶点坐标,要求用一个最小的圆将这个三个点都包含在内,另外输入一个点 ...

  9. Shiro认证的另一种方式

    今天在学习shiro的时候使用另一种shiro验证的方式. 总体的思路是: (1)先在自己的方法中进行身份的验证以及给出提示信息.(前提是将自己的验证方法设为匿名可访问) (2)当验证成功之后到Shi ...

  10. The Art of Memory Forensics-Windows取证(Virut样本取证)

    1.前言 The Art of Memory Forensics真是一本很棒的书籍,其中使用volatility对内存进行分析的描述可以辅助我们对更高级类的木马进行分析和取证,这里对书中的命令进行了笔 ...