제출 #1017161

#제출 시각아이디문제언어결과실행 시간메모리
1017161Gray수열 (APIO14_sequence)C++17
11 / 100
2378 ms131072 KiB
#include <algorithm> #include <cassert> #include <cstring> #include <ios> #include <iostream> #include <map> #include <vector> #define ll long long #define ull unsigned ll #define ld long double #define ff first #define ss second #define ln "\n" #define pll pair<ll, ll> using namespace std; vector<ll> dep(100001), wr(201), pref(100001), vals(100001); int link[100001][201]; void get(ll l, ll r, ll optl, ll optr, ll n, ll k){ if (l>r) return; ll m = (l+r)/2; pll optm = {-1, -1}; for (ll i=optl; i<=m; i++){ optm = max(optm, {dep[i]+(pref[m]-pref[i])*(pref[n]-pref[m]), i}); } wr[m]=optm.ff; // assert(optm.ss!=-1); link[m][k]=(int)(optm.ss); get(l, m-1, optl, optm.ss, n, k); get(m+1, r, optm.ss, optr, n, k); } void solve(){ memset(link, 0, sizeof(link)); ll n, k; cin >> n >> k; for (ll i=1; i<=n; i++){ cin >> vals[i]; pref[i]+=pref[i-1]+vals[i]; } for (ll i=1; i<=k; i++){ get(1, n, 0, n, n, i); wr.swap(dep); } pair<ll, ll> mx={-1, -1}; for (ll i=0; i<=n; i++) mx=max(mx, {dep[i], i}); cout << mx.ff << ln; vector<ll> ans; for (ll i=k; i>=1; i--){ ans.emplace_back(mx.ss); mx.ss=link[mx.ss][i]; } for (ll i=k-1; i>=0; i--){ cout << ans[i] << " "; } cout << ln; } // dp[i][k] = maxj(dp[j][k-1]+(pref[i]-pref[j])*(pref[n]-pref[i])); // (dp[j][k-1]-pref[j]*pref[n])+pref[j]*pref[i]; int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); ll t=1; // cin >> t; while (t--)solve(); }
#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...