제출 #893831

#제출 시각아이디문제언어결과실행 시간메모리
893831kigashBigger segments (IZhO19_segments)C++17
100 / 100
61 ms16092 KiB
#include "bits/stdc++.h"
using namespace std;       

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void IOIGold2024_InshAllah() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }

const ll inf = 1e18+7, MX = LLONG_MAX, MN = LLONG_MIN;
const ll mod = 1e9+7, N = 5e5+5;
ll a[N], pr[N];
pair<ll, ll> dp[N];

pair<ll, ll> comp(pair<ll, ll> x, pair<ll, ll> y) {
    if(x.ff==y.ff) return (x.ss<y.ss ? x : y);
    return (x.ff>y.ff ? x : y);
}

void kigash() {
    // dp[i] = {k, sum}
    // i < j <= n && pr[j]-pr[i]>=sum
    // dp[j] = max(dp[j], {dp[i].ff+1, -pr[j]+pr[i]})
    ll n;
    cin>>n;
    for(ll i=1; i<=n; i++) cin>>a[i], pr[i] = pr[i-1]+a[i];
    dp[1] = {1, a[1]};
    for(ll i=1; i<=n; i++) {
        dp[i] = comp({dp[i-1].ff, dp[i-1].ss+a[i]}, dp[i]);
        ll l = i+1, r = n, pos = -1;
        while(l<=r) {
            ll mid = l+(r-l)/2;
            if(pr[mid]-pr[i]>=dp[i].ss) pos = mid, r = mid-1;
            else l = mid+1;
        }
        if(pos!=-1) dp[pos] = comp(dp[pos], {dp[i].ff+1, pr[pos]-pr[i]});
    }
    cout<<dp[n].ff<<"\n";
    return;
}

signed main(/*Kigash Amir*/) {
    // freopen("");
    IOIGold2024_InshAllah();
    ll tt = 1;
    // cin>>tt;
    for(ll i=1; i<=tt; i++) {
        kigash();
    }
}

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

segments.cpp: In function 'void freopen(std::string)':
segments.cpp:17:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
      |                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:17:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
      |                                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...