LA 3983 Robotruck

  白书原题,简单的单调队列。

#include <cstdio>
#include <algorithm> 
#include <iostream>
#define MAXN 100010
using namespace std;
int t,T,c,n,x[MAXN],y[MAXN],d[MAXN],f[MAXN],dis[MAXN],q[MAXN],w[MAXN]; 
int main()
{
	cin.sync_with_stdio(false);
	cin>>T;
	while(T--)
	{
		cin>>c>>n;
		for(int i = 1;i <= n;i++)
		{
			cin>>x[i]>>y[i]>>w[i];
			d[i] = abs(x[i]) + abs(y[i]);
			w[i] += w[i-1];
			dis[i] = dis[i-1] + abs(x[i] - x[i-1]) + abs(y[i] - y[i-1]);	
		}	
		int s = t = 1;
		for(int i = 1;i <= n;i++)
		{
			while(s <= t && w[i] - w[q[s]] > c) s++;
			f[i] = f[q[s]] - dis[q[s]+1] + d[q[s]+1] + dis[i] + d[i];
			while(s <= t && f[i] - dis[i+1] + d[i+1] <= f[q[t]] - dis[q[t]+1] + d[q[t]+1]) t--;
			q[++t] = i;
		}
		cout<<f[n]<<endl;
		if(T) cout<<endl;
	}
 } 


你可能感兴趣的:(dp,ACM,水题,单调队列)