제출 #1160462

#제출 시각아이디문제언어결과실행 시간메모리
1160462sl1pzzBigger segments (IZhO19_segments)C++20
100 / 100
200 ms27836 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define ew "\n"
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
#define len(x) (int)x.size()
#define eb emplace_back
#define turbo                       \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0);

const int maxn = 1e6 + 12;
const int mxx = 100100;
const int MOD = 1e9 + 7;
const int inf = 2e18 + 7;
const int HH = 1e2 + 70;

int n;
int a[maxn], pref[maxn], t[maxn * 4], dp[maxn];

void upd(int v, int tl, int tr, int pos, int x)
{
  if (tl == tr)
  {
    t[v] = x;
    return;
  }
  int md = (tl + tr) >> 1;
  if (pos <= md)
  {
    upd(v + v, tl, md, pos, x);
  }
  else
  {
    upd(v + v + 1, md + 1, tr, pos, x);
  }
  t[v] = min(t[v + v + 1], t[v + v]);
}

int get(int v, int tl, int tr, int x)
{
  if (tl == tr)
  {
    return tl;
  }
  int md = (tl + tr) >> 1;
  if (t[v + v + 1] <= x)
  {
    return get(v + v + 1, md + 1, tr, x);
  }
  else
  {
    return get(v + v, tl, md, x);
  }
}

void code()
{
  cin >> n;
  for (int i = 0; i <= 4 * n; i++)
  {
    t[i] = inf;
  }
  for (int i = 1; i <= n; i++)
  {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i];
  }
  for (int i = 1; i <= n; i++)
  {
    int j = 0;
    if (t[1] <= pref[i])
    {
      j = get(1, 1, n, pref[i]);
    }
    dp[i] = dp[j] + 1;
    upd(1, 1, n, i, pref[i] + (pref[i] - pref[j]));
  }
  cout << dp[n];
}

int32_t main()
{
  // freopen("248.in", "r", stdin);
  // freopen("248.out", "w", stdout);
  // turbo;
  int tt = 1, j = 1;
  // cin >> tt;
  while (tt--)
  {
    // cout << "Case " << j << ":" << ew;
    code();
    // j++;
  }
}
#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...