/*
ID: awesome35
LANG: C++14
TASK: vans
*/
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pq priority_queue
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 1000000007;//998244353
const ld pi = 3.1415926535;
void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
void build_primes(vi& primes, int size)
{
vi visited;
visited.rsz(size, 0);
FOR(i, 2, size)
{
if (visited[i] == 0)
{
primes.pb(i);
int a = i;
while (a < size)
{
visited[a] = 1;
a += i;
}
}
}
}
vector<vector<ll>> matrix_mult(vector<vector<ll>>& a, vector<vector<ll>>& b)
{
int n = a.size();
vector<vector<ll>> answer;
answer.resize(n);
for (int i = 0; i < n; i++) answer[i].resize(n, 0);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) // calculate answer[i][j]
{
for (int k = 0; k < n; k++)
answer[i][j] = (answer[i][j] + a[i][k] * b[k][j]) % inf;
}
}
return answer;
}
int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m, a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
ll power(ll x, ll y)
{
ll k = 1LL << 60;
ll z = 1;
while (k != 0)
{
z *= z;
z %= inf;
if (y >= k)
{
z *= x;
z %= inf;
y -= k;
}
k >>= 1;
}
return z;
}
struct point
{
ld x, y;
bool operator<(const point& rhs)const
{
if (x == rhs.x)return y < rhs.y;
return x < rhs.x;
}
};
struct pt
{
ll x, y;
bool operator<(const point& rhs)const
{
if (x == rhs.x)return y < rhs.y;
return x < rhs.x;
}
};
// remember that you need to take abs
long double area(point x, point y, point z)
{
return (x.y * y.x + y.y * z.x + z.y * x.x - x.x * y.y - y.x * z.y - z.x * x.y) / 2.0;
}
bool clockwise(point x, point y, point z)
{
return area(x, y, z) > 0;
}
// remember that you need to take abs
long double area(pt x, pt y, pt z)
{
return (x.y * y.x + y.y * z.x + z.y * x.x - x.x * y.y - y.x * z.y - z.x * x.y) / 2.0;
}
bool clockwise(pt x, pt y, pt z)
{
return area(x, y, z) > 0;
}
ll gcd(ll a, ll b)
{
if (a > b)swap(a, b);
if (a == 0)return b;
return gcd(a, b % a);
}
int popcount(ll a)
{
int count = 0;
while (a)
{
count += (a & 1);
a >>= 1;
}
return count;
}
ll choose(ll n, ll r)
{
ll p = 1, k = 1;
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
long long m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else
p = 1;
return p;
}
vll prefix_hash(string& a, vll& powers)
{
int n = sz(a);
vll prefix(n + 1);
F0R(i, n)prefix[i + 1] = (prefix[i] + powers[i] * (a[i] - 'a' + 1)) % inf;
return prefix;
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
//the return type was size_t. But isnt that problematic?
uint64_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct custom_hash_fast {
//the return type was size_t. But isnt that problematic?
uint64_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
x ^= FIXED_RANDOM;
return x ^ (x >> 16);
}
};
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen((s + ".in").c_str(), "r", stdin);
//freopen((s + ".out").c_str(), "w", stdout);
}
struct Line {
mutable ll k, m, p, index; // slope, y-intercept, last optimal x
ll eval(ll x) { return k * x + m; }
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LC0 : deque<Line> { // linear time w/ additional assumptions
// for doubles, use inf = 1/.0, div(a,b) = a/b
const ll inf = LLONG_MAX;
ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // floored division
ll bet(const Line& x, const Line& y) { // last x such that first line is better
if (x.k == y.k) return x.m >= y.m ? inf : -inf;
return div(y.m - x.m, x.k - y.k);
}
void addBack(Line L) { // assume nonempty
while (1) {
auto a = back(); pop_back(); a.p = bet(a, L);
if (size() && back().p >= a.p) continue;
pb(a); break;
}
L.p = inf; pb(L);
}
void addFront(Line L) {
while (1) {
if (!size()) { L.p = inf; break; }
if ((L.p = bet(L, front())) >= front().p) pop_front();
else break;
}
push_front(L);
}
void add(ll k, ll m, ll index) { // assume line goes to one end of deque
if (!size() || k <= front().k) addFront({ k,m,0,index });
else assert(k >= back().k), addBack({ k,m,0,index });
}
int ord = 0; // 1 = increasing, -1 = decreasing
pair<ll, ll> query(ll x) {
assert(ord);
if (ord == 1) {
while (front().p < x) pop_front();
return { front().eval(x),front().index };
}
else {
while (size() > 1 && prev(prev(end()))->p >= x) pop_back();
return { back().eval(x),back().index };
}
}
};
int main()
{
setIO("cbarn");
int n, k;
cin >> n >> k;
vll a(n);
F0R(i, n)cin >> a[i];
vll prefix(n + 1);
F0R(i, n)prefix[i + 1] = prefix[i] + a[i];
vector<vi>ans(k + 1, vi(n));
vector<vll>dp(2, vll(n));
F0R(i, k)
{
LC0 hull;
hull.ord = 1;
FOR(j, i + 1, n)
{
hull.add(prefix[j], dp[0][j - 1] - prefix[j] * prefix[j], j);
pair<ll, ll>t = hull.query(prefix[j + 1]);
dp[1][j] = t.f;
ans[i + 1][j] = t.s;
}
F0R(j, n)dp[0][j] = dp[1][j];
}
//pv(dp);
cout << dp[0][n - 1] << endl;
vi b;
int i = k, j = n - 1;
while (i)
{
j = ans[i][j];
b.pb(j--);
i--;
}
reverse(all(b));
pv(b);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
288 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
0 ms |
384 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
2 ms |
512 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
1 ms |
512 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
8 ms |
1152 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
9 ms |
1152 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
8 ms |
1024 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
9 ms |
1184 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
8 ms |
1060 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
3 ms |
512 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
4 ms |
640 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
896 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
3 ms |
1024 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
79 ms |
8704 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
3 ms |
896 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
58 ms |
5728 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
68 ms |
6392 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
67 ms |
6784 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
53 ms |
5632 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
63 ms |
6272 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
81 ms |
8056 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
6376 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
24 ms |
6632 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
768 ms |
84064 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
25 ms |
6888 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
900 ms |
84192 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
680 ms |
60384 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
664 ms |
64608 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
534 ms |
53728 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
587 ms |
60640 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
747 ms |
76252 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |