acwing 2041.haystack

2014. Haystack

topic

Bessie is sorry for all the mischief she has caused around the farm lately, and agrees to help Farmer John stack up a new batch of hay bales.

At the beginning, there are N empty haystacks, numbered 1∼N.

John gave Bessie K instructions, each of which is in the format AB, which means that Bessie wants to add a new hay bale to the top of each haystack in the range A...B.

For example, if Bessie receives the instruction 10 13, she should add a hay bale to haystacks 10, 11, 12, and 13.

After Bessie completes all the instructions, John wants to know the median height of the N haystacks—that is, the height of the middle haystack if the haystacks are arranged in ascending order of height.

For convenience, N must be an odd number, so the middle heap is unique.

Please help Bessie determine the answer to John's question.

Input format
The first line contains N and K.

The next K lines, each line contains two integers A and B, are used to describe an instruction.

Output format:
Output the median height of N haystacks after completing all instructions.

data range

1≤N≤106,
1≤K≤25000,
1≤A≤B≤N

Input example:

7 4
5 5
2 4
4 6
3 5

Output sample:

1

Sample explanation:
After Bessie completes all instructions, the heights of each pile are 0,1,2,3,3,1,0.

After sorting the heights from small to large, we get 0,0,1,1,2,3,3, with 1 in the middle.

Ideas

This problem can be solved using differential thinking.

    for(int i = 1; i <= k; i++){
    
    
        cin >> a >> b;
        h[a]++, h[b + 1]--;  
    }
    for(int i = 1; i <= n; i++)h[i] += h[i - 1];

Version 1: Use differences to solve problems

#include<iostream>
#include<algorithm>
using namespace std;



int h[1000010];
int main(){
    
    
    int n, k;
    cin >> n >> k;
    
    int a,b;
    for(int i = 1; i <= k; i++){
    
    
        cin >> a >> b;
        h[a]++, h[b + 1]--;  
    }
    for(int i = 1; i <= n; i++)h[i] += h[i - 1];
    sort (h + 1, h + n + 1); 
    cout << h[(n + 1) >> 1];
    
    return 0;
}

Version 2: Improve running speed

The second version is modified based on the first version:
① Since the number of input cycles is large, the input operation is performed instead scanf, cinwhich can improve the running speed;

		// cin >> a >> b;
        scanf("%d %d", &a, &b);

② Since you only need to get a certain number in a specific order in this sequence, use a function nth_elementinstead of sorta function, which can improve the running speed.

    // sort (h + 1, h + n + 1);  
    nth_element(h+1, h + n/2 + 1, h + n + 1);
#include<iostream>
#include<algorithm>
using namespace std;



int h[1000010];
int main(){
    
    
    int n, k;
    cin >> n >> k;
    
    int a,b;
    for(int i = 1; i <= k; i++){
    
    
        scanf("%d %d", &a, &b);
        h[a]++, h[b + 1]--;  
    }
    for(int i = 1; i <= n; i++)h[i] += h[i - 1];
    // sort (h + 1, h + n + 1);  
    nth_element(h+1, h + n/2 + 1, h + n + 1);
    cout << h[(n + 1) >> 1];
    
    return 0;
}

Comparison of results
Insert image description here
It can be seen that the optimization effect is great.

**

The above is a personal solution. If you have any questions, please point them out. I hope I can help you~

**

Guess you like

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