斜率优化 WA 11 pts 玄关求条
查看原帖
斜率优化 WA 11 pts 玄关求条
814145
XXh0919楼主2024/12/14 17:18

hack 过了,就是过不了测试点,想尽一切办法都不如写 n2n^2 的。

#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mkp(a,b) make_pair((a),(b)) 
#define IOS cin.tie(0)->sync_with_stdio(0)

#define Y(i) dp[i]+s[i]
#define K(i) x[i]
#define X(i) sp[i]
#define B(i) dp[i]+s[i]-c[i]-x[i]*sp[i]

using namespace std;
const int N=1e6+15;

int n;
double x[N],p[N],c[N],sp[N];
double s[N];
double dp[N];
int q[N],hh,tt;

long double slope(int i,int j){
	return 1.0*(Y(i)-Y(j))/(X(i)-X(j));
}

signed main(){
	IOS;
	cin>>n;
	rep(i,1,n){
		cin>>x[i]>>p[i]>>c[i];
		s[i]=s[i-1]+p[i]*x[i];
		sp[i]=sp[i-1]+p[i];
	}
	hh=1,tt=1;
	rep(i,1,n){
		while(hh<tt&&slope(q[hh+1],q[hh])<=1.0*x[i])++hh;
		int j=q[hh];
		dp[i]=dp[j]+x[i]*(sp[i]-sp[j])-(s[i]-s[j])+c[i];
		while(hh<tt&&slope(i,q[tt])<=slope(q[tt],q[tt-1]))--tt;
		q[++tt]=i;
	}
	int p2=n;
	double ans=dp[n];
	per(i,n,1){
		ans=min(ans,dp[i]);
		if(p[i])break;
	}
	cout<<ans<<endl;
	return 0;
}
2024/12/14 17:18
加载中...