Challenges Programming Contest: Expeition

Subject to the effect

Here Insert Picture Description

Problem-solving ideas

  • After the gas station as a chance to refuel, pile into the big top, when no oil when refueling can be removed from the big top of the heap up to increase opportunities for oil.
    Here Insert Picture Description

Code

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

const int MAXN = 10005;
const int inf = 1000000;
pair<int, int> stops[MAXN];
priority_queue<int, vector<int> > PQ;
int main()
{
    int N;
    int L;
    int left_tank;
    cin >> N;
    for(int i=0; i<N; i++)
        cin >> stops[i].first >> stops[i].second;
    cin >> L >> left_tank;
    for(int i=0; i<N; i++)
        stops[i].first = L - stops[i].first;
    sort(stops, stops+N);
    int now_pos = 0;
    int last_stop = 0; //指向上一个还没有纳入机会集合的第一个加油站, 初始为0
    int now_stop; //指向前进一次后最后一个能纳入机会集合的加油站
    int ans = 0;
    while(now_pos + left_tank < L) //如果>=,则当前油量足够到达终点
    {
        now_pos += left_tank;
        now_stop = upper_bound(stops, stops+N, make_pair(now_pos, inf)) - stops - 1;
        for(int i=last_stop; i<=now_stop; i++)
            PQ.push(stops[i].second);
        last_stop = now_stop + 1;
        if(PQ.empty())
        {
            cout << -1 << endl;
            return 0;
        }
        left_tank = PQ.top();
        ans++;
        PQ.pop();
    }
    cout << ans << endl;
    return 0;
}

Knowledge Point

  • upper_boundAnd pairthe combined use of: (1) press the firstsort, then secondsort, so here looking for a last chance gas station can be incorporated into the set, will secondset up in order infto ensure the return of the elements firstmust be greater than now_pos. (2) make_pairdirect generation of a pair.

Guess you like

Origin blog.csdn.net/Wangpeiyi9979/article/details/93605378