码迷,mamicode.com
首页 > 其他好文 > 详细

SFSA

时间:2019-09-28 23:24:47      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:efi   head   algorithm   can   scan   stack   csharp   str   name   

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
const int MAXN=1e4+15;
#define ll long long
using namespace std;
ll f[MAXN];
int sum[MAXN],sumtimee[MAXN];
int num[MAXN];
int n,m;
int s;
int tail,head,que[MAXN];

int i;
int main(){
	scanf("%d%d",&n,&s);
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&sumtimee[i],&num[i]);
		sum[i]=sum[i-1]+num[i];sumtimee[i]=sumtimee[i-1]+sumtimee[i];
	}
	head=tail=1;
	que[tail++]=0;
	for(i=1;i<=n;i++)
	{	
		while(head+1<tail&&(f[que[head+1]]-f[que[head]])<=(s+sumtimee[i])*(sum[que[head+1]]-sum[que[head]]))
		head++;
		int k=que[head];
		f[i]=f[k]-(s+sumtimee[i])*sum[k]+sumtimee[i]*sum[i]+s*sum[n];
	
		while(head+1<tail&&(f[i]-f[que[tail-1]])*(num[que[tail-1]]-num[que[tail-2]])<=(num[i]-num[que[tail-1]])*(f[que[tail-1]]-f[que[tail-2]]))
		tail--;
		que[tail++]=i;
	}
	printf("%lld",f[n]);
	return 0;
}

  

SFSA

标签:efi   head   algorithm   can   scan   stack   csharp   str   name   

原文地址:https://www.cnblogs.com/Lamboofhome/p/11605196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!