1741C - Minimize the Thickness - CodeForces Solution


brute force math two pointers

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n=int(input())
    lis=list(map(int,input().split()))
    ans=n
    s=sum(lis)
    s_sum=0
    for i in range(n):
        s_sum+=lis[i]
        ref=i+1
        if s%s_sum==0:
            cur_sum=0
            lower=0
            for j in range(i+1,n):
                cur_sum+=lis[j]
                lower+=1
                if cur_sum==s_sum:
                    cur_sum=0
                    ref=max(ref,lower)
                    lower=0
                elif cur_sum>s_sum:break
            if cur_sum==0:
                ans=min(ans,ref)
    print(ans)

C++ Code:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <string>
#include<math.h>
#include <bits/stdc++.h>
#include <map>
#define ll long long
const int mod=1e9+7;

using namespace std;

void solve(){
  ll n;
  cin>>n;
  ll a[n+1], s[n+1]={0};
  for(ll i=1; i<=n; i++){
    cin>>a[i];
    s[i]=s[i-1]+a[i];
  }
  ll p=0, ans=n, k, res, ok;
  for(ll i=1; i<=n; i++){
    k=i;
    res=i;
    p=0;
    ok=0;
    for(ll j=i+1; j<=n; j++){
      if(s[j]-s[i]>s[k]) break;
      if(s[j]-s[i]==s[k]){
        res=max(res, j-k);
        k=j;
        if(j==n){
          p = max(res, p);
          ok=1;
        }
      }
      if(ok) ans = min(ans, p);
    }
  }
  cout<<ans<<endl;
}

int main(){
  ll n;
  cin>>n;
  while(n--) solve();
}


Comments

Submit
0 Comments
More Questions

780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack