NCPC2017A(dp+单调队列)

题目链接:http://codeforces.com/group/Us3rfLfgWv/contest/101572

这题其实出得蛮好的。。

先设等咖啡的时间和速度为t1、v1 ,喝咖啡的时间为t2、v2

显然有一个N^2的做法。。

一开始想令d[i]为到第i个咖啡厅的最短时间的。。可是精度上比较可能会出现问题。。必须用longdouble才行。。而tsy说这个其实时间最短意味着以v2速度走的路程最长啊。。

所以我们把d[i]改成到第i个咖啡厅时的以v2速度走的最长路程

然后需要分类讨论一下。。


关键就在j的决策了。。N^2肯定tle。。

然后这题给人感觉一种明显的单调决策性。。对当前的i来说,如果前面的j还不如后面的j优,那么以后这个j肯定就不是最优决策了。。

而事实上这个函数有分段性,所以到后面可能这个j会变成最优方案,可以设想一下等咖啡的时间远大于喝咖啡的时间就清楚了。。

所以直接用单调队列来维护决策不太好。。不过如果将这些j分段存在几个单调队列中,那么此时的单调队列就有严格的单调性了。。因为同一个分段上j的增长速度相同。。

所以可以考虑用3个单调队列维护决策方案。。事实上对第三段来说由于它不存在出队的情况。。所以可以直接维护最大值。。。





/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 500005
#define nm 5000005
#define pi 3.1415926535897931
const ll inf=1000000007;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

ll v1,v2,t2,t1,p,a[NM],d[NM];
int c[NM],n,tot,b[NM],ans;
int _q[NM],_qh,_qt,_x,_y,_t,q[NM],qh,qt;


int main(){
    //freopen("data.in","r",stdin);
    p=read();v1=read();v2=read();t1=read();t2=read();
    n=read();inc(i,1,n)a[i]=read();
    if(n==0)return 0*printf("0\n");
    qh=_qh=1;_x=_y=1;a[++n]=p;
    inc(i,1,n){
	while(_x<i&&a[_x]+v1*t1+v2*t2<=a[i]){
	    if(d[_t]<=d[_x])_t=_x;
	    _x++;
	}
	//while(_qh<_qt&&d[_q[_qh]]-a[_q[_qh]]<d[_q[_qh+1]]-a[_q[_qh]])_qh++;
	while(_y<i&&a[_y]+v1*t1<=a[i]){
	    while(_qh<=_qt&&d[_q[_qt]]-a[_q[_qt]]<d[_y]-a[_y])qt--;
	    _q[++_qt]=_y++;
	}
	while(_qh<=_qt&&a[_q[_qh]]+v1*t1+v2*t2<=a[i])_qh++;
	while(qh<=qt&&a[q[qh]]+v1*t1<=a[i])qh++;
	//while(qh<qt&&d[q[qh]]<d[q[qh+1]])qh++;
	if(_t)d[i]=d[_t]+v2*t2,c[i]=_t;
	if(_qh<=_qt&&d[i]<d[_q[_qh]]+a[i]-v1*t1-a[_q[_qh]])
	    d[i]=d[_q[_qh]]+a[i]-v1*t1-a[_q[_qh]],c[i]=_q[_qh];
	if(qh<=qt&&d[i]<d[q[qh]])d[i]=d[q[qh]],c[i]=q[qh];
	while(qh<=qt&&d[q[qt]]<d[i])qt--;
	q[++qt]=i;
    }
    //inc(i,1,n)printf("%d ",d[i]);putchar('\n');
    //inc(i,1,n)printf("%d ",c[i]);putchar('\n');
    //printf("%d\n",ans);
    for(int i=n;i;i=c[i])b[++tot]=i;
    printf("%d\n",tot-1);
    dec(i,tot,2)printf("%d ",b[i]-1);putchar('\n');
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/80536212
今日推荐