2017 ACM ICPC North Central North America Regional Contest F-Atlantis

[Monotonous stack] [Title] template

You may have heard of the lost city of Atlantis. As legend goes, Atlantis was a city of great wealth and power. Then the Gods became displeased with Atlantis and sank it into the ocean. What you may not have heard is the story of Demetrios, the only person in Atlantis with a ship at the time the city was pushed underwater.

Demetrios was an incredibly selfish man and thought of only one thing when he noticed the water level in the city rising: collecting the wealth of accessible gold in Atlantis and transporting it into his ship before the treasure was lost forever. Luckily for Demetrios, when the water level began rising, the many guards keeping safe the gold stores abandoned their posts to seek safety.

Demetrios knows the location of every gold store in Atlantis and how long it will take him to get to a particular store and back to his ship. He also knows the altitude at which each gold store resides, which determines when the store will be swallowed by the rising sea. The trouble is that he's not sure he'll have time to make it to every store before they become submerged. He now wonders the maximum number of stores he can visit prior to their respective submersion, if he picks his schedule optimally.

During the 2017 NCNA Regional, the following clarification was posted: "Important: The gold store must remain above water during the ENTIRE trip to and from the store.

The first line of input will contain the integer nn, (1≤n≤200000)(1n200000), the number of gold stores in Atlantis. The next nn lines will contain two integers on each line, t_iti and h_ihi(1≤t_i,h_i≤10^9)(1ti,hi109), the round-trip time in seconds it will take Demetrios to visit store ii and return to his ship with the gold, and the feet above sea level of store ii, respectively. 

\\[20pt]\normalsize \textbf{Output}Output

Output the maximum number of gold stores Demetrios can visit such that each is visited prior to it becoming submerged. Assume sea level rises by one foot every second, starts at height 00, and begins rising immediately.

Extra spaces at the end of each row, the answer does not affect the validity of the output

 

 

 

Determine the greedy, the shortest time to go into all the best. Time is short but a lower height would happen.

Solution: Enter the numbers first sort by height, then turn into a priority queue, setting a record total time required now_time queue is how much each plug one can assume that this will go into the first, than if now_time we go into new heights high, then it is required to eject the longest.

 

Why are sorted according to height? , The optimal solution for the height h when the state of the queue is guaranteed to go into the height to h.

While excluding the longest time occupied the solution, but the new figures add to the mix, one to one to ans remain unchanged, but the shorter the time required. If you do not exclude the ans + 1. In order to achieve "continue to read into the cache array elements, remove the oldest element from time to time, the ad hoc querying the current cache array of the smallest elements."

#include <bits/stdc++.h>
using namespace std;
#define maxn 200000
struct node{
    int t,h;
    bool operator < (const node &x) const{
        return t<x.t;
    }
}a[maxn];
priority_queue <node> q;

bool cmp(const node &a,const node &b){
    if (a.h==b.h) return a.t<b.t; else return a.h<b.h;
}

int main(){
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d%d",&a[i].t,&a[i].h);
    sort(a+1,a+1+n,cmp);
    int now_time=0;
    for (int i=1;i<=n;i++) {
        now_time+=a[i].t;
        q.push(a[i]);
        while (now_time>a[i].h) {
            node k=q.top();
            q.pop();
            now_time-=k.t;
        }
    }
    cout<<q.size()<<endl;

}

  

Guess you like

Origin www.cnblogs.com/asanagiyantia/p/12446152.html