This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a <= b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a >= b) return a = b, true; return false;}
const int N = 1e5+5;
const int K = 2e2+5;
const ll INF = 1e18;
ll n, k, a[N], pref[N], dp[K][N];
namespace Brute{
const int N = 1e3+5;
ll dp[K][N], trace[K][N];
void process()
{
for(int i = 1; i <= n; i++){
dp[0][i] = 0;
for(int j = 1; j <= k; j++){
for(int x = j+1; x <= i; x++){
if(ckmx(dp[j][i], dp[j-1][x-1] + (pref[i] - pref[x-1]) * pref[x-1])){
trace[j][i] = x;
}
}
}
}
cout << dp[k][n] <<"\n";
vector<int> path;
for(int cur_k = k, cur_pos = n; cur_k; cur_pos = trace[cur_k][cur_pos]-1, --cur_k){
path.push_back(trace[cur_k][cur_pos]-1);
}
reverse(all(path));
for(int i = 0; i < sz(path); i++) cout << path[i] <<" "; cout <<"\n";
return;
}
}
struct Line{
ld a,b;
Line(ld a = 0, ld b = 0): a(a), b(b) {}
ld intersect(Line& x){return (x.b - b)/(a - x.a);}
bool operator < (const Line& x) const{return a < x.a;}
};
struct CHT{
deque<pair<Line,ld>> dq;
void ins(Line x){
while(!dq.empty() && dq.back().second >= dq.back().first.intersect(x)) dq.pop_back();
if(dq.empty()){
dq.push_back(make_pair(x, -INF));
}
else dq.push_back(make_pair(x, dq.back().first.intersect(x)));
}
Line get(ll x){
while(dq.size() > 1 && dq[1].second <= x) dq.pop_front();
dq.front().second = -INF;
return dq.front().first;
}
};
void solve()
{
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = a[i] + pref[i-1];
}
Brute::process();
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void Brute::process()':
sequence.cpp:57:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
57 | for(int i = 0; i < sz(path); i++) cout << path[i] <<" "; cout <<"\n";
| ^~~
sequence.cpp:57:66: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
57 | for(int i = 0; i < sz(path); i++) cout << path[i] <<" "; cout <<"\n";
| ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |