大概题意:

在n*n的棋盘上面放n个车,能否使他们互相不攻击(即不能在同一行一列),并且第i个车必须落在第i的矩形范围(xl,yl, xr,yr)之内

xy互相并不干扰,所以就可以把这个二维问题压缩成一维的,即在x轴能否让第i个点落在第i个区间内,如果x或y轴不存在这样放法,那么二维肯定也不可以!!!

当我们转换成一维问题之后,便是一个很经典的区间贪心模型了

贪心策略就是,区间右界越小优先级越高,相同条件下左界越大优先级越高

把这些区间排序之后,如何选取才能保证最优???

其实这一类大题,贪心策略就是尽量自己用到自己特有的别人可能用不到的

也就是从右界往左不断取,这样的话,优先级比他低的(优先级低表示要么左界比他小,要么右界比他大,这两种情况都能保证有解),若能取则一定可以取到

这题就是告诉我们,有时候,我们所解决的问题,看似密不可分,其实完全可以向那个高数中求两个变量的积分一样,把他们拆分成完全不干扰的两个问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1e5+100;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
typedef long long ll;
bool vis[maxn];
int n;
struct node
{
int l,r,num;
node():l(0),r(0),num(0){}
bool operator < (const node& rhs)const
{
return r < rhs.r || (r == rhs.r && l < rhs.l);
}
}x[maxn],y[maxn];
bool slove(int* a,node* q)
{
memset(a, 0, sizeof a);
memset(vis, 0, sizeof vis);
for(int i = 0; i < n; ++i)
{
for(int j = q[i].l; j <= q[i].r; ++j)
{
if(!vis[j])
{
a[q[i].num] = j;
vis[j] = 1;
break;
}
if(j == q[i].r)
{
// pr(q[i].num);pr(q[i].l);prln(q[i].r);
return false;
}
}
}
return true;
}
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
int a[maxn],b[maxn];
while(cin >> n && n)
{
for(int i = 0; i < n; ++i)
{
scanf("%d%d%d%d",&x[i].l,&y[i].l,&x[i].r,&y[i].r);
x[i].num = y[i].num = i;
}
sort(x, x + n);sort(y, y + n);
if(slove(a,x)&&slove(b,y))
{
for(int i = 0; i < n; ++i)
printf("%d %d\n",a[i], b[i]);
}
else
{
printf("IMPOSSIBLE\n");
}
}
return 0;
}

UVA11134_Fabled Rooks的更多相关文章

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

  2. (light OJ 1005) Rooks dp

    http://www.lightoj.com/volume_showproblem.php?problem=1005        PDF (English) Statistics Forum Tim ...

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

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

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

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

  6. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

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

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

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

  9. 1005 - Rooks(规律)

    1005 - Rooks   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...

随机推荐

  1. Reciting(second)

      It is subtly revealed in the caricature that a son is expressing his concern about disposing of nu ...

  2. latex 查找缺失的库文件

    app-portage/pfl contains a program to search in an online database for a Gentoo package containing a ...

  3. Python中的内置函数和匿名函数

    1. 内置函数 print用法 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print ...

  4. 转 cpu高 问题分析定位

    文章来源: http://www.blogjava.net/hankchen/archive/2012/08/09/377735.html 一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原 ...

  5. DNS 放大

    DNS放大攻击是伪造一个DNS查询的报文,源地址改成想要攻击的IP.单个查询的包64字节,如果是ANY类型查询(或者DNSSEC记录),那么回复报文一般会大几十倍.当然,如果攻击者自己制造一个很大的T ...

  6. P1001 A+B Problem

    下面我们要展示这个题的玄学做法!!!@cjx!!! #include<iostream> #define chen using namespace std; #define jia int ...

  7. 动画FPS计算

    1.chrome的debug 2.PerformanceObserver var observer = new PerformanceObserver(function (list) { var pe ...

  8. Codeforces 1179D 树形DP 斜率优化

    题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...

  9. SQL笔试题:下面是学生表(student)的结构说明

    SQL笔试题:下面是学生表(student)的结构说明 SQL笔试题:下面是学生表(student)的结构说明 字段名称 字段解释 字段类型 字段长度 约束 s_id 学号 字符 10 PK s_na ...

  10. WaitForSingleObject的作用[转]

    在多线程的情况下,有时候我们会希望等待某一线程完成了再继续做其他事情(比如主线程等待子线程结束完之后,自己再结束),要实现这个目的,可以使用Windows API函数WaitForSingleObje ...