/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 13ms 560.0 KiB
#3 Accepted 13ms 540.0 KiB
#4 Accepted 9ms 540.0 KiB
#5 Accepted 9ms 588.0 KiB
#6 Accepted 8ms 556.0 KiB
#7 Accepted 6ms 328.0 KiB
#8 Accepted 3ms 540.0 KiB
#9 Accepted 3ms 568.0 KiB
#10 Accepted 3ms 540.0 KiB
#11 Accepted 4ms 564.0 KiB
#12 Accepted 3ms 540.0 KiB
#13 Accepted 4ms 560.0 KiB
#14 Accepted 4ms 328.0 KiB
#15 Accepted 4ms 564.0 KiB
#16 Accepted 4ms 568.0 KiB
#17 Accepted 5ms 496.0 KiB
#18 Accepted 4ms 332.0 KiB
#19 Accepted 13ms 560.0 KiB
#20 Accepted 15ms 636.0 KiB

Code

// Created on: 2025-01-02 21:55
// Author: Safwan_Ibrahim

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

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

void solve() {
    int n; cin >> n;
    int cnt1 = 0, cntm1 = 0, cnt0 = 0;
    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        cnt0 += x == 0; cnt1 += x == 1; cntm1 += x == -1;
    }

    int ans = 0;
    if (cnt1 >= cntm1 / 2) {
        cnt1 -= cntm1 / 2;
        ans += cntm1 / 2;
        ans += cnt1 / 3;
        if (cntm1 % 2 == 1 && cnt0 == 0) {
            ans--;
        }
    }
    else {
        ans += cnt1;
        cntm1 -= cnt1 * 2;
        if (cnt0 < cntm1 / 2) {
            cntm1 -= cnt0 * 2;
            ans -= cntm1 / 3;
        }
    }

    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
P1152 Special Array
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 16:19:27
Judged At
2025-01-02 16:19:27
Judged By
Score
100
Total Time
15ms
Peak Memory
636.0 KiB