제출 #750580

#제출 시각아이디문제언어결과실행 시간메모리
750580M_W수열 (APIO14_sequence)C++17
100 / 100
845 ms82004 KiB
#include <bits/stdc++.h>
#define ar array<long long, 3> 
using namespace std;
long long dp[2][100001];
int pos[201][100001], qs[100001];
deque<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){
  while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) cht.pop_front();
  return {f(cht[0], x), cht[0][2]};
}

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

  long long ans = 0;
  int maxj = 0;

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

      dp[i % 2][j] = const_val + res.first;
      pos[i][j] = res.second;

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

      ar cur = {qs[j] * 1ll, dp[(i - 1) % 2][j] - (qs[N] * 1ll * (qs[j] * 1ll)), 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);
  for(int i = K; i > 0; i--){
    printf("%d ", maxj);
    maxj = pos[i][maxj];
  }
  return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

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