【例题 8-4 UVA - 11134】Fabled Rooks
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
显然把问题分解成两个子问题。
x轴和y轴分别做。
即n个点要求第i个点在[li,ri]范围内。(ri按左端点、右端点排。尽量取左边的方法是错的。
hack数据:(1,1),(1,3),(2,2)
在安排idx=2的时候,优先用了(1,3)这个区间。导致原本3可以放的。现在放不了了。
所以我们的方法就是。
对于第i个点。
找一个能包含它。
但是右端点又尽量小的区间。
这样,能保证这个选取的区间尽量不影响到后面的点的选取。
(而前面的点无所谓,因为已经安排完了
O(N^2)的复杂度找这样的区间就可以了。
如果数据大的话。
可以考虑用set来处理:比如区间[li,ri]
则可以在li的位置插入一个数据ri
当ri<idx的时候,则在set中把ri去掉。
否则每次都找set中最小的ri.把它删掉。
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 5000;
vector < pair<int,pair<int,int> > > v1,v2;
int n;
int x[N+10],y[N+10];
bool ok(vector<pair<int,pair<int,int> > > v,int ju){
for (int i = 1;i <= n;i++){
int mi = 100000;
__typeof v.begin() idx;
for (auto j = v.begin();j!=v.end();j++){
auto temp = *j;
if (temp.first<=i && i<=temp.second.first){
if (temp.second.first<mi){
mi = temp.second.first;
idx = j;
}
}
}
if (mi==100000) return false;
if (ju==0){
x[(*idx).second.second] = i;
}else{
y[(*idx).second.second] = i;
}
v.erase(idx);
}
return true;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
while (cin >>n && n){
v1.clear(),v2.clear();
for (int i = 1;i <= n;i++){
int xl,yl,xr,yr;
cin >> xl >> yl >> xr >> yr;
v1.push_back({xl,{xr,i}});
v2.push_back({yl,{yr,i}});
}
if (ok(v1,0)&&ok(v2,1)){
for (int i = 1;i <= n;i++)
cout << x[i] <<' '<<y[i]<<endl;
}else{
cout <<"IMPOSSIBLE"<<endl;
}
}
return 0;
}
【例题 8-4 UVA - 11134】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 ...
- 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 (贪心)——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 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11134 - Fabled Rooks——[问题分解、贪心法]
We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...
- UVA 11134 Fabled Rooks
贪心+优先队列+问题分解 对x,y 分开处理 当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列.(y的处理同上) #include <io ...
- UVa 11134 Fabled Rooks(贪心)
题目链接 题意 在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解. ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
随机推荐
- (WPF)XAML 过程式代码
当代码编译.x:Code 元素的内容将被放到.g.cs文件. <Window x:Class="WpfApplication7.MainWindow" xmlns=" ...
- php中对象转数组有哪些方法(总结测试)
php中对象转数组有哪些方法(总结测试) 一.总结 一句话总结:json_decode(json_encode($array),true)和array强制转换(或带递归) 1.array方式强制转换对 ...
- 《读书报告 – Elasticsearch入门 》----Part II 深入搜索(1)
Part II 深入搜索 搜索不仅仅是全文本搜索:数据的很大部分是结构化的值例如日期.数字.这部分开始解释怎样以一种高效地方式结合结构化搜索和全文本搜索. 第十二章 结构化搜索 结构化搜索_ 是指查询 ...
- nodejs-website
http://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs http://nodeapi.ucdok.com/ ...
- eq3
然而一旦美国经济进入持续复苏,美联储必将逐步退出量化宽松和逐渐收紧银根,美国联邦基金利率和银行同业间拆借利率将会上升.这将使美元升值,并使部分套利交易平仓.而一旦美元企稳走强,国债利率上扬,大宗商品价 ...
- sublime 编辑器汉化
一.下载Sublime编辑器 官网下载地址:http://www.sublimetext.com/3 二.下载汉化包 汉化包下载地址:http://files.cnblogs.com/akwwl/su ...
- TCP/IP协议族-----20、远程登录:TELNET与SSH
- MongoDB + node-mongoskin简单演示样例
特点 无模式 MongoDB 中的每一条文档,都是一个 JSON 对象,因此你无需提前定义一个集合的结构,集合中的每一个文档也能够有不同的结构. 异步写入 MongoDB 默认全部的写操作都是『不安全 ...
- elasticsearch index 之 Mapping
Lucene索引的一个特点就filed,索引以field组合.这一特点为索引和搜索提供了很大的灵活性.elasticsearch则在Lucene的基础上更近一步,它可以是 no scheme.实现这一 ...
- MATLAB 最优化计算 (一)
1,令多行命 —— 逗号 VS 分号 2,管理工作空间 —— who , whos , clear , save , load , length (向量显示其长度,矩阵显示行数与列数中的较大数) s ...