Submission #750562

#TimeUsernameProblemLanguageResultExecution timeMemory
750562M_WSplit the sequence (APIO14_sequence)C++17
0 / 100
172 ms131072 KiB
#include <bits/stdc++.h>
#define ar array<long long, 3> 
using namespace std;
long long a[100001], dp[200][100001][2], qs[100001];
vector<ar> cht;
int idx = 0;

bool should_pop(ar cur, ar line1, ar line2){
  if(cur[0] == line1[0] && cur[1] > line1[1]) return true; 
  return (line2[1] - cur[1]) * (cur[0] - line1[0]) >= (line1[1] - cur[1]) * (cur[0] - line2[0]);
}

long long f(ar line, long long x){
  return line[0] * x + line[1];
}

pair<long long, int> query(long long x){
  if(cht.empty()) return {0, 0};
  idx = min(idx, int(cht.size() - 1));
  while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) idx++;
  return {f(cht[idx], x), cht[idx][2]};
}

int main(int argc, char* argv[]){
  int N, K; scanf("%d %d", &N, &K);
  for(int i = 1; i <= N; i++){
    scanf("%lld", &a[i]);
    qs[i] = qs[i - 1] + a[i];
  }

  long long ans = 0;
  int maxj = 0;

  cht.push_back({0, 0, 0});
  for(int i = 1; i <= K; i++){
    for(int j = 1; j <= N; j++){
      if(j >= i - 1){
        long long const_val = qs[N] * qs[j] - (qs[j] * qs[j]); 
        pair<long long, int> res = query(qs[j]);

        dp[i][j][0] = const_val + res.first;
        dp[i][j][1] = res.second;

        if(dp[i][j][0] > ans){
          ans = dp[i][j][0];
          maxj = j;
        }
      }

      ar cur = {qs[j], dp[i - 1][j][0] - (qs[N] * qs[j]), j};
      while(cht.size() > 1){
        int sz = cht.size();
        if(should_pop(cur, cht[sz - 1], cht[sz - 2])) cht.pop_back();
        else break;
      }
      if(cht.empty() || cur[0] > cht.back()[0] || (cur[0] == cht.back()[0] && cur[1] >= cht.back()[1])) cht.push_back(cur);
    }
    cht.clear();
  }
  printf("%lld\n", ans);
  if(ans == 0) for(int i = 1; i <= K; i++) printf("%d ", i);
  else{
    for(int i = K; i > 0; i--){
      printf("%d ", maxj);
      maxj = dp[i][maxj][1];
    }
  }
  return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'std::pair<long long int, int> query(long long int)':
sequence.cpp:20:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |   while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) idx++;
      |         ~~~~^~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main(int, char**)':
sequence.cpp:25:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   int N, K; scanf("%d %d", &N, &K);
      |             ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%lld", &a[i]);
      |     ~~~~~^~~~~~~~~~~~~~~
#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...