【紫书】Urban Elevations UVA - 221 离散化
题意:给你俯视图,要求依次输出正视图中可以看到的建筑物
题解:任意相邻的x间属性相同,所以离散化。
坑:unique只能对数组用。下标易错
list不能找某元素的next。用了个很麻烦的处理
数组:
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
//ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
const int maxn = 1e2 + ;
struct building {
int id;
double x, y, w, d, h;
bool operator<(const building& rhs)const {
return x < rhs.x || (x == rhs.x&&y < rhs.y);
}
}b[maxn];
int n;
double x[maxn * ];
bool cover(int i, double mx) {
return b[i].x<mx&&b[i].x + b[i].w>mx;
}
bool visible(int i, double mx) {
if (!cover(i, mx))return false;
rep(k, , n)
if (b[k].y < b[i].y&&b[k].h >= b[i].h&&cover(k, mx))return false;
return true;
} void Run() { } void smain() { int kase = ;
while (cin >> n) {
if (n == )break;
rep(i, , n)
{
cin >> b[i].x >> b[i].y >> b[i].w >> b[i].d >> b[i].h;
x[i * ] = b[i].x; x[i * + ] = b[i].x + b[i].w;
b[i].id = i;
}
sort(b + , b + + n);
sort(x + , x + + n * );
int m = unique(x + , x + + n * ) - x-;
if (kase++)cout << endl;
printf("For map #%d, the visible buildings are numbered as follows:\n%d", kase, b[].id);
rep(i, , n) {
bool vis = false;
rep(j, ,m ) if (visible(i, (x[j] + x[j + ]) / )) { vis = true; break; }
if (vis)cout << ' ' << b[i].id;
}
cout << endl; }
}
用list
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
//ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
const int maxn = 1e2 + ;
struct building {
int id;
double x, y, w, d, h;
bool operator<(const building& rhs)const {
return x < rhs.x || (x == rhs.x&&y < rhs.y);
}
}b[maxn];
int n;
double x[maxn * ];
list<double> l;
bool cover(int i, double mx) {
return b[i].x<mx&&b[i].x + b[i].w>mx;
}
bool visible(int i, double mx) {
if (!cover(i, mx))return false;
rep(k, , n)
if (b[k].y < b[i].y&&b[k].h >= b[i].h&&cover(k, mx))return false;
return true;
} void Run() { } void smain() { int kase = ;
while (cin >> n) {
if (n == )break;
rep(i, , n)
{
cin >> b[i].x >> b[i].y >> b[i].w >> b[i].d >> b[i].h; l.push_back(b[i].x), l.push_back(b[i].x + b[i].w);
b[i].id = i;
}
sort(b + , b + + n); l.sort(); l.unique();
if (kase++)cout << endl;
printf("For map #%d, the visible buildings are numbered as follows:\n%d", kase, b[].id);
rep(i, , n) {
bool vis = false;
int last = l.front();
for (auto t : l) if (t != l.front() && visible(i, (t + last) / )) { vis = true; break; }
else last = t;
if (vis)cout << ' ' << b[i].id;
}
cout << endl; }
}
【紫书】Urban Elevations UVA - 221 离散化的更多相关文章
- Urban Elevations UVA - 221
题目大意:给出建筑的俯视图,以及每个建筑的左下角坐标,宽度,长度,高度.求正视图可观察到的建筑的编号 思路:建筑物的可见性等于南墙的可见性,依据左下角排序后,逐个判断每个建筑是否可见.对南墙的x坐标进 ...
- 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)
这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...
- UVa 221 Urban Elevations 城市正视图 离散化初步 无限化有限
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 221 Urban Elevations 给出城市中建筑物的x, ...
- 紫书 例题8-3 UVa 1152(中途相遇法)
这道题要逆向思维, 就是求出答案的一部分, 然后反过去去寻找答案存不存在. 其实很多其他题都用了这道题目的方法, 自己以前都没有发现, 这道题专门考这个方法.这个方法可以没有一直往下求, 可以省去很多 ...
- 紫书 例题8-12 UVa 12627 (找规律 + 递归)
紫书上有很明显的笔误, 公式写错了.g(k, i)的那个公式应该加上c(k-1)而不是c(k).如果加上c(k-1)那就是这一次 所有的红气球的数目, 肯定大于最下面i行的红气球数 我用的是f的公式, ...
- 紫书 例题8-4 UVa 11134(问题分解 + 贪心)
这道题目可以把问题分解, 因为x坐标和y坐标的答案之间没有联系, 所以可以单独求两个坐标的答案 我一开始想的是按照左区间从小到大, 相同的时候从右区间从小到大排序, 然后WA 去uDebug找了数据 ...
- 紫书 习题 11-9 UVa 12549 (二分图最小点覆盖)
用到了二分图的一些性质, 最大匹配数=最小点覆盖 貌似在白书上有讲 还不是很懂, 自己看着别人的博客用网络流写了一遍 反正以后学白书应该会系统学二分图的,紫书上没讲深. 目前就这样吧. #includ ...
- 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)
很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...
- 紫书 习题8-12 UVa 1153(贪心)
本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...
随机推荐
- 【CLR】解析AppDomain
目录结构: contents structure [+] 什么是AppDomain 跨越AppDomain边界访问对象 按引用封送(Marshal-by-Reference) 按值封送(Marshal ...
- Swift Assert 断言
前言 对每次运行都会出现的错误通常不会过于苦恼,可以使用断点调试或者 try catch 之类的方式判断并修复它.但是一些偶发(甚至是无数次运行才会出现一次)的错误单靠断点之类的方式是很难排除掉的,为 ...
- Java注解应用,自定义注解映射实现方案说明.
插件结构如图: 注册模块定义了三个:用于实体与表映射的注解,用于属性到表字段的映射,用于映射时过滤掉的注解. 1.用于实体与表映射的注解 package com.dobby.plugins.annot ...
- ASP.NET中TimeSpan的用法
一.TimeSpan常量.字段 TimeSpan.MaxValue; // 10675199.02:48:05.4775807TimeSpan.MinValue; //-10675199.02:48: ...
- [转]protoc-gen-lua 编译、安装、使用教程
版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ https://blog.csdn.net/cp7906216 ...
- Odoo 8 Graph 视图 之 雷达图 (Radar\Spider)
据说7.0是有Radar图的,但是8以后被阉割掉了.自己动手 ,丰衣足食. 经过一天的努力,雷达图现已成功加入群共享套餐.
- Socket阻塞模式和非阻塞模式的区别
简单点说: 阻塞就是干不完不准回来, 非组赛就是你先干,我现看看有其他事没有,完了告诉我一声 我们拿最常用的send和recv两个函数来说吧... 比如你调用send函数发送一定的Byte,在系 ...
- tensflow自定义损失函数
tensflow 不仅支持经典的损失函数,还可以优化任意的自定义损失函数. 预测商品销量时,如果预测值比真实销量大,商家损失的是生产商品的成本:如果预测值比真实值小,损失的则是商品的利润. 比如如果一 ...
- Javascript 检测键盘按键
Javascript中 有3个事件句柄在对应键盘的输入状态:keydown.keypress和keyup. 分别对应的意思是:按键被按下(按下按键但还没有抬起).点击按键(按下并抬起按键).按键抬起( ...
- 和我一起学Effective Java之泛型
泛型 不要在新代码中使用原始类型 泛型(generic):声明中具有一个或多个类型参数 原始类型(raw type):不带任何实际类型参数的泛型名称 格式: 类或接口的名称 < 对应于泛型形式类 ...