1705B - Mark the Dust Sweeper - CodeForces Solution


constructive algorithms greedy implementation *900

Please click on ads to support us..

Python Code:

t = int(input())
ans = []
for i in range(t):
    n = int(input())
    a = list(map(int,input().split(" ")))
    count = 0
    for i in range(n-1):
        if a[i+1]  == 0 and a[i] != 0:
            a[i] -= 1
            a[i+1] += 1
            count += 1
    for i in range(n-1):
        count += a[i]
    ans.append(count)    


for i in ans:
    print(i)        

C++ Code:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;
#define PII pair<int,int>
#define LL long long 
#define PLL pair<LL,LL>
#define N 200010
const LL INF = 1e18;
LL t;
LL a[N],n;
void solve()
{
    cin>>n;
	LL ans=0;
	for(int i=1;i<=n;i++)cin>>a[i],ans+=a[i];
	ans-=a[n];
	LL pre=0;
	LL i=1;
	while(a[i]==0  &&i<=n)i++;
	LL cnt=0;vector<LL>wz(n+3);
	for(int j=n-1;j>=i;j--)
	{
		if(a[j]==0)cnt++;
	}
	cout<<ans+cnt<<endl;
}
int main()
{
    cin>>t;
    while(t--)solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction