UVA11134_Fabled Rooks
大概题意:
在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的更多相关文章
- 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 ...
- (light OJ 1005) Rooks dp
http://www.lightoj.com/volume_showproblem.php?problem=1005 PDF (English) Statistics Forum Tim ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- 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 ...
- 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 ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- 贪心 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 ...
- uva 11134 - Fabled Rooks(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- 1005 - Rooks(规律)
1005 - Rooks PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...
随机推荐
- <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalabl ...
- MSF——基本使用和Exploit模块(一)
MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) MSF——Metasploit Fra ...
- matplotlib系列——中文显示
幕布视图:https://mubu.com/doc/alG8r_3iSw 参考文献:嵩天的Python课程讲义 方式一: 示例 rcParams的属性 方式二:(推荐使用) 示例: 中文字体种类
- Ubuntu 16.04 修改状态栏位置
修改状态栏位置: gsettings set com.canonical.Unity.Launcher launcher-position Bottom #底部(苹果风格) gsettings set ...
- 页面加载时loading效果
页面加载时loading效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- 在linux中出现there are stopped jobs 的解决方法【转自:http://www.linuxdiyf.com/viewarticle.php?id=104604】
在用管理员执行一个命令后,我用Ctrl+Z把命令转移到了后台天.导致我无法退出root的. 输入命令:logout终端显示:There are stopped jobs. 解决方法:输入命令:jobs ...
- 24.循环栅栏 CyclicBarrier
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * ...
- Cesium经纬度
computed: { handler() { return new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas) } } ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- SQL IN 运算符
SQL IN 运算符 IN运算符允许您在WHERE子句中指定多个值. IN运算符是多个OR条件的简写. SQL IN 语法 SELECT column_name(s) FROM table_name ...