답안 #123218

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
123218 2019-06-30T12:32:28 Z davitmarg Building Bridges (CEOI17_building) C++17
100 / 100
130 ms 66540 KB
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <stack>
#include <cassert>
#include <iterator>
#include <bitset>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(),v.end()
using namespace std;

struct line
{
    LL k,b;
    line(LL K=0,LL B=0)
    {
		k=K;
		b=B;
    }
};

LL F(line p,LL x)
{
    return p.k*x+p.b;
}

int n;
LL h[100005],pr[100005],dp[100005];
line t[4*1000006];

void build(int v,int l,int r)
{
    t[v]=line(-mod,-mod*mod);
    int m=(l+r)>>1;
    if(l==r)
		return;
    build(v*2,l,m);
    build(v*2+1,m+1,r);
}

void add(int v,LL l,LL r,line p)
{
    LL m=(l+r)>>1;
    bool L,M;
    L=(F(p,l)>F(t[v],l));
    M=(F(p,m)>F(t[v],m));
    if(M)
        swap(p,t[v]);
    if(l==r)
        return;
    else if(L!=M)
        add(v*2,l,m,p);
	else
        add(v*2+1,m+1,r,p);
}

LL get(int v,LL l,LL r,LL x)
{
    if(l==r)
		return F(t[v],x);
    LL m=(l+r)/2;
    if(x<=m)
        return max(F(t[v],x),get(v*2,l,m,x));
	else
        return max(F(t[v],x),get(v*2+1,m+1,r,x));
}

int main()
{
    cin>>n;
	for(int i=1;i<=n;i++)
        scanf("%lld",h+i);
	for(int i=1;i<=n;i++)
	{
        scanf("%lld",pr+i);
        pr[i]+=pr[i-1];
	}
    build(1,0,1000000);
    add(1,0,1000000,line(2*h[1],pr[1]-h[1]*h[1]));

	for(int i=2;i<=n;i++)
	{
		dp[i]=h[i]*h[i]+pr[i-1]-get(1,0,1000000,h[i]);
        add(1,0,1000000,line(2*h[i],pr[i]-dp[i]-h[i]*h[i]));
	}

    cout<<dp[n]<<endl;

	return 0;
}


/*

6
3 8 7 1 6 6
0 -1 9 1 2 0


*/

Compilation message

building.cpp: In function 'int main()':
building.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",h+i);
         ~~~~~^~~~~~~~~~~~
building.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",pr+i);
         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 62968 KB Output is correct
2 Correct 61 ms 62968 KB Output is correct
3 Correct 61 ms 62968 KB Output is correct
4 Correct 62 ms 62988 KB Output is correct
5 Correct 62 ms 62968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 66296 KB Output is correct
2 Correct 119 ms 66424 KB Output is correct
3 Correct 119 ms 66296 KB Output is correct
4 Correct 117 ms 66200 KB Output is correct
5 Correct 113 ms 66272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 62968 KB Output is correct
2 Correct 61 ms 62968 KB Output is correct
3 Correct 61 ms 62968 KB Output is correct
4 Correct 62 ms 62988 KB Output is correct
5 Correct 62 ms 62968 KB Output is correct
6 Correct 120 ms 66296 KB Output is correct
7 Correct 119 ms 66424 KB Output is correct
8 Correct 119 ms 66296 KB Output is correct
9 Correct 117 ms 66200 KB Output is correct
10 Correct 113 ms 66272 KB Output is correct
11 Correct 130 ms 66460 KB Output is correct
12 Correct 130 ms 66296 KB Output is correct
13 Correct 117 ms 66472 KB Output is correct
14 Correct 127 ms 66424 KB Output is correct
15 Correct 115 ms 66356 KB Output is correct
16 Correct 116 ms 66296 KB Output is correct
17 Correct 102 ms 66424 KB Output is correct
18 Correct 103 ms 66540 KB Output is correct