Submission #241565

# Submission time Handle Problem Language Result Execution time Memory
241565 2020-06-24T13:03:21 Z kimbj0709 Building Bridges (CEOI17_building) C++14
100 / 100
105 ms 11128 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 100050
vector<int> vect3;
struct line{
    int m,c;
    int operator()(int x){
        return m*x+c;
    }
}a[maxn*4];
void insert(int node,int start,int end,line l){
    if(start==end){
        if(a[node](vect3[start])<l(vect3[start])){
            a[node] = l;
        }
        return;
    }
    if(a[node].m>l.m){
        swap(a[node],l);
    }
    int mid = (start+end)/2;
    if(a[node](vect3[mid])<l(vect3[mid])){
        swap(a[node],l);
        insert(node*2+1,start,mid,l);
    }
    else{
        insert(node*2+2,mid+1,end,l);
    }
}
int query(int node,int start,int end,int pos){
    if(start==end){
        return a[node](vect3[pos]);
    }
    int mid = (start+end)/2;
    if(pos<=mid){
        return max(a[node](vect3[pos]),query(node*2+1,start,mid,pos));
    }
    else{
        return max(a[node](vect3[pos]),query(node*2+2,mid+1,end,pos));
    }
}
int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int no_of_input;
    vector<int> vect1;
    vector<int> vect2;
    vector<int> dp(maxn);
    int input;
    cin >> no_of_input;
    for(int i=0;i<no_of_input;i++){
        cin >> input;
        vect1.push_back(input);
    }
    for(int i=0;i<no_of_input;i++){
        cin >> input;
        vect2.push_back(input);
    }
    vect3 = vect1;
    sort(vect3.begin(),vect3.end());
    for(int i=0;i<maxn*4;i++){
        a[i] = {0,-LLONG_MAX};
    }
    insert(0,0,vect3.size()-1,{2*vect1[0],-(vect1[0]*vect1[0]-vect2[0])});
    for(int i=1;i<no_of_input;i++){
        int pos = lower_bound(vect3.begin(),vect3.end(),vect1[i])-vect3.begin();
        int mini = query(0,0,vect3.size()-1,pos);
        dp[i] = -mini+vect1[i]*vect1[i]-vect2[i];
        insert(0,0,vect3.size()-1,{2*vect1[i],-(dp[i]+vect1[i]*vect1[i])});
    }
    int sum = 0;
    for(auto k:vect2){
        sum += k;
    }
    cout << dp[no_of_input-1]+sum;

}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 10 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 10096 KB Output is correct
2 Correct 105 ms 10224 KB Output is correct
3 Correct 94 ms 10224 KB Output is correct
4 Correct 86 ms 10224 KB Output is correct
5 Correct 52 ms 10224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 10 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 98 ms 10096 KB Output is correct
7 Correct 105 ms 10224 KB Output is correct
8 Correct 94 ms 10224 KB Output is correct
9 Correct 86 ms 10224 KB Output is correct
10 Correct 52 ms 10224 KB Output is correct
11 Correct 92 ms 11128 KB Output is correct
12 Correct 97 ms 10872 KB Output is correct
13 Correct 97 ms 10992 KB Output is correct
14 Correct 92 ms 10992 KB Output is correct
15 Correct 54 ms 10744 KB Output is correct
16 Correct 52 ms 10872 KB Output is correct
17 Correct 42 ms 10992 KB Output is correct
18 Correct 42 ms 11000 KB Output is correct