/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 18ms 576.0 KiB
#3 Accepted 26ms 788.0 KiB
#4 Accepted 24ms 944.0 KiB
#5 Accepted 25ms 956.0 KiB
#6 Accepted 21ms 788.0 KiB
#7 Accepted 23ms 788.0 KiB
#8 Accepted 45ms 788.0 KiB
#9 Accepted 21ms 788.0 KiB
#10 Accepted 21ms 956.0 KiB
#11 Accepted 20ms 788.0 KiB
#12 Accepted 36ms 788.0 KiB
#13 Accepted 45ms 788.0 KiB
#14 Accepted 45ms 788.0 KiB
#15 Accepted 47ms 788.0 KiB
#16 Accepted 22ms 792.0 KiB
#17 Accepted 23ms 836.0 KiB
#18 Accepted 32ms 788.0 KiB
#19 Accepted 26ms 788.0 KiB
#20 Accepted 26ms 788.0 KiB

Code

// Created on: 2024-11-05 20:16
// Author: Safwan_Ibrahim

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define endl '\n'

bool ok(int x,ll have) {
    ll sum = 1LL * x * (x + 1) / 2;
    return sum > have;
}

void solve()
{
    int n; cin >> n;
    int a[n + 1];
    ll sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum += a[i];
    }

    int ans = 1;
    int lo = 1, hi = 1e9 + 7;
    while(lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (ok(mid,sum)) {
            ans = mid;
            hi = mid - 1;
        }
        else {
            lo = mid + 1;
        }
    }     
    
    cout << ans << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
} 

Information

Submit By
Type
Submission
Problem
P1114 Maximize the MEX
Contest
Brain Booster #7
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 15:22:38
Judged At
2024-11-05 15:22:38
Judged By
Score
100
Total Time
47ms
Peak Memory
956.0 KiB