제출 #541016

#제출 시각아이디문제언어결과실행 시간메모리
541016mansur수열 (APIO14_sequence)C++17
71 / 100
76 ms131072 KiB
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #include<bits/stdc++.h> using namespace std; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define sz(a) a.size() #define int long long #define pb push_back #define vt vector #define s second #define f first #define nl '\n' using pii = pair<int, int>; using ll = long long; vt<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int inf = 1e9, N = 1e6 + 5, mod = 1e9 + 7; double eps = 1e-6; bool fl = 0; int ntr(pii a, pii b) { int x = b.f - a.f; int y = a.s - b.s; return (x + y - 1) / y; } void solve() { int n, k; cin >> n >> k; int p[n + 1]; bool is[n + 1][k + 1]; p[0] = 0; for (int i = 1, a; i <= n; i++) { cin >> a; p[i] = p[i - 1] + a; } int dp[n + 1][k + 1]; for (int i = 0; i <= n; i++) { for (int s = 0; s <= k; s++) dp[i][s] = -inf, is[i][s] = 0; } dp[0][0] = 0; vector<pair<pii, pii>> s; s.pb({{0, -1}, {0, 0}}); for (int j = 0; j <= k; j++) { int l = 0; for (int i = j + 1; i <= n; i++) { while (l < sz(s) && s[l].f.s < i && s[l].f.f <= p[i]) l++; dp[i][j] = max(dp[i - 1][j], s[l - 1].s.f + s[l - 1].s.s * p[i]); if (dp[i][j] == dp[i - 1][j]) { is[i][j] = 1; } } s.clear(); for (int i = j + 1; i <= n; i++) { int a = dp[i][j] - p[i] * p[i], b = p[i]; int ps = 0; if (sz(s) && b == s.back().s.s) { if (a > s.back().s.f) { s.pop_back(); while (sz(s) && ntr({a, b}, s.back().s) <= s.back().f.f) s.pop_back(); if (sz(s)) ps = ntr({a, b}, s.back().s); while (sz(s) && s.back().f.f < 0) s.pop_back(); s.pb({{ps, i}, {a, b}}); } continue; } while (sz(s) && ntr({a, b}, s.back().s) <= s.back().f.f) s.pop_back(); if (sz(s)) ps = ntr({a, b}, s.back().s); while (sz(s) && s.back().f.f < 0) s.pop_back(); s.pb({{ps, i}, {a, b}}); } } cout << dp[n][k] << nl; vector<int> ans; int i = n, j = k; while (j > 0) { if (is[i][j]) { i--; continue; } for (int x = j; x <= i - 1; x++) { int a = dp[x][j - 1] - p[x] * p[x], b = p[x]; if (a + b * p[i] == dp[i][j]) { i = x; j--; ans.pb(x); break; } } } for (int x: ans) cout << x << ' '; } main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(NULL); cin.tie(NULL); int tp = 1; if (fl) cin >> tp; while (tp--) solve(); }

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

sequence.cpp: In function 'void solve()':
sequence.cpp:55:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |    while (l < sz(s) && s[l].f.s < i && s[l].f.f <= p[i]) l++;
      |             ^
sequence.cpp: At global scope:
sequence.cpp:102:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  102 | main() {
      | ^~~~
#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...