UVA - 11134 Fabled Rooks问题分解,贪心
题目:点击打开题目链接
思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[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问题分解,贪心的更多相关文章
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 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 ...
- uva 11134 - Fabled Rooks(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- UVA 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
- UVa 11134 Fabled Rooks (贪心+问题分解)
题意:在一个n*n的棋盘上放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 ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
- UVa 11134 - Fabled Rooks——[问题分解、贪心法]
We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...
随机推荐
- 使用xadmin覆盖Django的admin
安装xadmin pip 安装 pip install xadmin 可能会报错 pip install git+git://github.com/sshwsfc/xadmin.git 安装后要使用 ...
- (转)linux traceroute命令参数及用法详解--linux跟踪路由命令
linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通 ...
- 超全面的vue.js使用总结
一.Vue.js组件 vue.js构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue ...
- javac 找不到文件 的可能原因
初学Java还不太明白,竟在些简单的事情上栽跟头,分享一下省的麻烦. 当我们配置好JDK和环境变量之后,在命令行下输入javac,说明我们的安装是正确的.
- MySQL存储过程多条修改语句
DROP procedure Sel_Function_ActivityPastDueDELIMITER $$DROP procedure IF EXISTS`shouyi`.`Sel_Functio ...
- 关于报错“More than one fragment with the name [spring_web] was found. This is not legal ...”的解决办法
最近在搭建一个spring mvc 项目时遇到“More than one fragment with the name [spring_web] was found. This is not leg ...
- VueJs $watch()方法总结!!
最近公司用vue框架写交互,之前没怎么写过,但是很多数据双向绑定的东东跟angular很像!所以上手很快!哈哈 今天就碰到一个vue的问题啊!!产品需求是,datetimepick时间选择器一更改时间 ...
- ios 身份证照片识别信息
一个近乎完整的可识别中国身份证信息的Demo就问问你霸气不
- python+selenium之处理HTML5的视频播放
from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get(" ...
- HDU 1124 Factorial (阶乘后缀0)
题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...