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 ...
随机推荐
- mycat学习日记:全局sequence
mycat分库分表的情况下,原生mysql的自增长主键无法满足主键全局唯一这个要求.看了MYCAT社区从零开始的一篇博客,加上自己的实践,大概总结一下. 目前mycat对于全局sequence主要提供 ...
- mysql导入sql文件错误#1044 - Access denied for user 'root'@'localhost'
在我的个人知识管理中,经常用到mysql数据库,wordpress搭建的worklog.搜索测试数据.我blog的测试环境等.我在自己的电脑上整了WAMP(Windows Apache MySQL P ...
- Spring Boot Admin 监控中心
Spring Boot Admin 监控中心 Spring Boot Admin用来收集微服务系统的健康状态.会话数量.并发数.服务资源.延迟等度量信息 服务端 建立spring-cloud-admi ...
- This file's format is not supported or you don't specify a correct format. 解决办法
string path = @"c:\请假统计表.xlsx"; Workbook workBook = new Workbook(); workBook.Open(path); A ...
- SpringBoot 2.x (14):WebFlux响应式编程
响应式编程生活案例: 传统形式: 一群人去餐厅吃饭,顾客1找服务员点餐,服务员把订单交给后台厨师,然后服务员等待, 当后台厨师做好饭,交给服务员,经过服务员再交给顾客1,依此类推,该服务员再招待顾客2 ...
- redis 大批量数据插入导致MISCONF Redis is configured to save RDB snapshots的解决
PS:之前写过一遍,那个方法没有彻底解决,现找到真正的解决方法 环境:redis 3.2.100 windows版(注意!!!这是关键),win10,redis客户端spring boot 2.0.7 ...
- Android 4.4及以后将内容布局延伸到状态栏
首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示: 界面 这个界面的布局就是co ...
- android动画ppt整理
案例
- ArcSDE 10.1 For Windows 创建空间数据库与常见错误_SQL Server
本文是2013年时候参加ESRI竞赛,创建ArcSDE 10.1 for SQL Server时候出问题了,因此写了该文档. 由于一直忙于学习,忘了发布.今天一师弟也遇到同样问题,为此我觉得可能有不少 ...
- /usr/local/sbin/fping -s www.baidu.com www.google.com
/usr/local/sbin/fping -s www.baidu.com www.google.com