제출 #1111834

#제출 시각아이디문제언어결과실행 시간메모리
1111834ljtunas수열 (APIO14_sequence)C++17
100 / 100
743 ms86852 KiB
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v)    memset(h, v, sizeof h)
#define Forv(i, a)     for(auto& i : a)
#define For(i, a, b)   for(int i = a; i <= b; ++i)
#define Ford(i, a, b)  for(int i = a; i >= b; --i)
#define c_bit(i)     __builtin_popcountll(i)
#define Bit(mask, i)    ((mask >> i) & 1LL)
#define onbit(mask, i)  ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
    template <typename T, int D>
    struct _vector : public vector <_vector <T, D - 1>> {
        static_assert(D >= 1, "Dimension must be positive!");
        template <typename... Args>
        _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
    };
    template <typename T> struct _vector <T, 1> : public vector <T> {
        _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
    };
    template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
    template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e5 + 5;
const  ll  mod = 1e9 + 7;
const  ll   oo = 1e18;


int n, k; int trace[MAXN][202]; bool ok[MAXN];
ll dp[MAXN], a[MAXN];

struct convexhulltrick{

    struct point {
        ll x, y; int id;
        ll dot(point p) {
            return x * p.x + y * p.y;
        }
        ll cross(point p) {
            return x * p.y - y * p.x;
        }
        point conj() {
            return {-y, x};
        }
        point vt(point p) {
            return {x - p.x, y - p.y};
        }
    };

    vector<point> hull, vec;

    void add(ll a, ll b, int id) {
        auto p = point{a, b, id};
        while (vec.size() >= 1 && vec.back().dot(p.vt(hull.back())) <= 0) { // <= 0 neu max
            vec.pop_back();
            hull.pop_back();
        }
        if (!hull.empty())
            vec.push_back(p.vt(hull.back()).conj());
        hull.push_back(p);
    }

    ii get(ll x) {
        if(hull.empty()) return {0, 0};
        auto query = point{x, 1};
        auto it = lower_bound(vec.begin(), vec.end(), query, [](point a, point b) {
            return a.cross(b) < 0; // <0 neu max
        });
        return {query.dot(hull[it - vec.begin()]), hull[it - vec.begin()].id};
    }

    void clear(){
        hull.clear(), vec.clear();
    }
} cvh;

void Solve() {
    cin >> n >> k;
    For(i, 1, n) cin >> a[i], a[i] += a[i - 1];
    reset(dp, -0x3f); dp[0] = 0; cvh.add(0, 0, 0);
    For(_k, 1, k){
        For(i, 1, n) {
            ll dpi = dp[i];
            ii tmp = cvh.get(a[n] - a[i]);
            if(i >= _k) dp[i] = tmp.fi + a[i] * (a[n] - a[i]);
            else dp[i] = -0x3f;
            cvh.add(-a[i], dpi, i);
            trace[i][_k] = tmp.se;
        }
        cvh.clear();
    }
    ll ans = 0;
    int i = 0, _k = k;
    For(j, 1, n) if(ans < dp[j]) i = j, ans = dp[j];
    vector<int> List;
    while(i > 0){
        ok [i] = true;
        List.push_back(i);
        i = trace[i][_k];
        --_k;
    }
    cout << ans << '\n';
    if(List.size() < k){
        For(i, 1, n) if(!ok[i] &&a[i] == a[i - 1]){
            List.push_back(i);
            if(List.size()==k) break;
        }
    }
//    cout<< List.size() << '\n';
    sort(all(List));
    Forv(x, List) cout << x << ' ';
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    if(fopen("APIO14_sequence.inp", "r")) {
        freopen("APIO14_sequence.inp", "r", stdin);
        freopen("APIO14_sequence.out", "w", stdout);
    }

    int t = 1;
//    cin >> t;

    for (int test = 1; test <= t; test++) {
        Solve();
    }
    return 0;
}

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

sequence.cpp: In function 'void Solve()':
sequence.cpp:110:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  110 |     if(List.size() < k){
      |        ~~~~~~~~~~~~^~~
sequence.cpp:113:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  113 |             if(List.size()==k) break;
      |                ~~~~~~~~~~~^~~
sequence.cpp: In function 'int main()':
sequence.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         freopen("APIO14_sequence.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen("APIO14_sequence.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...