제출 #1154259

#제출 시각아이디문제언어결과실행 시간메모리
1154259simuyuPo (COCI21_po)C++20
70 / 70
6 ms1604 KiB
#include <bits/stdc++.h> using namespace std; #define f first #define s second #define ll long long ll N; ll diffs[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; ll prev = 0, t; for (ll i=0; i<N; i++) { cin >> t; diffs[i] = t-prev; prev=t; } diffs[N] = 0 - prev; // use stack ll res = 0; stack<ll> nums; // number of positives stored for (ll i=0; i<=N; i++) { if (diffs[i] == 0) continue; if (diffs[i] > 0) { // positive - add to stack nums.push(diffs[i]); } else { // negative - remove from stack ll num_rem = -diffs[i]; // amount that we need to remove from stack while (true) { if (nums.top() > num_rem) { // we remove a bit from nums.top() res ++; ll t = nums.top() - num_rem; nums.pop(); nums.push(t); break; } else { // remove nums.top() res ++; num_rem -= nums.top(); nums.pop(); if (num_rem == 0) break; } } } } if (!nums.empty()) { // should never happen. TLE if it happens ll t=0; while (true) t++; } cout << res << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...