提出 #56073023


ソースコード 拡げる

/*
    Template Version: 1.0.0 - 20220620
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define TRAV(x, a) for (auto &x : a)

using namespace std;
using ll = long long; using ld = double; 
using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<ld, ld>;
using cd = complex<ld>; using vcd = vector<cd>;

using vi = vector<int>; using vl = vector<ll>;
using vd = vector<ld>; using vs = vector<string>;
using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; // vector<pair>

template<class T> using min_pq = priority_queue<T, vector<T>, greater<T> >;
template<class T> inline int ckmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; }
template<class T> inline int ckmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sort(ALL(v)); v.erase(unique(ALL(v)), end(v)); }

constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x)) 
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }

ll ceilDiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
ll floorDiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down
void setPrec(int x) { cout << fixed << setprecision(x); }

// TO_STRING
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string) s; }
string ts(string s) { return s; }
string ts(bool b) { return (b ? "true" : "false"); }

template<class T> using V = vector<T>;
template<class T> string ts(complex<T> c);
string ts(V<bool> v);
template<size_t sz> string ts(bitset<sz> b);
template<class T> string ts(T v);
template<class T, class U> string ts(pair<T,U> p);
template<class ...U> string ts(tuple<U...> u);

template<class T> string ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); }
string ts(V<bool> v) {string res = "{"; FOR(i,0,SZ(v)-1) res += char('0'+v[i]); res += "}"; return res; }
template<size_t sz> string ts(bitset<sz> b) { string res = ""; FOR(i,0,SZ(b)-1) res += char('0'+b[i]); return res; }
template<class T> string ts(T v) { // containers with begin(), end()
    bool fst = 1; string res = "";
    for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); }
    return res;
}
template<class T, class U> string ts(pair<T,U> p) { return "(" + ts(p.FI) + ", " + ts(p.SE) + ")"; }
template<size_t i, class T> string print_tuple_utils(const T& tup) { if constexpr(i == tuple_size<T>::value) return ")"; else return (i ? ", " : "(") + ts(get<i>(tup)) + print_tuple_utils<i + 1, T>(tup); }
template<class ...U> string ts(tuple<U...> u) { return print_tuple_utils<0, tuple<U...>>(u); }

// OUTPUT
template<class T> void pr(T x) { cout << ts(x); }
template<class T, class ...U> void pr(const T& t, const U&... u) { pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
template<class T, class ...U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); }

// DEBUG
void DBG() { cerr << "]" << endl; }
template<class T, class ...U> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); }

#ifdef LOCAL_DEBUG
#define CONCAT(x, y) x##y
#define with_level setw(__db_level * 2) << setfill(' ') << "" << setw(0)
#define dbg(...) cerr << with_level << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define chk(...) if (!(__VA_ARGS__)) cerr << setw(__db_level * 2) << setfill(' ') << "" << setw(0) << "Line(" << __LINE__ << ") -> function(" << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#define db_block() debug_block CONCAT(dbbl, __LINE__)
int __db_level = 0;
struct debug_block {
    debug_block() { cerr << with_level << "{" << endl; ++__db_level; }
    ~debug_block() { --__db_level; cerr << with_level << "}" << endl; }
};
#else
#define dbg(...) 0
#define chk(...) 0
#define db_block() 0
#endif

const ld PI = acos(-1.0);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
const ld EPS = 1e-9;
const ll MODBASE = 1000000007LL;
const int INF = 0x3f3f3f3f;

const int MAXN = 200010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

struct Line {
    ll a, b; // y = a * x + b
    Line(ll a = 0, ll b = 0) : a(a), b(b) {}
    ll eval(ll x) { return a * x + b; }
};

struct ConvexHullTrick {
    vector<Line> lines;
    int ptr = 0;

    bool bad(Line l1, Line l2, Line l3) {
        // a1 > a2 > a3
        // intersection(l1, l3) < intersection(l1, l2) => l2 is bad, remove l2
        return (l3.b - l1.b) * (l1.a - l2.a) < (l2.b - l1.b) * (l1.a - l3.a);
    }

    void addLine(ll a, ll b) {
        // a decreasing
        Line l(a, b);
        while (SZ(lines) >= 2 && bad(lines[SZ(lines)-2], lines.back(), l)) lines.pop_back();
        lines.push_back(l);
    }

    ll get(ll x) {
        // x increasing
        while (ptr < SZ(lines) - 1 && lines[ptr].eval(x) > lines[ptr+1].eval(x)) ptr++;
        if (ptr >= SZ(lines)) ptr = SZ(lines) - 1;
        return lines[ptr].eval(x);
    }
};

int n;
ll C, h[MAXN], dp[MAXN];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> C;
    FOR(i,1,n) cin >> h[i];

    ConvexHullTrick cht;
    dp[1] = 0;
    cht.addLine(-2 * h[1], h[1] * h[1] + dp[1]);

    FOR(i,2,n) {
        dp[i] = h[i] * h[i] + C + cht.get(h[i]);
        cht.addLine(-2 * h[i], h[i] * h[i] + dp[i]);
    }

    cout << dp[n];
    return 0;
}

提出情報

提出日時
問題 Z - Frog 3
ユーザ farmerboy
言語 C++ 20 (gcc 12.2)
得点 100
コード長 6054 Byte
結果 AC
実行時間 15 ms
メモリ 10416 KiB

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 39
セット名 テストケース
All 0_00, 0_01, 0_02, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09, 1_10, 1_11, 1_12, 1_13, 1_14, 1_15, 1_16, 1_17, 1_18, 1_19, 1_20, 1_21, 1_22, 1_23, 1_24, 1_25, 1_26, 1_27, 1_28, 1_29, 1_30, 1_31, 1_32, 1_33, 1_34, 1_35
ケース名 結果 実行時間 メモリ
0_00 AC 1 ms 3580 KiB
0_01 AC 1 ms 3504 KiB
0_02 AC 1 ms 3452 KiB
1_00 AC 1 ms 3620 KiB
1_01 AC 1 ms 3504 KiB
1_02 AC 14 ms 9944 KiB
1_03 AC 14 ms 9944 KiB
1_04 AC 14 ms 9960 KiB
1_05 AC 15 ms 9940 KiB
1_06 AC 12 ms 7376 KiB
1_07 AC 14 ms 9928 KiB
1_08 AC 14 ms 9864 KiB
1_09 AC 14 ms 9808 KiB
1_10 AC 14 ms 9932 KiB
1_11 AC 14 ms 9888 KiB
1_12 AC 14 ms 9812 KiB
1_13 AC 14 ms 10240 KiB
1_14 AC 14 ms 10140 KiB
1_15 AC 14 ms 10416 KiB
1_16 AC 13 ms 8172 KiB
1_17 AC 12 ms 7196 KiB
1_18 AC 12 ms 7296 KiB
1_19 AC 14 ms 10412 KiB
1_20 AC 13 ms 7964 KiB
1_21 AC 13 ms 8236 KiB
1_22 AC 12 ms 6920 KiB
1_23 AC 12 ms 7348 KiB
1_24 AC 12 ms 7348 KiB
1_25 AC 13 ms 7856 KiB
1_26 AC 13 ms 8148 KiB
1_27 AC 12 ms 7136 KiB
1_28 AC 13 ms 8060 KiB
1_29 AC 12 ms 7380 KiB
1_30 AC 13 ms 8204 KiB
1_31 AC 13 ms 7884 KiB
1_32 AC 13 ms 7132 KiB
1_33 AC 13 ms 8436 KiB
1_34 AC 13 ms 7864 KiB
1_35 AC 14 ms 8132 KiB