Submission #393112

# Submission time Handle Problem Language Result Execution time Memory
393112 2021-04-22T18:22:25 Z Tc14 Aliens (IOI16_aliens) C++17
0 / 100
2000 ms 332 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;
#define ve vector
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 10;
const ll LLINF = (ll)1e18 + 10;

ve<pii> P;
ve<ll> L;
int a;

struct segment {
    ll m, b, l, r, k;
};

double intersect(ll m1, ll m2, ll b1, ll b2) {
    if ((b1 - b2) % (m2 - m1) == 0) assert(false);
    return (double) (b1 - b2) / (double) (m2 - m1);
}

ll sq(int x) { return (ll)x*x; }

pair<ll, ll> calc(ll lambda) {

    ve<ll> DP(a), K(a);
    list<segment> H;

    DP[0] = sq(P[0].second - P[0].first + 1) - lambda;
    K[0] = 1;

    for (int i = 1; i < a; i++) {

        ll x = -P[i].first + 1;
        ll b = DP[i - 1] - L[i] + x * x;
        ll m = 2 * x;

        if (H.size() == 0) H.push_back({m, b, -LLINF, LLINF, K[i - 1]});
        else {

            while (intersect(H.back().m, m, H.back().b, b) < (double)H.back().l) {
                H.pop_back();
            }

            double z = intersect(H.back().m, m, H.back().b, b);
            ll l = (ll)ceil(z);
            H.back().r = (ll)floor(z);

            H.push_back({m, b, l, LLINF, K[i - 1]});
        }

        while (H.begin()->r < P[i].second) {
            H.pop_front();
        }
        H.begin()->l = -LLINF;

        ll q = H.begin()->m * P[i].second + H.begin()->b;
        ll v = q + sq(P[i].second) - lambda;

        DP[i] = sq(P[i].second - P[0].first + 1) - lambda;
        K[i] = 1;

        if (DP[i] > v) {
            DP[i] = v;
            K[i] = H.begin()->k + 1;
        }
    }

    return {DP[a - 1], K[a - 1]};
}

ll take_photos(int n, int m, int k, ve<int> R, ve<int> C) {

    map<int, int> M;

    for (int i = 0; i < n; i++) {
        int r = R[i];
        int c = C[i];
        if (c < r) swap(r, c);
        if (M.find(r) == M.end()) M[r] = c;
        else M[r] = max(M[r], c);
    }

    int cOld = -1;
    a = 0;
    for (pii p : M) {
        int r, c;
        tie(r, c) = p;
        if (c > cOld) {
            cOld = c;
            P.push_back({r, c});
            a++;
        }
    }

    L = ve<ll>(a);
    for (int i = 1; i < a; i++) {
        if (P[i - 1].second >= P[i].first) {
            L[i] = sq(P[i - 1].second - P[i].first + 1);
        }
    }

    ll lo = 0;
    ll hi = (ll)1e9;

    while (true) {

        ll m = (lo + hi) / 2;

        ll v, t;
        tie(v, t) = calc(m);

        if (t > min(a, k)) lo = m + 1;
        else if (t < min(a, k)) hi = m - 1;
        else return v + t * m;

    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 0 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 276 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Runtime error 1 ms 332 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 1
2 Execution timed out 2061 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 0 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 276 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Runtime error 1 ms 332 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 0 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 276 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Runtime error 1 ms 332 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 0 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 276 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Runtime error 1 ms 332 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Correct answer: answer = 4
2 Correct 0 ms 204 KB Correct answer: answer = 4
3 Correct 1 ms 276 KB Correct answer: answer = 4
4 Correct 1 ms 204 KB Correct answer: answer = 12
5 Runtime error 1 ms 332 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -