Submission #837538

#TimeUsernameProblemLanguageResultExecution timeMemory
837538ballbattleAliens (IOI16_aliens)C++14
4 / 100
34 ms240 KiB
#include "aliens.h"

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

typedef int ll;
typedef long double f;

typedef pair<ll,ll> pll;
typedef pair<f,ll> pfl;

typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<pfl> vp;


#define rep(a,b,c) for(ll a = b; a < c; a++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define sz(a) a.size()

pfl operator+ (pfl a, pfl b) {return mp(a.first+b.first,a.second+b.second);}

pfl _dp(f lambda);

ll n;

vpll p;
vll h;

vp dp;

long long take_photos(ll N, ll M, ll k, vll R, vll C) {
    p.clear();
    rep(i,0,N) {if (R[i] > C[i]) {swap(R[i],C[i]);}} // Flip over diagonal
    rep(i,0,N) {p.pb(mp(C[i],-R[i]));}
    sort(all(p)); // Sort in a way that makes worse points come before better points
    rep(i,0,N) {
        C[i] = p[i].first;
        R[i] = -p[i].second;
    }
    /*cout << "C[i] R[i]" << endl;
    rep(i,0,N) {cout << C[i] << " " << R[i] << endl;}*/
    // C[i] is in increasing order
    // R[i] is in decreasing order
    // The first point is the optimal

    p.clear();
    p.pb(mp(-1,-1)); // thing....
    rep(i,0,N) {

        while ((C[i] >= p[sz(p)-1].first) && (R[i] <= p[sz(p)-1].second)) { // Prune bad points
            p.pop_back();
        }
        p.pb(mp(C[i],R[i])); // Add points in (x,-y) format to p
    }
    
    /*cout << "points:" << endl;
    rep(i,0,sz(p)) {cout << p[i].first << " " << p[i].second << endl;}*/

    n = sz(p);
    h.resize(n,0);
    rep(i,0,n-1) {h[i] = p[i].first - p[i+1].second;}

    /*cout << "h:" << endl;
    rep(i,0,n) {cout << h[i] << " ";}
    cout << endl;*/

    /*rep(i,0,10) {cout << _dp(f(i)).first << endl;}*/

    ll ans = -1;

    f lo = f(0.0);
    f hi = f(10000000001);
    f mid;
    pfl curr;
    rep(i,0,64) {
        mid = (lo+hi)/f(2);
        curr = _dp(mid);
        //cout << curr.first << " " << curr.second << " " << mid << endl; 
        if (curr.second == min(k,n-1)) {
            ans = round(curr.first - mid*(f(curr.second)));
            break;
        } else if (curr.second > min(k,n-1)) {
            lo = mid;
        } else {
            hi = mid;
        }
    }
    if (ans==-1) {ans = round(curr.first - mid*(f(curr.second)));}

    //cout << n-1 << endl;
    return ans;
}

pfl _dp(f lambda) {
    dp.clear();
    dp.resize(n,mp(f(10000000000000000),0));
    //dp[0] = mp(f( (p[0].first-p[0].second+1)*(p[0].first-p[0].second+1) ),1);
    dp[0] = mp(f(0),0);
    rep(i,0,n) {
        rep(j,0,i) {
            ll x_i = p[i].first;
            ll x_j = p[j].first;
            if (h[j] < 0) {
                dp[i] = min(dp[i], dp[j] + mp(f( (x_i-x_j+h[j]+1)*(x_i-x_j+h[j]+1) ),1) + mp(lambda,0));
            } else {
                dp[i] = min(dp[i], dp[j] + mp(f( (x_i-x_j+h[j]+1)*(x_i-x_j+h[j]+1) - (h[j]+1)*(h[j]+1) ),1) + mp(lambda,0));
            }
        }
    }
    /*cout << "dp: ";
    rep(i,0,n) {cout << dp[i].first - lambda*dp[i].second << " ";}
    cout << endl;*/
    return dp[n-1];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...