1433B - Yet Another Bookshelf - CodeForces Solution


greedy implementation *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())) :
    
    n = int(input())
    arr = "".join(list(map(str, input().split())))
    arr = arr.strip("0")
    n = len(arr)
    
    arr0 = ["0"]*n
    
    for i in range(n) :
        if arr[i] == "0" :
            arr0[i] = "1"
        
    arr0 = "".join(arr0).strip("0")
        
    sm = 0    
        
    for i in arr0 :
        sm += int(i)
        
    print(sm if arr.count("1") > 1 else 0)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define For(i, s, e) for (ll i = s; i < e; i++)
#define FOR(i, s, e) for (ll i = s; i <= e; i++)
#define FORD(i, s, e) for (ll i = s; i >= e; i--)
#define pb push_back
#define vii vector<ll>
#define makep make_pair
#define vpll vector<pair<ll, ll>>
#define fi first
#define se second
#define sii set<ll>
#define pii pair<int, int>
#define rev(c) reverse(c.begin(), c.end())
#define sortf(c) sort(c.begin(), c.end())
#define sortd(c) sort(c.begin(), c.end(), greater<int>())
#define test()   \
    int test;    \
    cin >> test; \
    while (test--)
#define fast()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define sqr(n) (n)*(n)
#define len(s) (int) (s.length())
#define re0 return 0
#define re return

int const max2d = 1e4 + 7;
int const N = 1e6 + 7;
int const inf = 1e12 + 7;
int const mod = 1e9 + 7; 
char const nl = '\n';

// ! ¸,ø¤º°`°º¤ø,¸¸,ø¤º° [ нvмegy ] °º¤ø,¸¸,ø¤º°`°º¤ø,¸ roadтoнυe

signed main()
{
    fast();
    
    test() { 
        int n; 
        int st, ed; 
        st = ed = 0; 
        cin >> n;
        vii a(n+1, 0); 
        FOR(i, 1, n) { 
            cin >> a[i]; 
            if (a[i]) { 
                if (!st) st = i;
                else ed = i;
            }
        }
        int cnt = 0;
        FOR(i, st, ed) { 
            if (!a[i]) cnt++;
        }
        cout << cnt << nl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game