题目:点击打开题目链接

思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第i个整数在区间[x, y]内,此问题可以通过贪心法解决,但需要注意选择合适的贪心策略。我的贪心方法是:以后约束点为重点考察对象对点进行排序,然后遍历给每一个点选择最小的合适的下标,若找不到合适的下标,说明无解。

AC代码:

 #include <iostream>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = + ; struct point{
int st, ed, id;
}x[maxn], y[maxn]; int n, cx[maxn], cy[maxn]; bool cmp(const point& temp1, const point& temp2) {
return temp1.ed == temp2.ed ? temp1.st < temp2.st : temp1.ed < temp2.ed;
} bool judge(int *arr, point *Point) {
sort(Point, Point + n, cmp);
int temp[maxn] = {}; for(int i = ; i < n; i++) {
bool ok = false;
for(int j = Point[i].st; j <= Point[i].ed; j++) {
if(!temp[j]) {
ok = true;
temp[j] = ;
arr[Point[i].id] = j;
break;
}
}
if(!ok) return false;
}
return true;
} int main()
{
while(cin >> n && n) {
for(int i = ; i < n; i++){
cin >> x[i].st >> y[i].st >> x[i].ed >> y[i].ed;
x[i].id = i;
y[i].id = i;
}
memset(cx, , sizeof(cx));
memset(cy, , sizeof(cy));
if(judge(cx, x) && judge(cy, y)) {
for(int i = ; i < n; i++)
cout << cx[i] << ' ' << cy[i] << endl;
}
else
cout << "IMPOSSIBLE" << endl; }
return ;
}

UVA - 11134 Fabled Rooks问题分解,贪心的更多相关文章

  1. UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  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. uva 11134 - Fabled Rooks(问题转换+优先队列)

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

  4. UVA 11134 Fabled Rooks 贪心

    题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...

  5. UVa 11134 Fabled Rooks (贪心+问题分解)

    题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...

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

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

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

  8. UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)

    题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...

  9. UVa 11134 - Fabled Rooks——[问题分解、贪心法]

    We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...

随机推荐

  1. xxx cannot be resolved to a type

    1.jdk不匹配(或不存在)     项目指定的jdk为“jdk1.6.0_18”,而当前eclipse使用的是“jdk1.6.0_22”.需要在BuildPath | Libraries,中做简单调 ...

  2. 零基础逆向工程18_PE结构02_联合体_节表_PE加载过程

    联合体 特点 1.联合体的成员是共享内存空间的 2.联合体的内存空间大小是联合体成员中对内存空间大小要求最大的空间大小 3.联合体最多只有一个成员有效 节表数据结构说明 PE 加载 过程 FileBu ...

  3. Android RecyclerView使用GridLayoutManager间距设置

    使用RecyclerView设置间距,需要重写RecyclerView.ItemDecoration这个类.有如下的效果图需要实现,间距只有中间的格子和底部的格式之间有.   Paste_Image. ...

  4. Centos离线安装Docker并加入到Swarm管理节点

    以root用户登录 加入Swarm前需要在Swarm上生成Token,所以需要提前将Swarm集群搭建完成后,再运行以下命令将各虚机加入到swarm节点 下载docker离线安装包,并拷贝到/root ...

  5. bt5r3开启远程登录

    sshd-generate /etc/init.d/ssh restart

  6. SAP成都研究院DevOps那些事

    今天的文章来自我的同事平静静,SAP成都研究院一位程序媛.平静静2010年加入SAP,熟悉她的人一般都叫她平静.在她待过的每个小组,平静静都不是最引人瞩目的开发人员,然而她总是能一如既往,保质保量地完 ...

  7. 【转】iOS学习笔记(十七)——文件操作(NSFileManager)

    iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...

  8. python 基础之for循环有限循环

    #  range(3) 表示 >>> range(3) [0, 1, 2] for循环 for i in range(3): print(i) 测试 0 1 2 打印1~100的奇数 ...

  9. 剑指offer55 字符流中第一个不重复的字符(最典型错误)

    典型并且基础的错误: class Solution { public: //Insert one char from stringstream void Insert(char ch) { ) res ...

  10. 将unity3d项目嵌入到Android App中使用

    创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...