/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 344.0 KiB
#2 Accepted 11ms 572.0 KiB
#3 Accepted 13ms 576.0 KiB
#4 Accepted 13ms 532.0 KiB
#5 Accepted 14ms 532.0 KiB
#6 Accepted 13ms 532.0 KiB
#7 Accepted 8ms 532.0 KiB
#8 Accepted 4ms 344.0 KiB
#9 Accepted 5ms 532.0 KiB
#10 Accepted 5ms 532.0 KiB
#11 Accepted 4ms 532.0 KiB
#12 Accepted 5ms 532.0 KiB
#13 Accepted 5ms 532.0 KiB
#14 Accepted 5ms 536.0 KiB
#15 Accepted 8ms 532.0 KiB
#16 Accepted 6ms 532.0 KiB
#17 Accepted 3ms 536.0 KiB
#18 Accepted 8ms 532.0 KiB
#19 Accepted 24ms 536.0 KiB
#20 Accepted 16ms 532.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
void solve()
{
    int n , minusOne = 0 , plusOne = 0 , zero = 0 , x ; cin >> n ;  ;
    for(int i = 0 ; i < n ; i++) {
        cin >> x ;
        if(x == 0) zero++ ;
        else if(x == 1) plusOne++ ;
        else minusOne++ ;
    }
    // Adding
    int adding = min(minusOne / 2 , plusOne) ;
    int deducting = 0 ;
    minusOne -= (adding * 2) ;
    plusOne -= adding ;
    adding += (plusOne / 3) ;
    plusOne %= 3 ;
    // deducting zero
    int deductingZero = min(minusOne / 2 , zero) ;
    minusOne -= (deductingZero * 2) ;
    zero -= deductingZero ;

    deductingZero = min(minusOne , zero / 2) ;
    zero -= (deductingZero * 2) ;
    minusOne -= deductingZero ;
     if(zero && minusOne && plusOne) {
        zero-- ;
        minusOne-- ;
        plusOne-- ;
     }
     zero %= 3 ;
    deductingZero = min(zero / 2 , plusOne) ;
    zero -= (deductingZero * 2) ;
    plusOne -= zero ;
    deductingZero = min(plusOne / 2 , zero) ;
    zero -= deductingZero ;
    plusOne -= (deductingZero * 2) ;

    if(minusOne){
        deducting = minusOne / 3 ;
        minusOne %= 3 ;
    }
    if(plusOne) {
        adding += (plusOne / 3) ;
        plusOne %= 3 ;
    }
    plusOne += minusOne ;
    plusOne += zero ;
    plusOne /= 3 ;
    deducting += plusOne ;
    adding -= deducting ;
    cout << adding << "\n" ;
}
int main()
{
     __Heart__
    // READ("0in.txt") ;
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1152 Special Array
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-19 01:33:55
Judged At
2024-12-19 01:33:55
Judged By
Score
100
Total Time
24ms
Peak Memory
576.0 KiB