• UVA 221 Urban Elevations


    题目链接:https://vjudge.net/problem/UVA-221

    题目翻译摘自《算法禁赛入门经典》

    题目大意

      有n(n ≤ 100)个建筑物。

      输入每个建筑物左下角坐标(x,y)、宽度(即 x 方向的长度)、深度 (即 y 方向的长度)和高度(以上数据均为实数),输出正视图中能看到的所有建筑物,按 照左下角 x 坐标从小到大进行排序。左下角 x 坐标相同时,按 y 坐标从小到大排序。

      输入保证不同的 x 坐标不会很接近(即任意两个 x 坐标要么完全相同,要么差别足够大, 不会引起精度问题)。

    分析

      没有比离散化更简明的方法了。
      暴力的方法是枚举所有 x 坐标,然后再枚举所有建筑物,如果某个建筑与在某个 x 坐标能被看到,那这个建筑物就能被看到,缺点是由于建筑坐标是实数,所以 x 坐标有无限多。
      讨论这样一种 x 坐标区间 [a, b] ,所有建筑物都完全覆盖它或者完全不覆盖它,那么我们只需要枚举区间上一个点就等同于枚举了区间上的所有点。
      因此我们只需要找出所有满足这种条件的区间,就把无限问题离散化成有限问题了。
      具体方法是:把所有 x 坐标排序去重,则任意两个相邻 x 坐标形成的区间就是我们要找的区间。然后只需在这个区间里任选一个点(例如中点),就能判断出一个建筑物是否在整个区间内可见。如何判断一个建筑物是否在某个 x 坐标处可见呢?首先,建筑物的坐标中必须包含这个 x 坐标,其次,建筑物南边不能有另外一个建筑物也包含这个 x 坐标,并且不比它矮。
      PS:掌握 unique() 函数在普通数组和 vector 上的用法。

    代码如下

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3  
      4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
      5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
      6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
      7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
      8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
      9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     12  
     13 #define pr(x) cout << #x << " = " << x << "  "
     14 #define prln(x) cout << #x << " = " << x << endl
     15  
     16 #define LOWBIT(x) ((x)&(-x))
     17  
     18 #define ALL(x) x.begin(),x.end()
     19 #define INS(x) inserter(x,x.begin())
     20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
     21  
     22 #define ms0(a) memset(a,0,sizeof(a))
     23 #define msI(a) memset(a,inf,sizeof(a))
     24 #define msM(a) memset(a,-1,sizeof(a))
     25 
     26 #define MP make_pair
     27 #define PB push_back
     28 #define ft first
     29 #define sd second
     30  
     31 template<typename T1, typename T2>
     32 istream &operator>>(istream &in, pair<T1, T2> &p) {
     33     in >> p.first >> p.second;
     34     return in;
     35 }
     36  
     37 template<typename T>
     38 istream &operator>>(istream &in, vector<T> &v) {
     39     for (auto &x: v)
     40         in >> x;
     41     return in;
     42 }
     43  
     44 template<typename T1, typename T2>
     45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     46     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     47     return out;
     48 }
     49 
     50 inline int gc(){
     51     static const int BUF = 1e7;
     52     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     53     
     54     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     55     return *bg++;
     56 } 
     57 
     58 inline int ri(){
     59     int x = 0, f = 1, c = gc();
     60     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     61     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     62     return x*f;
     63 }
     64 
     65 template<class T>
     66 inline string toString(T x) {
     67     ostringstream sout;
     68     sout << x;
     69     return sout.str();
     70 }
     71 
     72 inline int toInt(string s) {
     73     int v;
     74     istringstream sin(s);
     75     sin >> v;
     76     return v;
     77 }
     78 
     79 //min <= aim <= max
     80 template<typename T>
     81 inline bool BETWEEN(const T aim, const T min, const T max) {
     82     return min <= aim && aim <= max;
     83 }
     84  
     85 typedef long long LL;
     86 typedef unsigned long long uLL;
     87 typedef pair< double, double > PDD;
     88 typedef pair< int, int > PII;
     89 typedef pair< int, PII > PIPII;
     90 typedef pair< string, int > PSI;
     91 typedef pair< int, PSI > PIPSI;
     92 typedef set< int > SI;
     93 typedef set< PII > SPII;
     94 typedef vector< int > VI;
     95 typedef vector< double > VD;
     96 typedef vector< VI > VVI;
     97 typedef vector< SI > VSI;
     98 typedef vector< PII > VPII;
     99 typedef map< int, int > MII;
    100 typedef map< int, string > MIS;
    101 typedef map< int, PII > MIPII;
    102 typedef map< PII, int > MPIII;
    103 typedef map< string, int > MSI;
    104 typedef map< string, string > MSS;
    105 typedef map< PII, string > MPIIS;
    106 typedef map< PII, PII > MPIIPII;
    107 typedef multimap< int, int > MMII;
    108 typedef multimap< string, int > MMSI;
    109 //typedef unordered_map< int, int > uMII;
    110 typedef pair< LL, LL > PLL;
    111 typedef vector< LL > VL;
    112 typedef vector< VL > VVL;
    113 typedef priority_queue< int > PQIMax;
    114 typedef priority_queue< int, VI, greater< int > > PQIMin;
    115 const double EPS = 1e-8;
    116 const LL inf = 0x7fffffff;
    117 const LL infLL = 0x7fffffffffffffffLL;
    118 const LL mod = 1e9 + 7;
    119 const int maxN = 1e4 + 7;
    120 const LL ONE = 1;
    121 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
    122 const LL oddBits = 0x5555555555555555;
    123 
    124 struct Building{
    125     int id;
    126     double Y, L, R, h;
    127     
    128     Building() {}
    129     Building(int x) : id(x) {}
    130     
    131     bool operator< (const Building &x) const {
    132         if(L == x.L) return Y < x.Y;
    133         return L < x.L;
    134     }
    135 };
    136 
    137 istream& operator >> (istream& in, Building& x){
    138     in >> x.L >> x.Y >> x.R >> x.h >> x.h;
    139     x.R += x.L;
    140     return in;
    141 }
    142 
    143 int N, T;
    144 vector< Building > b;
    145 VI ans;
    146 VD x;
    147 
    148 int main(){
    149     //freopen("MyOutput.txt","w",stdout);
    150     //freopen("input.txt","r",stdin);
    151     //INIT();
    152     while(cin >> N) {
    153         if(!N) break;
    154         ans.clear();
    155         b.clear();
    156         x.clear();
    157         
    158         Rep(i, N) {
    159             Building t(i + 1);
    160             cin >> t;
    161             b.PB(t);
    162             x.PB(t.L);
    163             x.PB(t.R);
    164         }
    165         sort(ALL(b));
    166         sort(ALL(x));
    167         UNIQUE(x);
    168         
    169         Rep(i, b.size()) {
    170             For(j, lower_bound(ALL(x), b[i].L) - x.begin(), x.size() - 2) {
    171                 if(x[j] >= b[i].R) break;
    172                 double m = (x[j] + x[j + 1]) / 2; // 取中点为代表点 
    173                 
    174                 bool flag = true;
    175                 Rep(k, b.size()) {
    176                     if(b[k].h >= b[i].h && b[k].Y < b[i].Y && BETWEEN(m, b[k].L, b[k].R)) {
    177                         flag = false;
    178                         break;
    179                     }
    180                 }
    181                 
    182                 if(flag) {
    183                     ans.PB(b[i].id);
    184                     break;
    185                 }
    186             }
    187         }
    188         
    189         if(T) printf("
    ");
    190         printf("For map #%d, the visible buildings are numbered as follows:
    ", ++T);
    191         Rep(i, ans.size()) printf("%d%c", ans[i], " 
    "[i == ans.size() - 1]);
    192     }
    193     return 0;
    194 }
    View Code
  • 相关阅读:
    $Django 中间件 csrf
    $Django cookies与session--解决无连接无状态问题, session配置
    $Django Form组件
    $Django Paginator分页器 批量创建数据
    $Djangon admin界面 添加表 增删查改
    $Django ajax简介 ajax简单数据交互,上传文件(form-data格式数据),Json数据格式交互
    $Django 多对多-自定义第三张表 基于双下划线的跨表查询(补充)
    $Django 客户端->wsgi->中间组件->urls->views(model,template) 总结+补充(事物,choices,inclusion_tag)!
    $Django 聚合函数、分组查询、F,Q查询、orm字段以及参数
    经典的C++库【转帖】
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/11066988.html
Copyright © 2020-2023  润新知