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 ...
随机推荐
- Android之开启手机系统自带铃声
/** * 开启手机系统自带铃声 */ private void startAlarm() { mMediaPlayer = MediaPlayer.create(this, getSystemDef ...
- stringbuffer 和 stringbuilder的区别
1. stringbuffer 和 stringbuilder的区别 StringBuffer是线程安全的, 这个类里的所有方法是同步的.这个反过来就会对程序的性能有一定的影响.StringBuild ...
- java面试总结-(hibernate ibatis struts2 spring)
说说Hibernate对象的三种状态 Hibernate对象有三种状态,分别是:临时态(Transient). 持久态(Persistent).游离态(Detached). 临时状态:是指从对象通过n ...
- Excel Sheet Column Title & Excel Sheet Column Number
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...
- Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
- HDU 5734 Acperience (公式推导) 2016杭电多校联合第二场
题目:传送门. #include <iostream> #include <algorithm> #include <cstdio> #include <cs ...
- std::map常用方法
map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair ...
- [MAC ] Mac-OSX下安装Git
转载自 : http://www.cnblogs.com/shanyou/archive/2011/01/30/1948088.html Mac-OSX下安装Git是一件很简单的事,我们可以下载一个安 ...