1778A - Flip Flop Sum - CodeForces Solution


greedy implementation

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define ll  long long
#define sz(x) (int)x.size()
#define fi first
#define sc second
#define pb push_back
#define yes cout<<'YES'<<endl;
#define no cout<<'NO'<<endl;
#define all(a) (a).begin(),(a).end()
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b))
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
using namespace std;

int binarysearch(int arr[],int size ,int key){
int start =0;
int end =size-1;
int mid=(start+end)/2;
while (start<=end)
{
if (arr[mid]==key)
{
return mid;
}
else if (arr[mid]<key)
{
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;
}
return -1;
 }

int factorial(int n) {
if(n > 1)
 return n * factorial(n - 1);
else
return 1;
}

int main(){
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
    ll n;
    cin>>n;
    ll sum=0,v=0;
    ll arr[n],p=0;
    for (int i = 0; i < n; i++)
    {
        cin>>arr[i];
        sum+=arr[i];
    }
    for (int i = 1; i < n; i++)
    {
        if (arr[i-1]==-1 && arr[i]==-1)
        {
            
            v=1;
            break;
        }
        else if (arr[i]==-1 || arr[i-1]==-1)
        {
            p=1;
        }
        else
        {
            v=0;
        }
        
        
    }
    if (v==1)
    {
        cout<<sum+4<<endl;
    }
    else if (p==0)
    {
        cout<<sum-4<<endl;
    }
    else
    {
        cout<<sum<<endl;
    }
    
    
}
    return 0;
}


Comments

Submit
0 Comments
More Questions

450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General