BZOJ1645 [Usaco2007 Open]City Horizon 城市地平线

本来想打线段树的说。。。

就是把坐标离散化了,然后区间最大求和即可。。。

后来觉得有点烦的说(silver题就要线段树。。。),于是看了下usaco的题解,发现了个高端的东西:善用STL里的容器和迭代器就可以了。

以下就是高端程序:

 1 /**************************************************************
 2     Problem: 1645
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:264 ms
 7     Memory:6120 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <vector>
12 #include <utility>
13 #include <map>
14 #include <set>
15 #include <algorithm>
16
17 using namespace std;
18 typedef pair<int, int> PAIR;
19 typedef long long ll;
20
21 map<int, vector<PAIR> > M;
22 map<int, vector<PAIR> > ::iterator now;
23 vector <PAIR> ::iterator I;
24 multiset <int, greater<int> > S;
25 ll ans;
26 int n, L, R, H, K;
27
28 inline int read(int &x){
29     int sgn = 1; x = 0;
30     char ch = getchar();
31     while (ch < ‘0‘ || ch > ‘9‘){
32         if (ch == ‘-‘) sgn = -1;
33         ch = getchar();
34     }
35     while (ch >= ‘0‘ && ch <= ‘9‘){
36         x = x * 10 + ch - ‘0‘;
37         ch = getchar();
38     }
39     x *= sgn;
40 }
41
42 int main(){
43     read(n);
44     for (int i = 1; i <= n; ++i){
45         read(L), read(R), read(H);
46         M[L].push_back(make_pair(H, 1));
47         M[R].push_back(make_pair(H, 0));
48     }
49     L = 0;
50     for (now = M.begin(); now != M.end(); ++now){
51         R = now -> first;
52         if (!S.empty()) ans += (ll) (R - L) * (*S.begin());
53         L = R;
54         for (I = (now -> second).begin(); I != (now -> second).end(); ++I){
55             K = I -> first;
56             if (I -> second) S.insert(K);
57             else S.erase(S.find(K));
58         }
59     }
60     printf("%lld\n", ans);
61     return 0;
62 }

(反正蒟蒻自己是不会写的。。。真是太神了!!!)

时间: 2024-10-10 23:10:06

BZOJ1645 [Usaco2007 Open]City Horizon 城市地平线的相关文章

1645: [Usaco2007 Open]City Horizon 城市地平线

1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: 157[Submit][Status] Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe t

BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

Description N个矩形块,交求面积并. Input * Line 1: A single integer: N * Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i Output * Line 1: The total area, in square units, of the silhouettes formed by all

bzoj1683[Usaco2005 Nov]City skyline 城市地平线

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1683 Input 第1行:2个用空格隔开的整数N和W. 第2到N+1行:每行包括2个用空格隔开的整数x,y,其意义如题中所述.输入中的x严格递增,并且第一个z总是x. Output 输出一个整数,表示城市中最少包含的建筑物数量. Sample Input 10 26 1 1 2 2 5 1 6 3 8 1 11 0 15 2 17 3 20 2 22 1 INPUT DETAILS: T

bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 $upd:$本题的底端点坐标简化为$(x,0)$ 蓝后对纵坐标建一棵线段树(本题需要对高度进行离散化). 每次对线段树进行覆盖$or$删除区间操作,顺便统计一下$k=$有多少点被覆盖到 而两次(线段)操作之间的长度为$r=x_{i}-x_{i-1}$ 于是两条线段之间被覆盖的面积即为$k*r$ (

BZOJ1628: [Usaco2007 Demo]City skyline

1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Submit][Status] Description The best part of the day for Farmer John's cows is when the sun sets. They can see the skyline of the distant city. Bessie w

POJ 3277 City Horizon

City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18555   Accepted: 5114 Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouette

xtu数据结构 H. City Horizon

H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silh

ACM基础训练题解4301 城市地平线

遍历线段树   线段树的插入和查询 1 //城市地平线(线段树) 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<cstdio> 7 using namespace std; 8 typedef __int64 LL; 9 struct building{ 10 LL x1; 11 LL x2; 12 LL

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <