#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
int N, K;
ll A[MAXN+10];
typedef __int128 dll;
struct Line { ll a, b, p; }; // ax+b
struct Frac
{
ll u, d;
Frac(ll _u, ll _d)
{
if(_d<0) u=-_u, d=-_d;
else u=_u, d=_d;
}
bool operator < (const Frac &ot) const { return (dll)u*ot.d < (dll)ot.u*d; }
};
// If all queries are integer, change 'Frac' to 'div', be careful when different direction !!
ll div(ll u, ll d) { return u/d - ((u^d)<0 && u%d); }
struct CHT
{
Frac cross(const Line &p, const Line &q) { return Frac(p.b-q.b, q.a-p.a); }
deque<Line> V;
// Lines are inserted from left to right
// if p, q, r is lines in V, cross(p, q) < cross(q, r)
// If not, reverse '<' in 'bool operator <'
void update(Line p)
{
if(!V.empty() && V.back().a==p.a)
{
if(V.back().b<=p.b) return; // 'V.back()' is more optimal than 'p'
V.pop_back();
}
while(V.size()>1 && !(cross(V[V.size()-2], V[V.size()-1]) < cross(V[V.size()-1], p))) V.pop_back();
V.push_back(p);
}
int query(ll x)
{
assert(!V.empty());
int lo=0, hi=V.size();
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(cross(V[mid-1], V[mid]) < Frac(x, 1)) lo=mid;
else hi=mid;
}
return V[lo].p;
}
// x must be queried from left to right ( same as push )
int query2(ll x)
{
assert(!V.empty());
while(V.size()>1 && cross(V[0], V[1]) < Frac(x, 1)) V.pop_front();
return V[0].p;
}
}cht;
namespace ailien
{
ll dp[MAXN+10];
int cnt[MAXN+10], memo[MAXN+10];
// dp[i] := optimal dp value of i
// cnt[i] := how much transitions were used in optimal dp[i]
// memo[i] := previous transition position from i
vector<int> V;
void solve(ll lambda) // changes dp, cnt, memo, V
{
V.clear();
// initialize dp, cnt, memo
dp[0]=0;
cht.V.clear();
cht.update({0, 0, 0});
for(int i=1; i<=N; i++)
{
// get_opt(i), cost(p, q) must be implemented
int opt = cht.query2(A[i]); // opt = argmin_{j<i} (dp[j] + cost(j, i)*2)
dp[i]=dp[opt] + (A[i]-A[opt])*(A[i]-A[opt])*2 - lambda; // Don't forget *2
cnt[i]=cnt[opt]+1;
memo[i]=opt;
cht.update({-4*A[i], 2*A[i]*A[i]+dp[i], i});
}
// V = restore optimal solution
for(int i=N; i>0;)
{
V.push_back(i);
i=memo[i];
}
V.push_back(0);
reverse(V.begin(), V.end());
}
void init(int _N)
{
N=_N;
}
// code for **minimum**
pair<ll, vector<int>> ailien(int K)
{
ll lo=-1e18, hi=1e18; // range for lambda is [2*lo+1, 2*hi+1]
while(lo+1<hi)
{
ll mid=lo+hi>>1;
solve(2*mid+1);
if(K<=cnt[N]) hi=mid;
else lo=mid;
}
vector<int> L, R, ansV;
solve(2*lo+1); L=V;
solve(2*hi+1); R=V;
if(L.size()-1==K) ansV=L;
else if(R.size()-1==K) ansV=R;
else
{
assert(L.size()-1<K && K<R.size()-1);
for(int i=0; i+1<R.size(); i++)
{
int l=R[i], r=R[i+1];
int lt=upper_bound(L.begin(), L.end(), l)-L.begin()-1;
if(lt+1<L.size() && L[lt]<=l && r<=L[lt+1] && i+L.size()-lt-1==K)
{
for(int j=0; j<=i; j++) ansV.push_back(R[j]);
for(int j=lt+1; j<L.size(); j++) ansV.push_back(L[j]);
assert(ansV.size()-1==K);
break;
}
}
}
solve(2*hi);
ll ans=dp[N]/2+hi*K;
return {ans, ansV};
}
}
int main()
{
scanf("%d%d", &N, &K); K++;
for(int i=1; i<=N; i++) scanf("%lld", &A[i]);
for(int i=1; i<=N; i++) A[i]+=A[i-1];
ailien::init(N);
auto [ans, ansV] = ailien::ailien(K);
ans=(A[N]*A[N]-ans)/2;
printf("%lld\n", ans);
for(int i=1; i+1<ansV.size(); i++) printf("%d ", ansV[i]);
printf("\n");
}
Compilation message
sequence.cpp: In member function 'int CHT::query(ll)':
sequence.cpp:52:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid=lo+hi>>1;
| ~~^~~
sequence.cpp: In function 'std::pair<long long int, std::vector<int> > ailien::ailien(int)':
sequence.cpp:116:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
116 | ll mid=lo+hi>>1;
| ~~^~~
sequence.cpp:126:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
126 | if(L.size()-1==K) ansV=L;
| ~~~~~~~~~~^~~
sequence.cpp:127:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
127 | else if(R.size()-1==K) ansV=R;
| ~~~~~~~~~~^~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from sequence.cpp:1:
sequence.cpp:130:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
130 | assert(L.size()-1<K && K<R.size()-1);
| ~~~~~~~~~~^~
sequence.cpp:130:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | assert(L.size()-1<K && K<R.size()-1);
| ~^~~~~~~~~~~
sequence.cpp:131:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | for(int i=0; i+1<R.size(); i++)
| ~~~^~~~~~~~~
sequence.cpp:136:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | if(lt+1<L.size() && L[lt]<=l && r<=L[lt+1] && i+L.size()-lt-1==K)
| ~~~~^~~~~~~~~
sequence.cpp:136:78: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
136 | if(lt+1<L.size() && L[lt]<=l && r<=L[lt+1] && i+L.size()-lt-1==K)
| ~~~~~~~~~~~~~~~^~~
sequence.cpp:139:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int j=lt+1; j<L.size(); j++) ansV.push_back(L[j]);
| ~^~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from sequence.cpp:1:
sequence.cpp:140:41: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
140 | assert(ansV.size()-1==K);
| ~~~~~~~~~~~~~^~~
sequence.cpp: In function 'int main()':
sequence.cpp:163:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | for(int i=1; i+1<ansV.size(); i++) printf("%d ", ansV[i]);
| ~~~^~~~~~~~~~~~
sequence.cpp:155:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | scanf("%d%d", &N, &K); K++;
| ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:156:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | for(int i=1; i<=N; i++) scanf("%lld", &A[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
1 ms |
2392 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
2648 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
1 ms |
2392 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
2 ms |
2396 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
2 ms |
2396 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
2 ms |
2396 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
2 ms |
2480 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
2 ms |
2396 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
3 ms |
2396 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
2 ms |
2396 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
2 ms |
2512 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
3 ms |
2392 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2648 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
17 ms |
2652 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
19 ms |
2688 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
18 ms |
2652 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
18 ms |
2652 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
18 ms |
2652 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
22 ms |
2652 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
19 ms |
2904 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
19 ms |
2648 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
25 ms |
2652 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
3808 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
205 ms |
4064 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
191 ms |
4416 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
177 ms |
4304 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
183 ms |
4300 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
198 ms |
4044 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
219 ms |
4336 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
200 ms |
4040 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
195 ms |
4048 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
198 ms |
4052 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |