题意:你的任务是在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. bzoj千题计划159:bzoj2055: 80人环游世界(有源汇上下界可行最小费用流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2055 某个国家必须经过vi次, 可以转化为上下界都为vi的边 对这张图做有源汇上下界可行最小费用流 ...

  2. Intellij IDEA 2017 控制台打印换行

    Intellij IDEA 2017 控制台打印的内容超过屏幕宽度了,请问怎么自动换行? 记得重启idea

  3. JavaScript事件代理入门

    事件代理(Event Delegation),又称之为事件委托.是 JavaScript 中常用绑定事件的常用技巧. 顾名思义,“事件代理”即是把原本需要绑定的事件委托给父元素,让父元素担当事件监听的 ...

  4. 你需要了解 Windows Phone 8.1 的11件事

    微软已经发布了其新一代手机操作系统 Windows Phone 8.1,拥有一些新的特性.从本质上来说,微软此次的大修让 Windows Phone 更接近 Android 和 iOS,对于使用体验的 ...

  5. BZOJ第一页刷题计划

    BZOJ第一页刷题计划 已完成:67 / 90 [BZOJ1000]A+B Problem:A+B: [BZOJ1001][BeiJing2006]狼抓兔子:最小割: [BZOJ1002][FJOI2 ...

  6. fifo 上使用 select -- 转

    http://www.outflux.net/blog/archives/2008/03/09/using-select-on-a-fifo/ The right way to handle on-g ...

  7. [转]Git忽略提交规则 - .gitignore配置运维总结

    在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...

  8. Hibernate5笔记6--Hibernate检索优化

    Hibernate检索优化: 检索即查询.为了减轻DB的访问压力,提高检索效率,Hibernate对检索进行了优化. 所谓检索优化,指的是对查询语句的执行时机进行了细致.严格的把控:并不是代码中一出现 ...

  9. Once you eliminate all the other factors,the only thing remaining must be the truth.

    Once you eliminate all the other factors,the only thing remaining must be the truth. 一旦你排除了杂因,剩下的一定是 ...

  10. 新手向-同步关键字synchronized对this、class、object、方法的区别

    synchronized的语义 实验 分析 在看源代码时遇到多线程需要同步的时候,总是会看见几种写法,修饰方法.修饰静态方法.synchronized(Xxx.class).synchronized( ...