제출 #1106420

#제출 시각아이디문제언어결과실행 시간메모리
1106420ALTAKEXE수열 (APIO14_sequence)C++17
100 / 100
314 ms82248 KiB
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define inf INT_MAX
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FAR(i, a, b) for (int i = (a); i >= (b); i--)
#define all(x) (x.begin(), x.end())
const int MOD = 1e9 + 7;
using namespace std;
int n, k;
int pos[201][100001];
ll pref[100001]{0}, dp[2][100001], q[100001], l = 1, r = 1;
bool case1(int x, int y, int i)
{
  return (dp[0][y] - dp[0][x] >= (pref[y] - pref[x]) * (pref[n] - pref[i]));
}
bool case2(int x, int y, int i)
{
  return ((dp[0][y] - dp[0][x]) * (pref[i] - pref[y]) <=
          (dp[0][i] - dp[0][y]) * (pref[y] - pref[x]));
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  FOR(i, 1, n + 1)
  {
    int x;
    cin >> x;
    pref[i] = pref[i - 1] + x;
  }
  fill(dp[0], dp[0] + n + 1, 0);
  FOR(i, 1, k + 1)
  {
    q[r++] = 0;
    FOR(j, 1, n + 1)
    {
      while (r - l > 1 && case1(q[l], q[l + 1], j))
        l++;
      ll x = q[l];
      dp[1][j] = dp[0][x] + (pref[j] - pref[x]) * (pref[n] - pref[j]);
      pos[i][j] = x;
      while (r - l > 1 && case2(q[r - 2], q[r - 1], j))
        r--;
      q[r++] = j;
    }
    l = r = 1;
    FOR(j, 1, n + 1)
    dp[0][j] = dp[1][j];
  }
  ll mx = -1;
  int _i = -1;
  FOR(i, 1, n + 1)
  {
    if (dp[0][i] > mx)
      mx = dp[0][i], _i = i;
  }
  cout << mx << endl;
  FOR(i, 0, k)
  {
    cout << _i << ' ';
    _i = pos[k - i][_i];
  }
  return 0;
}
#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...