Submission #218245

#TimeUsernameProblemLanguageResultExecution timeMemory
218245emil_physmathAliens (IOI16_aliens)C++17
4 / 100
6 ms512 KiB
#include "aliens.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using llong = long long;
struct Rat
{
    llong a, b;
    Rat(llong a_ = 0, llong b_ = 1)
        : a(a_)
        , b(b_)
    {}
    Rat operator-() const { return Rat(-a, b); }
};
const Rat inf = Rat(10'000'000'000LL, 1);
bool operator<(const Rat& l, const Rat& r)
{
    if ((l.b < 0) ^ (r.b < 0))
        return l.a * r.b > r.a * l.b;
    return l.a * r.b < r.a * l.b;
}
struct Line
{
    llong k, b;
    Line(llong k_ = 1, llong b_ = 0)
        : k(k_)
        , b(b_)
    {}
    llong operator()(int x) const { return k * x + b; }
};
Rat Inter(const Line& a, const Line& b) { return Rat(a.b - b.b, b.k - a.k); }

int mergedto = -1;
vector<vector<Line> > cht;
vector<vector<Rat> > st;
vector<vector<int> > mrg;
void Add(vector<Line>& cht, vector<Rat>& st, vector<int>& mrg, Line l, int ind, int indismergedto)
{
    while (cht.size() >= 2 && Inter(cht.back(), l) < Inter(*next(cht.rbegin()), l))
    {
        cht.pop_back();
        st.pop_back();
        mrg.pop_back();
    }
    if (cht.size())
        st.push_back(Inter(cht.back(), l));
    else
        st.push_back(-inf);
    mrg.push_back(indismergedto);
    cht.push_back(l);
}
llong Min(vector<Line>& cht, vector<Rat>& st, vector<int>& mrg, int x)
{
    if (cht.size() == 2 && cht[1].k == -2 && cht[1].b == 2 && x == 3)
        cerr << "";
    int i = prev(upper_bound(st.begin(), st.end(), Rat(x))) - st.begin();
    mergedto = mrg[i];
    return cht[i](x);
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
    swap(m, n);
    cht.resize(k + 1); st.resize(k + 1); mrg.resize(k + 1);
    struct Point { int x, y; };
    vector<Point> a(m);
    for (int i = 0; i < m; ++i)
    {
        if (r[i] < c[i])
            swap(r[i], c[i]);
        a[i] = Point{r[i], n - c[i] - 1};
    }
    sort(a.begin(), a.end(), [](const Point& l, const Point& r) {
         return make_pair(l.x, -l.y) < make_pair(r.x, -r.y);
    });
    vector<Point> b;
    for (int i = 0; i < a.size(); ++i)
    {
        while (b.size() && b.back().y <= a[i].y)
            b.pop_back();
        b.push_back(a[i]);
    }
    vector<int> hei(b.size());
    for (int i = 0; i < b.size(); ++i)
        hei[i] = b[i].y + b[i].x - n + 2;
    vector<llong> dp(b.size());
    dp[0] = hei[0] * hei[0];
    for (int i = 1; i < b.size(); ++i)
        dp[i] = dp[i - 1] + hei[i] * hei[i] - max(b[i - 1].x - b[i].x + hei[i], 0) * max(b[i - 1].x - b[i].x + hei[i], 0);
    return dp[b.size() - 1];
    /*vector<vector<llong> > dp(b.size(), vector<llong>(k + 1));
    for (int j = 1; j <= k; ++j)
    {
        dp[0][j] = hei[0] * hei[0];
        int i = 0;
        llong l = b[0].x;
        Add(cht[j], st[j], mrg[j], Line(2LL * hei[i] - 2LL * l, -2LL * l * hei[i] + l * l + dp[i][j]), 0, 0);
    }
    for (int i = 1; i < b.size(); ++i)
        for (int j = 1; j <= k; ++j)
        {
            dp[i][j] = b[i].x * b[i].x + Min(cht[j], st[j], mrg[j], b[i].x);
            // cerr << dp[i][j] << " s\n";
            if (j > 1)
            {
                llong curdp = dp[i - 1][j - 1] + hei[i] * min(hei[i], b[i].x - b[i - 1].x);
                llong l = b[i].x;
                Add(cht[j], st[j], mrg[j], Line(2LL * hei[mergedto] - 2LL * l, -2LL * l * hei[mergedto] + l * l + curdp), i, mergedto);
                if (curdp < dp[i][j])
                {
                    dp[i][j] = curdp;
                    mergedto = i;
                }
            }
            // cerr << dp[i][j] << " e\n";
            // cerr << "dp[" << i << "][" << j << "] = " << dp[i][j] << '\n';
            // if (i == 1 && j == 2)
                // cerr << 2LL * hei[i] - 2LL * l << ", " << -2LL * l * hei[i] + l * l + dp[i][j] << '\n';
        }
    return dp[b.size() - 1][b.size()];
    llong ans = dp[b.size() - 1][1];
    for (int j = 1; j <= k; ++j)
        ans = min(ans, dp[b.size() - 1][j]);
    return ans;*/
}

Compilation message (stderr)

aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:76:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a.size(); ++i)
                     ~~^~~~~~~~~~
aliens.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < b.size(); ++i)
                     ~~^~~~~~~~~~
aliens.cpp:87:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < b.size(); ++i)
                     ~~^~~~~~~~~~
#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...