题意:你的任务是在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. redis sentinel集群

    ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...

  2. 【转载】 深度学习与自然语言处理(1)_斯坦福cs224d Lecture 1

    版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址http://blog.csdn.net/longxinchen_ml/article/details/51567960  目录(?)[- ...

  3. c++虚函数&重写

    虚函数是C++中实现多态的一种方法,父类A的一个函数声明为虚函数,在子类B中覆盖定义之后,当在调用的时候使用A*a=new B(),此时调用对应的那个虚函数的名字,则会执行B中的函数.当父类中没有定义 ...

  4. 浏览器内核控制meta name="renderer" 说明文档

    https://blog.csdn.net/adc_god/article/details/51531263

  5. SpringBoot 线程池配置 实现AsyncConfigurer接口方法

      目的是:  通过实现AsyncConfigurer自定义线程池,包含异常处理  实现AsyncConfigurer接口对异常线程池更加细粒度的控制 *a) 创建线程自己的线程池  b) 对void ...

  6. fvwm:还是觉得你最好

    2008-07-12的老日志 用了gnome和xfce,还是有些厌了,摆弄了两天fvwm,发现虽然配置起来有点麻烦,但用起来还是它最贴心,而且占资源极少,系统使用过程中内存一直只用了五六十兆.我的鼠标 ...

  7. 【leetcode 简单】 第一百五十题 两个列表的最小索引总和

    假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...

  8. python调用百度语音识别接口实时识别

    1.本文直接上干货 奉献代码:https://github.com/wuzaipei/audio_discern/tree/master/%E8%AF%AD%E9%9F%B3%E8%AF%86%E5% ...

  9. python初步学习-Python模块之 re

    re 正则表达式 python正则表达式在线检验网站 python re正则表达式语法 匹配字符 语法 解释 表达式 匹配实例 . 匹配任意除"\n"以外的任何字符 a.c abc ...

  10. css 实现圆形头像

    1.方法一 直接设置img为圆形,这种情况下如果图片不是正方形,图片会被拉伸 <img class="circleImg" src="../img/photo/im ...