BZOJ3024 : [Balkan2012]balls
问题1:
ans=max(sum[n]-(sum[i]-sum[j-1])+a[i]*(i-j+1))
=max(sum[n]-sum[i]+sum[j-1]+a[i]*(i+1)-a[i]*j)
=sum[n]-sum[i]+a[i]*(i+1)+f[i]
f[i]=max(-j*a[i]+sum[j-1]),j<i
由于j递增,-j递减,所以从右往左建立凸壳,查询时在凸壳上二分查找即可,时间复杂度$O(n\log n)$。
问题2:
将序列翻转后即化为问题1。
#include<cstdio>
#define N 300010
typedef long long ll;
int n,i,j,a[N],q[N],t;ll sum[N],b[N],f[N],ans;
inline void read(int&a){
char c;bool f=0;a=0;
while(!((((c=getchar())>='0')&&(c<='9'))||(c=='-')));
if(c!='-')a=c-'0';else f=1;
while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';
if(f)a=-a;
}
inline double pos(int x,int y){return (double)(b[x]-b[y])/(double)(x-y);}
inline ll ask(int x){
int l=1,r=t-1,fin=t,mid;
while(l<=r){
mid=(l+r)>>1;
if((double)x>pos(q[mid],q[mid+1]))r=(fin=mid)-1;else l=mid+1;
}
return b[q[fin]]-(ll)q[fin]*x;
}
inline void up(ll x){if(ans<x)ans=x;}
void work(){
for(ans=-1LL<<60,i=1;i<=n;i++)sum[i]=sum[i-1]+a[i],b[i]=sum[i-1];
for(t=0,i=1;i<=n;q[++t]=i++){
if(i>1)up(sum[n]-sum[i]+(ll)a[i]*(i+1)+ask(a[i]));
while(t>1&&pos(i,q[t])>pos(q[t],q[t-1]))t--;
}
printf("%lld\n",ans);
}
int main(){
for(read(n),i=1;i<=n;i++)read(a[i]);
work();
for(i=1,j=n;i<j;i++,j--)t=a[i],a[i]=a[j],a[j]=t;
work();
return 0;
}
BZOJ3024 : [Balkan2012]balls的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- 13 Balls Problem
今天讨论的是称球问题. No.3 13 balls problem You are given 13 balls. The odd ball may be either heavier or ligh ...
- Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls 总时间限制: 1000ms 内存限制: 262144kB 描述 Wenwen has a magical ball. When put on an infin ...
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...
- HDU 5810 Balls and Boxes(盒子与球)
Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
随机推荐
- Unity 3D 粒子系统的一点经验
http://hunterwang.diandian.com/post/2012-10-21/40041523890 最近做东西需要增加效果,简单的运用了一下粒子效果,真心感觉比较难调整好效果.同时也 ...
- 如何用rake tasks 生成migration对应的sql
how-to-use-rake-tasks-to-generate-migration-sql Rakefile文件里有load_tasks的方法 http://api.rubyonrails.org ...
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- RemObjects SDK Source For Delphi XE7
原文:http://blog.csdn.net/tht2009/article/details/39545545 1.目前官网最新版本是RemObjects SDK for Delphi and al ...
- django-cms 代码研究(五)深入(代码结构)
前言: 前戏已经做得比较充分了,下面我们开始步入正题. 代码结构: cms |--admin (猜测是admin界面的二次开发和改良) |--cache (猜测是缓存机制的处理) |--extensi ...
- How to: Set up Openswan L2TP VPN Server on CentOS 6
Have you ever wanted to set up your own VPN server? By following the steps below, you can set up you ...
- Git SSH Key 生成步骤
it是分布式的代码管理工具,远程的代码管理是基于ssh的,所以要使用远程的git则需要ssh的配置. github的ssh配置如下: 一 . 设置git的user name和email: $ git ...
- 【SpringMVC】SpringMVC系列3之@PathVariable映射URL占位符参数
3.@PathVariable映射URL占位符参数 3.1.概述 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义. ...
- Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- recv和send函数
转自 http://www.cnblogs.com/blankqdb/archive/2012/08/30/2663859.html 1. send解析 sockfd:指定发送端套接字描述符. bu ...