제출 #635996

#제출 시각아이디문제언어결과실행 시간메모리
635996ghostwriter수열 (APIO14_sequence)C++17
71 / 100
570 ms131072 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #endif #define st first #define nd second #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i)) #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i)) #define EACH(i, x) for (auto &(i) : (x)) #define WHILE while #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* Tran The Bao CTL - Da Lat Cay ngay cay dem nhung deo duoc cong nhan */ struct Line { ll a, b; int id; Line() {} Line(ll a, ll b, int id) : a(a), b(b), id(id) {} }; const ldb eps = 1e-7; const ll oo = 2e18 + 5; const int N = 1e5 + 5; const int K = 205; int n, k, a[N], s, f[N], pre[K][N]; ll d[K][N]; ll cal(int f, ll a, ll b) { return 1LL * f * s - 1LL * f * f + a * f + b; } ldb it(Line a, Line b) { if (a.a == b.a) return oo; return (ldb)(b.b - a.b) / (a.a - b.a); } void trace(int i, int j) { int prev = pre[i][j]; if (!prev) return; cout << prev << ' '; trace(i - 1, prev); } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); cin >> n >> k; FOR(i, 1, n) cin >> a[i]; FOR(i, 1, n) f[i] = f[i - 1] + a[i]; s = f[n]; FOR(i, 1, n) d[0][i] = -1; FOR(i, 1, k + 1) { vector<Line> cv; cv.pb(Line(0, d[i - 1][0], 0)); int l = 0; d[i][0] = -1; FOR(j, 0, n) { l = min(l, sz(cv) - 1); WHILE(l < sz(cv) - 1 && (d[i - 1][cv[l].id] < 0 || cal(f[j], cv[l].a, cv[l].b) <= cal(f[j], cv[l + 1].a, cv[l + 1].b))) ++l; if (d[i - 1][cv[l].id] >= 0) { d[i][j] = cal(f[j], cv[l].a, cv[l].b); pre[i][j] = cv[l].id; } else d[i][j] = -1; if (d[i - 1][j] < 0) continue; Line cur(2 * f[j], d[i - 1][j] -1LL * f[j] * s - 1LL * f[j] * f[j], j); WHILE(!cv.empty() && d[i - 1][cv.back().id] < 0) cv._pb(); WHILE(sz(cv) > 1) { Line tmp = cv[sz(cv) - 1], tmp1 = cv[sz(cv) - 2]; ldb x1 = it(tmp, tmp1), x2 = it(tmp, cur); if (x2 <= x1 || x2 - x1 <= eps) { cv._pb(); continue; } if (x2 == oo) cv._pb(); break; } if (!cv.empty() && cv.back().a == cur.a) cv._pb(); cv.pb(cur); } } cout << d[k + 1][n] / 2 << '\n'; trace(k + 1, n); return 0; } /* 7 3 4 1 3 4 0 2 3 5 1 0 9 6 3 0 */

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

sequence.cpp: In function 'int main()':
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sequence.cpp:64:5: note: in expansion of macro 'FOR'
   64 |     FOR(i, 1, n) cin >> a[i];
      |     ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sequence.cpp:65:5: note: in expansion of macro 'FOR'
   65 |     FOR(i, 1, n) f[i] = f[i - 1] + a[i];
      |     ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sequence.cpp:67:5: note: in expansion of macro 'FOR'
   67 |     FOR(i, 1, n) d[0][i] = -1;
      |     ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sequence.cpp:68:5: note: in expansion of macro 'FOR'
   68 |     FOR(i, 1, k + 1) {
      |     ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
sequence.cpp:73:9: note: in expansion of macro 'FOR'
   73 |         FOR(j, 0, n) {
      |         ^~~
#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...