↓ 跳过正文
  1. Posts/

BZOJ 1628: [Usaco2007 Demo]City skyline (单调栈)

·502 字·2 分钟

1628: [Usaco2007 Demo]City skyline
#

Time Limit: 5 Sec Memory Limit: 64 MB Submit: 396 Solved: 317 [Submit][Status][Discuss]

Description
#

City skyline 示意图

Input
#

第一行给出N,W

第二行到第N+1行:每行给出二个整数x,y,输入的x严格递增,并且第一个x总是1

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:

The case mentioned above

Sample Output
#

6

思路:我是正着做的,判断条件没有问题,但是细节不好处理,一直WA..大概是有什么地方没想到? 正解是单调栈。

转载一段题解:

答案的上限 肯定是 n, 何时会减一呢? 当有两座楼高度相等且它们的中间没有比它们低的楼。

所以要维护的是一个单调递增的序列, 每次弹出比它大的直到遇到一个和它相等的, 没有相等的话就把 它加入这个序列中。

实现很简单。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月04日 星期一 15时23分45秒
 4File Name :code/bzoj/1628.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E6+7;
34int n,w;
35int x[N],y[N];
36int st[N];
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	ios::sync_with_stdio(false);
44	cin>>n>>w;
45	for ( int i = 1 ; i <= n ; i++)
46	{
47	    cin>>x[i]>>y[i];
48	}
49
50	int top = 0;
51	int ans = n;
52	for ( int i = 1 ;i  <= n ; i++)
53	{
54	    while (top&&y[i]<st[top]) top--;
55	    if (st[top]==y[i]) ans--;
56	    else st[++top] = y[i];
57	}
58	cout<<ans<<endl;
59
60
61
62  #ifndef ONLINE_JUDGE
63  fclose(stdin);
64  #endif
65    return 0;
66}

相关文章

codeforces 442C. Artem and Array

·492 字·1 分钟
C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn’t have an adjacent number to the left or right, Artem doesn’t get any points.

BZOJ 1627: [Usaco2007 Dec]穿越泥地 (BFS)

·1161 字·3 分钟
1627: [Usaco2007 Dec]穿越泥地 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 624 Solved: 411 [Submit][Status][Discuss] Description # 清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (-500 <= X <= 500; -500 <= Y <= 500)处。当然咯, FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 (A_i, B_i) (-500 <= A_i <= 500;-500 <= B_i <= 500)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。

BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路 (MST)

·1176 字·3 分钟
1626: [Usaco2007 Dec]Building Roads 修建道路 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1362 Solved: 541 [Submit][Status][Discuss] Description # Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场)。有些农场之间原本就有道路相连。 所有N(1 <= N <= 1,000)个农场(用1..N顺次编号)在地图上都表示为坐标为(X_i, Y_i)的点(0 <= X_i <= 1,000,000;0 <= Y_i <= 1,000,000),两个农场间道路的长度自然就是代表它们的点之间的距离。现在Farmer John也告诉了你农场间原有的M(1 <= M <= 1,000)条路分别连接了哪两个农场,他希望你计算一下,为了使得所有农场连通,他所需建造道路的最小总长是多少。

BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 (Floyd)

·804 字·2 分钟
1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 507 Solved: 345 [Submit][Status][Discuss] Description # 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上经过的小岛依次出现了Ai,A2,…,AM(2≤M≤10000)这样的序列(不一定相邻),那他最终就能找到古老的宝藏. 但是,由于牛勒比海有海盗出没.约翰知道任意两个岛屿之间的航线上海盗出没的概率,他用一个危险指数Dij(0≤Dij≤100000)来描述.他希望他的寻宝活动经过的航线危险指数之和最小.那么,在找到宝藏的前提下,这个最小的危险指数是多少呢?

BZOJ 1623: [Usaco2008 Open]Cow Cars 奶牛飞车 (贪心)

·811 字·2 分钟
1623: [Usaco2008 Open]Cow Cars 奶牛飞车 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 386 Solved: 266 [Submit][Status][Discuss] Description # 编号为1到N的N只奶牛正各自驾着车打算在牛德比亚的高速公路上飞驰.高速公路有M(1≤M≤N)条车道.奶牛i有一个自己的车速上限Si(l≤Si≤1,000,000).