单调栈和优先队列

单调栈和优先队列

一. 单调栈

二. 优先队列

========================================================

单调栈

单调栈:栈内数值单调递增或递减的栈。
单调栈的生成:
1.从所给数组里面拿出一个数。
2.若栈为空,直接放入并记录当前原数组位置。
3.若栈不为空,将栈顶元素与数组中得到的数进行比较。
4.若栈顶元素比数组中得到的数大,则弹出栈顶元素,继续比较新的栈顶元素,直至栈顶元素小于该数或者栈空。
5.若栈顶元素比数组中得到的元素小,则先记录当前栈顶元素,然后将提取的数压入栈。

代码实现
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f
using namespace std;
const int N = 1010;
stack <int> s;
int n,list[N],L[N];
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) 
		cin >> list[i];
    for(int i = 0; i < n; i++)
	{
        while(!s.empty()&&list[s.top()]>=list[i])//这个栈单调递减,单调递增改成<=; 
            s.pop();
        if(s.empty())
            L[i] = 0;
        else 
			L[i] = s.top();
        s.push(i);
    }
    return 0;
}
例题:City Skyline

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 wonders how many buildings the city has. Write a program that assists the cows in calculating the minimum number of buildings in the city, given a profile of its skyline.
The city in profile is quite dull architecturally, featuring only box-shaped buildings. The skyline of a city on the horizon is somewhere between 1 and W units wide (1 <= W <= 1,000,000) and described using N (1 <= N <= 50,000) successive x and y coordinates (1 <= x <= W, 0 <= y <= 500,000), defining at what point the skyline changes to a certain height.
An example skyline could be:

…XX…XXX…
.XXX.XX…XXXXXXX…
XXXXXXXXXX…XXXXXXXXXXXX
and would be encoded as (1,1), (2,2), (5,1), (6,3), (8,1), (11,0), (15,2), (17,3), (20,2), (22,1).
This skyline requires a minimum of 6 buildings to form; below is one possible set of six buildings whose could create the skyline above:
… …
…22…333… …XX…XXX…
.111.22…XX333XX… .XXX.XX…5555555…
X111X22XXX…XX333XXXXXXX 4444444444…5555555XXXXX3

…XX…XXX…
.XXX.XX…XXXXXXX…
XXXXXXXXXX…666666666666
Input:

  • Line 1: Two space separated integers: N and W
  • Lines 2…N+1: Two space separated integers, the x and y coordinate of a point where the skyline changes. The x coordinates are presented in strictly increasing order, and the first x coordinate will always be 1.

Output:

  • Line 1: The minimum number of buildings to create the described skyline.

Sample Input:
10 26
1 1
2 2
5 1
6 3
8 1
11 0
15 2
17 3
20 2
22 1
Sample Output:
6

题意:以坐标的形式给出一张图,表示建筑的正视图,求出楼房的最少个数。
分析:构建一个按高度递增的单调栈,遍历所有的高度,如果栈顶元素大于当前高度,说明栈顶元素所代表高度的楼找到了,因为比它低的点不能再被该栈顶元素覆盖,否则其坐标不会比其低,所以每pop一次就增加一个楼。
代码
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f
using namespace std;
const int N = 5e4+10;
int a[N];
int n,m,x,ans;
stack<int> s;
int main()
{
	scanf("%d%d",&m,&n);
	for(int i = 1; i <= m; i++)
		scanf("%d%d",&x,&a[i]);
	a[0] = 0;
	a[m+1] = 0;
	while(!s.empty())
	     s.pop();
	s.push(0);
	for(int i = 1; i <= m+1; i++)   
	{
	 	while(!s.empty()&&a[s.top()]>a[i])  
		{
			s.pop();
			ans++;
		}
		if(a[s.top()]!=a[i]) 
			s.push(i);
	}
	printf("%d\n",ans);
   return 0;
}

========================================================

优先队列

在默认的优先队列 (默认为降序排序)priority_queue q当中,队首的元素是最大的,但是有些时候我们想使用优先队列但是又想要升序排序,那么这个默认的优先队列是没有办法满足我们的需求的,那么我们有两种方法来解决这个问题,第一个,把所有输入的数据(一般全为整数)取反在放入这个默认的优先队列,这样我们吗就可以得到升序排列的数了,最后只需要 -q.top()就好了。第二个,就是使用小根堆的优先队列。

========================================================
less < int > 表示数字大的优先级越大;
greater < int > 表示数字小的优先级越大;
priority_queue < int > q;
priority_queue < int,vector < int > , less < int > > q;
这两种写法是等价的,也就是默认的降序排列;
升序排列:priority_queue < int,vector < int > , greater < int > > q;
在这么写的时候 less < int > 和 greater < int > 后面有一个空格;
同时在使用小根堆的时候还学要加上头文件 #include < functional >;

========================================================

例题 :Fence Repair

description:Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input:
Line 1: One integer N, the number of planks
Lines 2…N+1: Each line contains a single integer describing the length of a needed plank

Output:
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input:
3
8
5
8
Sample Output:
34

题意:分成指定长度的n份,每次花的钱和每次切割的长度相同
分析:小根堆优先队列,因为要花费最少,所以每一切小的,把大的留到后面。用 long long 不然会溢出。
代码:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f
using namespace std;
const int N = 20020;
priority_queue<long long,vector<long long>,greater<long long> > q;
long long n,node,sum;
int main()
{
	scanf("%lld",&n);
	for(int i = 1; i <= n; i++)
	{
		scanf("%lld",&node);
		q.push(node); 
	}
	long long x,y;
	for(int i = 1; i < n; i++)
    {
        x = q.top();
        q.pop();
        y = q.top();
        q.pop();
        x = x + y;
        sum += x;
        q.push(x);
    }
    printf("%lld\n",sum);
	return 0;
}

你可能感兴趣的:(单调栈和优先队列)