【紫书】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 0;
}
const int maxn = 1e2 + 5;
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 * 2];
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, 1, 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 = 0;
    while (cin >> n) {
        if (n == 0)break;
        rep(i, 1, n)
        {
            cin >> b[i].x >> b[i].y >> b[i].w >> b[i].d >> b[i].h;
            x[i * 2] = b[i].x; x[i * 2 + 1] = b[i].x + b[i].w;
            b[i].id = i;
        }
        sort(b + 1, b + 1 + n);
        sort(x + 2, x + 2 + n * 2);
        int m = unique(x + 2, x + 2 + n * 2) - x-2;
        if (kase++)cout << endl;
        printf("For map #%d, the visible buildings are numbered as follows:\n%d", kase, b[1].id);
        rep(i, 2, n) {
            bool vis = false;
            rep(j, 2,m ) if (visible(i, (x[j] + x[j + 1]) / 2)) { 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 0;
}
const int maxn = 1e2 + 5;
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 * 2];
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, 1, 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 = 0;
    while (cin >> n) {
        if (n == 0)break;
        rep(i, 1, 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 + 1, b + 1 + n);
        
        l.sort();
        
        l.unique();
        if (kase++)cout << endl;
        printf("For map #%d, the visible buildings are numbered as follows:\n%d", kase, b[1].id);
        rep(i, 2, n) {
            bool vis = false;
            int last = l.front();
            for (auto t : l) if (t != l.front() && visible(i, (t + last) / 2)) { vis = true; break; }
            else last = t;
            if (vis)cout << ' ' << b[i].id;
        }
        cout << endl;



    }
}

猜你喜欢

转载自www.cnblogs.com/SuuT/p/8988914.html