Submission #477269

# Submission time Handle Problem Language Result Execution time Memory
477269 2021-10-01T14:23:00 Z AkiYuu Building Bridges (CEOI17_building) C++17
30 / 100
3000 ms 2344 KB
#include <bits/stdc++.h>
#define task "GROUP"
#define ll long long
#define ld long double
#define pb(u) emplace_back(u)
#define ffw(i,a,b) for (ll i = a; i <= b; i++)
#define fbw(i,b,a) for (ll i = b; i >= a; i--)
#define adj(v,adj,u) for (auto v : adj[u])
#define rep(i,a) for (auto i : a)
#define reset(a) memset(a, 0, sizeof(a))
#define sz(a) a.size()
#define all(a) a.begin(),a.end()

using namespace std;
const int mxn = 1e6 + 5;
const ll inf = 1e18 + 7;
typedef pair<ll, ll> ii;

void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
//	freopen(task".inp", "r", stdin);
//	freopen(task".out", "w", stdout);
}

ll n, w[mxn], h[mxn], dp[mxn];
bool sol2;
ll tot = 0;

void subtask1(){
    ll res;
    ffw(i,2,n){
        dp[i] = inf; res = 0;
        fbw(j,i-1,1){
            ll cur = res + dp[j] + (h[i] - h[j]) * (h[i] - h[j]);
            if ( cur < dp[i]) dp[i] = cur;
            res += w[j];
        }
    }
    cout << dp[n] << '\n';
}

ll trans(int i){
    return (h[1]-h[i]) * (h[1]-h[i]) + (h[i]-h[n]) * (h[i]-h[n]) + tot - w[1] - w[i] - w[n];
}

void subtask2(){
    map<ll, set<ll> > check;
    ll res = tot - w[1] - w[n] + (h[n] - h[1]) * (h[n] - h[1]);
    ffw(i,2,n-1){
        ffw(j,-20,20){
            if (check[j].size() != 0){
                auto point = check[j].lower_bound((h[i] + h[1])/2);
                res = min(res, trans(i));
                if ( point != check[j].end())
                    res = min(res,(h[1]-(*point)) * (h[1]-(*point)) + ((*point)-h[i]) * ((*point)-h[i]) +  (h[n]-h[i])*(h[n]-h[i]) + tot - w[1] - w[i] - w[n] - j);
                if ( point != check[j].begin()){
                    point = prev(point);
                    res = min(res,(h[1]-(*point)) * (h[1]-(*point)) + ((*point)-h[i]) * ((*point)-h[i]) + (h[n]-h[i]) * (h[n]-h[i]) + tot - w[1] - w[i]- w[n] - j);
                }
            }
        } check[w[i]].insert(h[i]);

    }
    cout << res << '\n';
}

void solve(){
    cin >> n;
    ffw(i,1,n) cin >> h[i];
    sol2 = true;
    ffw(i,1,n) {
        cin >> w[i];
        if ( abs(w[i]) > 2) sol2 = false;
        tot += w[i];
    }
    if (!sol2) subtask1();
    else
        subtask2();
}

int main(){
	fastio();
	solve();
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3065 ms 2344 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Execution timed out 3065 ms 2344 KB Time limit exceeded
7 Halted 0 ms 0 KB -