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

【单调栈】【模板题】

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.

输出时每行末尾的多余空格,不影响答案正确性

 

 

 

确定贪心,把时间最短的都塞进去是最好的。会出现时间虽然短但是高度更低的情况。

解法:先把输入的数字按高矮排序,然后依次塞进优先队列里,设定一个now_time记录队列里所需的总时间是多少,每塞一个,都先假定这个可以塞进去,如果now_time比我们新塞进去的高度高的话,就弹出所需时间最长的。

 

为什么按照高矮排序?保证队列中的状态是塞进去的高度为h时的,对于高度为h的最优解。

而排除时间占用最长的解,但又新加进来的数字,一来一去ans维持不变,但是所需的时间变短。如果不用排除,则ans+1。以达到“不断地向缓存数组里读入元素,也不时地去掉最老的元素,不定期的询问当前缓存数组里的最小的元素。”

#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;

}

  

猜你喜欢

转载自www.cnblogs.com/asanagiyantia/p/12446152.html