제출 #43068

#제출 시각아이디문제언어결과실행 시간메모리
43068top34051Aliens (IOI16_aliens)C++14
100 / 100
233 ms40272 KiB
#include "aliens.h"
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<ll,ll>
#define X first
#define Y second
const int maxn = 1e5 + 5;

struct line {
    ll m,c;
    int cnt;
    line(ll _m = 0, ll _c = 0, int _cnt = 0) {
        m = _m; c = _c; cnt = _cnt;
    }
};

int n,m,k;
pii p[maxn];
int r[maxn], c[maxn];
ll dp[maxn];
int cnt[maxn];
line cht[maxn];

bool cmp(pii x, pii y) {
    if(x.X!=y.X) return x.X<y.X;
    return x.Y>y.Y;
}

ll p2(ll x) { return x*x; }

double cut(int x, int y) {
    if(x==0 || y==0) return -2e16;
    return (double)(cht[y].c-cht[x].c)/(cht[x].m-cht[y].m);
}

long long func(line t, ll x) {
    return t.m*x + t.c;
}

int solve(ll cost) {
    int st = 1, ft = 0;
    for(int x=1;x<=n;x++) {
        cht[++ft] = line(-2*r[x], p2(r[x]) + dp[x-1] - p2(max(0,c[x-1]-r[x])), cnt[x-1]+1);
        while(ft-st+1>=3 && cut(ft-1,ft)<cut(ft-2,ft)) cht[ft-1] = cht[ft], ft--;
        while(ft-st+1>=2 && func(cht[st],c[x])>func(cht[st+1],c[x])) st++;
        dp[x] = cht[st].m*c[x] + cht[st].c + p2(c[x]) + cost;
        cnt[x] = cht[st].cnt;
//        printf("dp %d = %lld %d\n",x,dp[x],cnt[x]);
    }
//    printf("cost = %lld : %d  dp = %lld\n",cost,cnt[n],dp[n]);
    return cnt[n];
}

long long take_photos(int N, int M, int K, std::vector<int> R, std::vector<int> C) {
	n = N; m = M; k = K;
	//prep
	for(int i=0;i<n;i++) {
        p[i] = {R[i],C[i]};
        if(p[i].X>p[i].Y) swap(p[i].X,p[i].Y);
	}
	sort(p,p+n,cmp);
    ll cur = -1, sz = 0;
	for(int i=0;i<n;i++) {
        if(p[i].Y<=cur) continue;
        sz++;
        r[sz] = p[i].X; c[sz] = p[i].Y+1;
        cur = p[i].Y;
	}
	n = sz;
	//solve
	ll l = 0, r = 2e16, mid;
	while(l<r) {
        mid = (l+r)/2;
        if(solve(mid)<=k) r = mid;
        else l = mid+1;
	}
	solve(l);
    return dp[n] - l*k;
}
#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...