DP-- travel monotonous queue optimization problem

Travel questions

John intends to drive a car to travel around a ring road.

A total of n stations on the road, every station has a number of liters of gasoline (fuel and some stations may be zero), per liter of oil can make cars with one thousand meters.

John must proceed from a station, has been in a clockwise (or counterclockwise) direction traveled all the stations and back to the starting point.

In the beginning of time, car fuel is zero, John each station, the station put all the oil are put on (starting point is also true), the process of moving without oil situation can not occur.

Task: to determine whether each station as a starting point for success by traveling the week.

The input format
of the first line is an integer n, the number of stations on the loop highway;

Next n lines of two integers pi, di, respectively, represent the i-th station oil deposit the i-th station and the distance to the next station.

Output format
output a total of n lines, if starting from the i-th station, has been in a clockwise (or counterclockwise) direction of travel, can successfully travel around the circle, the TAK in the i-th row output, otherwise the output NIE.

data range
3 n 1 0 6 3≤n≤10^6 ,
0 p i 2 × 1 0 9 0≤p_i≤2×10^9 ,
0 < d i 2 × 1 0 9 0<d_i≤2×10^9
Input Sample:
. 5
. 3. 1
. 1 2
. 5 2
0. 1
. 5. 4
Output Sample:
TAK
NIE
TAK
NIE
TAK

answer:

We first analyze the meaning of the questions, he said that any departure from a gas station, then clockwise or counterclockwise perimeter can come back on the line. We know that any problem can be converted to ring a linear problem linear DP learning. N only need to add a length in the back on it, then we can know is that he demanded a fixed length of the interval n of most value problem. If we prefix minimum interval n and greater than 0, then we can do on behalf of. So, when we think of the fixed length of the queue monotonous. This question is monotonous queue so we maintain a clockwise and a counter on it, when you need maintenance attention clockwise when we need to update our team tail, so while on the front, and this time we are endpoint must be less than in, it does not contain the current point, and we counterclockwise is included, and we do not need to update the tail.

#include <bits/stdc++.h>
#define int long long
#define lowbit(x) (x&(-x))
using namespace std;
const int N=1e6+6;
int p[N],d[N],s[N*2+5],q[N*2],ans[N*2];
signed main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++) cin>>p[i]>>d[i];
    for(int i=1;i<=n;i++) s[i]=s[i+n]=p[i]-d[i];
    for(int i=1;i<=n*2;i++) s[i]+=s[i-1];
    int hh=0,tt=-1;
    for(int i=n*2;i;i--){
        if(hh<=tt&&q[hh]>=i+n) hh++;
        while(hh<=tt&&s[q[tt]]>=s[i]) tt--;
        q[++tt]=i;
        if(i<=n){
            if(s[q[hh]]>=s[i-1]) ans[i]=1;
        }
    }
    hh=0,tt=-1;
    d[0]=d[n];
    for(int i=1;i<=n;i++) s[i]=s[i+n]=p[i]-d[i-1];
    for(int i=1;i<=n*2;i++) s[i]+=s[i-1];
    for(int i=1;i<=2*n;i++){
        if(hh<=tt&&q[hh]<i-n) hh++;
        if(i>n){
            if(s[q[hh]]<=s[i]) ans[i-n]=1;
        }
        while(hh<=tt&&s[q[tt]]<=s[i]) tt--;
        q[++tt]=i;
    }
    for(int i=1;i<=n;i++)
    if(ans[i]) puts("TAK");
    else puts("NIE");
}

Published 92 original articles · won praise 6 · views 1150

Guess you like

Origin blog.csdn.net/weixin_42979819/article/details/104012454