제출 #857542

#제출 시각아이디문제언어결과실행 시간메모리
857542Nuraly_Serikbay수열 (APIO14_sequence)C++14
50 / 100
2033 ms13576 KiB
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <cassert> #include <iomanip> #include <iostream> #include <algorithm> #include <stdio.h> #include <fstream> #include <unordered_map> using namespace std; typedef long long ll; typedef long double ld; #define all(x) x.begin(),x.end() #define pb push_back const int maxn = (int)1e5 + 13; const ll inf = (long long)1e9 + 20; const int mod = 1e9 + 7; #define int long long int n,a[maxn],k; ll pref[maxn],suf[maxn],r; pair<ll,ll> dp[maxn],dp1[maxn]; pair<int,int>v[maxn]; int v1[maxn],pr[201][1001]; double inter(pair<int,int>x,pair<int,int>y){ if(x.first == y.first){ return 0; } return 1.0 * (y.second - x.second) / (x.first - y.first); } pair<ll,int> get(ll x){ if(1 > r)return {0,0}; ll L = 1,R = r,ans = 0,mn = -inf; while(L <= R){ ll y = v[L].first * x + v[L].second; if(y > mn){ mn = y; ans = v1[L]; } L++; } return {mn,ans}; } void add(pair<int,int>x,int ind){ while(r>=2 && inter(v[r - 1],x) < inter(v[r],v[r - 1]))r --; v[++r] = x; v1[r] = ind; } void solve(){ cin >> n >> k; for(int i = 1 ; i <= n ; i ++){ cin >> a[i]; pref[i] = pref[i - 1] + a[i]; } for(int i = n ; i >= 1 ; i --){ suf[i] = suf[i + 1] + a[i]; } for(int i = 1 ; i < n ;i ++){ dp[i] = {pref[i] * suf[i + 1],i}; } int mx = -inf,ind = 0; for(int kol = 2 ; kol <= k ; kol ++){ r = 0; add({pref[kol-1],dp[kol-1].first},kol-1); for(int i = kol ; i < n ; i ++){ pair<int,int> y = get(-suf[i + 1]); ll x = pref[i] * suf[i + 1] + y.first; dp1[i] = {x,y.second}; pr[kol][i] = y.second; add({pref[i],dp[i].first},i); } //cout << "\n"; for(int i = kol - 1; i <= n ; i ++){ dp[i] = dp1[i]; } } for(int i = k ; i <= n ; i ++){ if(mx < dp[i].first){ mx = dp[i].first; ind = i; } } cout << mx << "\n"; vector<int>v; v.pb(ind); int ok = pr[k][ind]; while(ok){ v.pb(ok); ok = pr[--k][ok]; } for(int i = v.size() - 1 ; i >= 0 ; i --){ cout << v[i] << ' '; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while(t --){ solve(); } return 0; }
#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...