제출 #170408

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 4000;
const int MAXK = 4000;

struct Point
{
    ll y, x;
};

struct CHT
{
    struct Line { ll a, b; };
    double cross(const Line &p, const Line &q) { return (double)(q.b-p.b)/(p.a-q.a); }
    vector<Line> S;

    void update(Line p)
    {
        while(S.size()>1 && cross(S[S.size()-1], p)<=cross(S[S.size()-1], S[S.size()-2])) S.pop_back();
        S.push_back(p);
    }

    int pos=0;
    ll query(ll x)
    {
        if(S.size()<=pos) pos=S.size()-1;
        else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++;
        return S[pos].a*x+S[pos].b;
    }

    void init()
    {
        S.clear();
        pos=0;
    }
} cht;

int N, M, K;
Point B[MAXN+10], A[MAXN+10];
ll dp[MAXK+10][MAXN+10];

ll take_photos(int _N, int _M, int _K, vector<int> R, vector<int> C)
{
    int i, j, k;
    N=_N; M=_M; K=_K;

    for(i=1; i<=N; i++) B[i]={min(R[i-1], C[i-1]), max(R[i-1], C[i-1])};
    sort(B+1, B+N+1, [&](const Point &p, const Point &q)
         {
             if(p.y==q.y) return p.x>q.x;
             return p.y<q.y;
         });

    int cnt=1;
    ll val=-1;
    for(i=1; i<=N; i++) if(val<B[i].x) A[cnt++]=B[i], val=B[i].x;
    N=cnt-1;
    for(i=1; i<=N; i++) if(A[i].x>A[i].y) swap(A[i].x, A[i].y);

    for(i=1; i<=N; i++) dp[1][i]=(A[i].y-A[1].x+1)*(A[i].y-A[1].x+1);

    /*
    for(i=2; i<=K; i++)
    {
        for(j=1; j<=N; j++)
        {
            dp[i][j]=(A[j].y-A[1].x+1)*(A[j].y-A[1].x+1);
            for(k=2; k<=j; k++) dp[i][j]=min(dp[i][j], dp[i-1][k-1]+(A[j].y-A[k].x+1)*(A[j].y-A[k].x+1)-max(0ll, A[k-1].y-A[k].x+1)*max(0ll, A[k-1].y-A[k].x+1));
        }
    }
    */

    for(i=2; i<=K; i++)
    {
        cht.init();
        dp[i][1]=(A[1].y-A[1].x+1)*(A[1].y-A[1].x+1);
        for(j=2; j<=N; j++)
        {
            cht.update({-2*A[j].x, dp[i-1][j-1]+(A[j].x-1)*(A[j].x-1)-max(0ll, A[j-1].y-A[j].x+1)*max(0ll, A[j-1].y-A[j].x+1)});
            dp[i][j]=min((A[j].y-A[1].x+1)*(A[j].y-A[1].x+1), cht.query(A[j].y)+A[j].y*A[j].y+2*A[j].y);
        }
    }

    //for(i=1; i<=K; i++) { for(j=1; j<=N; j++) printf("%lld ", dp[i][j]); printf("\n"); }
    return dp[K][N];
}

컴파일 시 표준 에러 (stderr) 메시지

aliens.cpp: In member function 'll CHT::query(ll)':
aliens.cpp:32:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(S.size()<=pos) pos=S.size()-1;
            ~~~~~~~~^~~~~
aliens.cpp:33:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++;
                    ~~~~~^~~~~~~~~
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:50:15: warning: unused variable 'k' [-Wunused-variable]
     int i, j, 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...