Submission #15775

# Submission time Handle Problem Language Result Execution time Memory
15775 2015-07-22T11:16:45 Z comet 달리는 게임 (kriii3_E) C++
70 / 70
515 ms 33104 KB
#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long ll;
ll s[1000010],d[1000010],d2[100],p[1000010],p2[1000010];
deque<ll> a,b,c;
int cnt=0;
void push(ll n,ll m){
	int sz=a.size();
	//cout<<"push"<<endl;
	//cout<<"n="<<n<<" "<<"m="<<m<<endl;
	//for(int i=0;i<a.size();i++)cout<<c[i]<<" ";
	//cout<<endl;
	while(  sz>1  &&  (b[0]-b[1])*(a[0]-n)  < (m-b[0])*(a[1]-a[0])  ){
		//cout<<(double)(b[1]-b[0])/(a[0]-a[1])<<" "<<(double)(m-b[0])/(a[0]-n)<<endl;
		sz--;
		a.pop_front();
		b.pop_front();
		c.pop_front();
	}
	//cout<<"push end"<<endl;
	a.push_front(n);
	b.push_front(m);
	c.push_front(cnt++);
}
ll query(ll x,ll y){
	if(a.size()==1)return a[0]*x+b[0]+y;
	int lo=0,hi=a.size(),mid;
	while(lo+1<hi){
		mid=(lo+hi)/2;
		if(b[mid]-b[mid-1]<x*(a[mid-1]-a[mid])){
			hi=mid;
		}
		else{
			lo=mid;
		}
	}
	//for(int i=0;i<a.size();i++)cout<<a[i]*x+b[i]+y<<",("<<c[i]<<") ";
	//cout<<"lo="<<lo<<endl;
	return a[lo]*x+b[lo]+y;
}
int main(){
	ll n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];
		p[i]=p[i-1]+s[i];
		p2[i]=p2[i-1]+i*s[i];
	}
	push(-1,-p2[1]+p[1]);
	for(ll i=1;i<=n;i++){
		d[i]=p2[i];
		d2[i]=p2[i];
		//for(ll j=0;j<i;j++){
		//	d2[i]=max(d2[i],  -(j+1)*p[i]  +d2[j]-p2[j+1]+(j+1)*p[j+1]  +p2[i]  );
		//}
		//cout<<d[i]<<" "<<d2[i]<<endl;
		d[i]=max(d[i],query(p[i],p2[i]));
		push(-(i+1),d[i]-p2[i+1]+(i+1)*p[i+1]);
		//cout<<d[i]<<" "<<d2[i]<<endl;
		//cout<<"i="<<i<<endl;
	}
	cout<<d[n];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 32976 KB Output is correct
2 Correct 0 ms 32976 KB Output is correct
3 Correct 0 ms 32976 KB Output is correct
4 Correct 0 ms 32976 KB Output is correct
5 Correct 0 ms 32976 KB Output is correct
6 Correct 0 ms 32976 KB Output is correct
7 Correct 0 ms 32976 KB Output is correct
8 Correct 0 ms 32976 KB Output is correct
9 Correct 0 ms 32976 KB Output is correct
10 Correct 0 ms 32976 KB Output is correct
11 Correct 0 ms 32976 KB Output is correct
12 Correct 0 ms 32976 KB Output is correct
13 Correct 1 ms 32976 KB Output is correct
14 Correct 0 ms 32976 KB Output is correct
15 Correct 0 ms 32976 KB Output is correct
16 Correct 0 ms 32976 KB Output is correct
17 Correct 0 ms 32976 KB Output is correct
18 Correct 0 ms 32976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 32976 KB Output is correct
2 Correct 20 ms 32976 KB Output is correct
3 Correct 27 ms 32976 KB Output is correct
4 Correct 50 ms 32976 KB Output is correct
5 Correct 49 ms 33104 KB Output is correct
6 Correct 52 ms 32976 KB Output is correct
7 Correct 249 ms 32976 KB Output is correct
8 Correct 509 ms 32976 KB Output is correct
9 Correct 515 ms 32976 KB Output is correct