Algorithm greedy +pair ——AcWing 2014. Island

topic

Whenever it rains, Farmer John's fields always flood.

Because the fields are not perfectly level, some areas are filled with water, leaving many "islands" separated by water.

John's field is described as a one-dimensional scene specified by Ncontinuous height values ​​.H1,…,HN

Assuming that the scene is surrounded by infinitely high walls, consider what happens during a rainstorm:

The lowest parts are first covered by water, forming discrete islands that will eventually be covered as the water level continues to rise.

Once the water level equals the height of a field, that field is considered to be underwater.
Insert image description here
The image above shows an example: in the image on the left, we have added just over 1 unit of water, leaving 4 islands (the maximum number of islands remaining), while in the image on the right, we have added a total of 7 units of water. water, there are only 2 islands left at this time.

Please calculate the maximum number of islands we can see at any one time during a storm.
The water will rise until all the fields are underwater.

Input format
The first line contains the integer N.

The next N lines, each line contains an integer representing Hi.

Output Format
Outputs the maximum number of islands we can see at a point in time during a storm.

Data range
1≤N≤105,
1≤Hi≤109

Input example:

8
3
5
2
3
1
4
2
3

Output sample:

4

Problem explanation

The idea is
that as the water level rises, the islands will be covered by the water one by one according to the order of island height from low to high, so here a greedy (enumeration) method is used to enumerate the islands from low to high. During the enumeration process, the islands that are still there are The number of islands on the water changes.
PS:
①For each island covered by the water surface, there are four situations:
Insert image description here

It can be found that only when the state of "high in the middle and low on both sides" occurs, the number of islands decreases 1. When "low in the middle and high on both sides" occurs, the number of islands increases 1. In other cases, the number of islands remains unchanged.

code

#include<iostream>
#include<cstring>
#include<algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 100010;

int n;
int h[N];//记录每座山的高度,用于枚举每个q时与相邻h对比
PII q[N];//q用于高度排序后枚举

int main(){
    
    
    
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &h[i]);

    n = unique(h + 1, h + n + 1 ) - h - 1;//判重得到  返回得到判重后的所有元素
    h[n + 1] = 0;
    
    //printf("n=%d\n",n);
    for(int i = 1; i <= n; i ++) q[i] = {
    
    h[i], i}; //将【每座岛的高度,下表序号】存入q
    
    sort(q + 1, q + n + 1);//排序
    
    int res = 1;//定义答案
    int cnt = 1;//定义当前岛数量
    
    for(int i = 1; i <= n; i++){
    
    
        int k = q[i].y;
        if(h[k -1] < h[k] && h[k + 1] < h[k]) cnt--;
        else if(h[k -1] > h[k] && h[k + 1] > h[k]) cnt++;
        
        if( q[i].x != q[i + 1].x)
            res = max(res, cnt);
        
    }
    printf("%d\n", res);
    
    return 0;
}

Hope it helps, please like and comment

Guess you like

Origin blog.csdn.net/smile66688/article/details/122993704