Submission #30641

# Submission time Handle Problem Language Result Execution time Memory
30641 2017-07-25T19:49:58 Z Nirjhor Split the sequence (APIO14_sequence) C++14
0 / 100
23 ms 93748 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int K = 205;
const int N = 100005;

struct line {
  int id;
  ll m, c;
};

int n, k, ptr;
ll a[N], f[2][N];
int pre[K][N];
vector <line> v[2];
stack <int> opts;

bool bad (line i, line j, line k) {
  return (i.c - k.c) * (j.m - i.m) < (i.c - j.c) * (k.m - i.m);
}

void add (int at, line l) {
  v[at].push_back(l);
  while (v[at].size() >= 3 and bad(v[at][v[at].size() - 3], v[at][v[at].size() - 2], v[at][v[at].size() - 1])) {
    v[at].erase(v[at].end() - 2);
  }
}

line query (int at, ll x) {
  while (ptr < v[at].size() - 1 and v[at][ptr].m * x + v[at][ptr].c < v[at][ptr + 1].m * x + v[at][ptr + 1].c) {
    ++ptr;
  }
  ptr = min(ptr, (int) v[at].size() - 1);
  return v[at][ptr];
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    a[i] += a[i - 1];
  }
  int now = 1, bef = 0;
  for (int i = 1; i <= k; ++i) {
    ptr = 0, v[bef].clear();
    // add(i - 1, {i, a[i], f[i - 1][i] - a[i] * a[i]});
    add(bef, {i, a[i], f[bef][i] - a[i] * a[i]});
    for (int j = i + 1; j <= n; ++j) {
      line opt = query(bef, a[j]);
      f[now][j] = opt.m * a[j] + opt.c;
      pre[i][j] = opt.id;
      add(bef, {j, a[j], f[bef][j] - a[j] * a[j]});
    }
    swap(now, bef);
  }
  printf("%lld\n", f[bef][n]);
  for (int i = k, j = n; i; --i) {
    j = pre[i][j];
    assert(0 < j and j < n);
    opts.push(j);
  }
  while (!opts.empty()) {
    printf("%d ", opts.top());
    opts.pop();
  }
  puts("");
  return 0;
}

Compilation message

sequence.cpp: In function 'line query(int, ll)':
sequence.cpp:33:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (ptr < v[at].size() - 1 and v[at][ptr].m * x + v[at][ptr].c < v[at][ptr + 1].m * x + v[at][ptr + 1].c) {
              ^
sequence.cpp: In function 'int main()':
sequence.cpp:41:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &k);
                         ^
sequence.cpp:43:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", a + i);
                         ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 84448 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 84448 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 84448 KB contestant found the optimal answer: 0 == 0
4 Correct 0 ms 84448 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 0 ms 84448 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Incorrect 0 ms 84448 KB contestant didn't find the optimal answer: 0 < 1
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 84448 KB contestant didn't find the optimal answer: 252308 < 1093956
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 84448 KB contestant didn't find the optimal answer: 484133 < 610590000
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 84596 KB contestant didn't find the optimal answer: 395622 < 21503404
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 85684 KB contestant didn't find the optimal answer: 1187850 < 1818678304
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 93748 KB contestant didn't find the optimal answer: 5054352 < 19795776960
2 Halted 0 ms 0 KB -