您的位置:首页 > 其它

【斜率优化】[BZOJ3437] 小P的牧场

2014-12-15 13:06 309 查看
模板题,不用多说,只不过本地测,cin 2.6s,scanf 0.6s过。确实数据大了还是少用cin.
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#define MAXN 1000000
using namespace std;
#define gx(i) (sum[i])
#define gy(i) (d[i] + sumt[i])
typedef long long LL;//!!!!
int a[MAXN +10];
int b[MAXN +10];
int que[MAXN +10];
LL sumt[MAXN +10];//t[i] = b[i] * i;
LL sum[MAXN +10];
LL d[MAXN +10];
LL get_d(LL i,LL j)
{
return d[j] + i * (sum[i] - sum[j]) - (sumt[i] - sumt[j]) + a[i];
}
int main()
{
//freopen("farm.in","r",stdin);
//freopen("farm.out","w",stdout);
LL i,f,r,x1,x2,y1,y2,n;
cin >> n;
for(i = 1;i <= n;i++)
scanf("%d",&a[i]);
for(i = 1;i <= n;i++)
scanf("%d",&b[i]);
for(i = 1;i <= n;i++)
{
sumt[i] = sumt[i - 1] + b[i] * i;
sum[i] = sum[i - 1] + b[i];
}
f = r = que[0] = 0;
for(i = 1;i <= n;i++)
{
LL tmp = i - 1;
while(f < r)
{
x1 = gx(que[r]) - gx(que[r-1]);
y1 = gy(que[r]) - gy(que[r-1]);
x2 = gx(tmp) - gx(que[r]);
y2 = gy(tmp) - gy(que[r]);
if(y1 * x2 >= y2 * x1)
r--;
else break;
}
que[++r] = tmp;
while(f < r && get_d(i,que[f]) > get_d(i,que[f + 1]))
f++;
d[i] = get_d(i,que[f]);
}
cout << d
<< endl;
//printf("%d\n",d
);
}
/*
4
2 4 2 4
3 1 4 2
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: