/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 17ms 576.0 KiB
#3 Accepted 22ms 532.0 KiB
#4 Accepted 19ms 532.0 KiB
#5 Accepted 41ms 764.0 KiB
#6 Accepted 20ms 536.0 KiB
#7 Accepted 46ms 532.0 KiB
#8 Accepted 20ms 532.0 KiB
#9 Accepted 20ms 532.0 KiB
#10 Accepted 28ms 324.0 KiB
#11 Accepted 19ms 532.0 KiB
#12 Accepted 45ms 568.0 KiB
#13 Accepted 41ms 568.0 KiB
#14 Accepted 25ms 764.0 KiB
#15 Accepted 22ms 532.0 KiB
#16 Accepted 47ms 532.0 KiB
#17 Accepted 20ms 532.0 KiB
#18 Accepted 20ms 532.0 KiB
#19 Accepted 20ms 532.0 KiB
#20 Accepted 20ms 532.0 KiB

Code

#include <iostream>
#include <cmath>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T;
    cin >> T;
    while(T--){
        int N;
        cin >> N;
        long long S = 0;
        for (int i = 0; i < N; i++){
            long long x;
            cin >> x;
            S += x;
        }
        // We want the maximum integer k such that k*(k-1)/2 <= S.
        // Solve k^2 - k - 2S <= 0.
        // The positive root is k = (1 + sqrt(1+8S)) / 2.
        long double disc = sqrt((long double)(1 + 8 * S));
        long long k = floor((1 + disc) / 2);
        
        // Adjust if necessary:
        while(k * (k - 1LL) / 2 > S) k--;
        while((k + 1LL) * k / 2 <= S) k++;
        
        cout << k << "\n";
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1114 Maximize the MEX
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 14:47:27
Judged At
2025-02-17 14:47:27
Judged By
Score
100
Total Time
47ms
Peak Memory
764.0 KiB