大概题意:

在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. [题解]Magic Line-计算几何(2019牛客多校第三场H题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...

  2. Lock的使用

    Lock是一个Java类,synchronized是一个Java关键字,两者有本质的不同 Lock需要手动释放锁,synchronized是自动释放锁 Lock适合大量同步的代码同步,synchron ...

  3. js获取下拉框的value值

    var Resultstr=""; var param = { action: "MoneyList" };//参数拼接 $.ajax({ type: &quo ...

  4. sourcetree配置gitlab

    一.准备 1.安装git,下载地址:https://git-scm.com/download 安装教程百度一下      git客户端(1.产生gitlab服务端和本地git相互传输时所需要校验的私钥 ...

  5. HDU 1709 The Balance( DP )

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. Codeforces 500B New Year Permutation( Floyd + 贪心 )

    B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. HDU 5125 magic balls(线段树+DP)

    magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. Pandas之loc\iloc\ix

    ---------------------------------------------------------------------------------------------------- ...

  9. ThreadLocal简单使用示例

    ThreadLocal为每个线程提供单独的数据副本,线程间的数据为自身线程所独有(不存在共享变量问题),直接看代码 public class ThreadLocalTest { private sta ...

  10. 转 Tomcat访问日志详细配置

    配置http访问日志.Tomcat自带的能够记录的http访问日志已经很详细了取消下面这段的注释: <Valve className="org.apache.catalina.valv ...