Submission #1212880

#TimeUsernameProblemLanguageResultExecution timeMemory
1212880Zbyszek99Aliens (IOI16_aliens)C++20
100 / 100
1066 ms47104 KiB
#include "aliens.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

vector<pll> get_real_points(vector<pll> p)
{
    rep(i,siz(p)) p[i].ss *= -1;
    sort(all(p));
    rep(i,siz(p)) p[i].ss *= -1;
    int cur_max = -1e9;
    vector<pll> ans = {{-1,-1}};
    forall(it,p)
    {
        if(it.ss > cur_max)
        {
            ans.pb(it);
            cur_max = it.ss;
        }
    }
    return ans;
}

pll dp[100001];

struct line
{
    ll a = 0,b = 0;
    int k =-1,last_vis = -1;
    ll operator()(ll x)
    {
        return a + b*x;
    }
};

const int tree_siz = 1024*2048-1;
line nodes[tree_siz+1];
int cur_iter = 0;

void add_line(int akt, int l, int r, line l2)
{
    if(nodes[akt].last_vis < cur_iter)
    {
        nodes[akt] = l2;
        return;
    }
    ll x = (l+r)/2;
    if(l2(x) < nodes[akt](x) || (l2(x) == nodes[akt](x) && l2.k < nodes[akt].k)) swap(l2,nodes[akt]);
    if(l == r) return;
    if(l2.b < nodes[akt].b)
    {   
        add_line(akt*2+1,(l+r)/2+1,r,l2);
    }
    else
    {
        add_line(akt*2,l,(l+r)/2,l2);
    }
}

line get_best(int akt, int l, int r, ll x)
{
    line best = nodes[akt];
    if(l == r) return best;
    line best2;
    if(x <= (l+r)/2)
    {
        best2 = get_best(akt*2,l,(l+r)/2,x);
    }
    else
    {
        best2 = get_best(akt*2+1,(l+r)/2+1,r,x);
    }
    if(best.last_vis != cur_iter) return best2;
    if(best2.last_vis != cur_iter) return best;
    if(best(x) == best2(x))
    {
        if(best.k < best2.k) return best;
        return best2;
    }
    if(best(x) < best2(x)) return best;
    return best2;
}

pll calc(int n, vector<pll>& points, ll c)
{
    cur_iter++;
    rep2(i,1,n)
    {
        dp[i] = {(points[i].ss - points[1].ff+1) * (points[i].ss - points[1].ff+1),1};
        if(i != 1)
        {
            line l = get_best(1,0,tree_siz/2,points[i].ss);
            dp[i] = min(dp[i],{l(points[i].ss) + points[i].ss * points[i].ss + 2*points[i].ss+1,l.k+1});
        }
        dp[i].ff += c;
        if(i != n)
        {
            line new_line;
            new_line.a = dp[i].ff-2*points[i+1].ff + points[i+1].ff * points[i+1].ff - max(0LL,(points[i].ss - points[i+1].ff+1)) * max(0LL,points[i].ss - points[i+1].ff+1);
            new_line.b = -2*points[i+1].ff;
            new_line.k = dp[i].ss;
            new_line.last_vis = cur_iter;
            add_line(1,0,tree_siz/2,new_line);
        }
    }
    return {dp[n].ff - c * dp[n].ss,dp[n].ss};
}

ll take_photos(int n, int m, int k, vi R, vi C) 
{
    vector<pll> points;
    rep(i,n)
    {
        if(C[i] - R[i] < 0)
        {
            points.pb({C[i],R[i]});
        }
        else
        {
            points.pb({R[i],C[i]});
        }
    }
    points = get_real_points(points);
    n = siz(points)-1;
    ll l = 0;
    ll r = 1e12;
    ll ans = 1e18;
    while(l <= r)
    {
        ll c = (l+r)/2;
        pll dp = calc(n,points,c);
        if(dp.ss <= k)
        {
            ans = dp.ff - c * (ll)(k - dp.ss);
            r = c-1;
        }
        else
        {
            l = c+1;
        }
    }
    return ans;
}

Compilation message (stderr)

aliens.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
aliens_c.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
#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...