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;
}

  

时间: 2024-10-14 00:56:06

BZOJ3024 : [Balkan2012]balls的相关文章

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

POJ3687Labeling Balls

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14278   Accepted: 4162 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

codeforces 653A A. Bear and Three Balls(水题)

题目链接: A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each

[2016-02-08][UVA][679][Dropping Balls]

UVA - 679 Dropping Balls Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped f

poj 3687 Labeling Balls(拓补排序)

Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share the same label. The labeling satisfies several constrains like "The ball labeled with a is lighter

杭电3635-Dragon Balls(并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4582    Accepted Submission(s): 1747 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

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/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305 Problem Description Bob has N balls and A b

Hdoj 5194 DZY Loves Balls 【打表】+【STL】

DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 394    Accepted Submission(s): 221 Problem Description There are n black balls and m white balls in the big box. Now, DZY starts

UVa679:Dropping Balls

Dropping Balls A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down, either follows the path of the left subtree, o