363B - Fence - CodeForces Solution


brute force dp *1100

Please click on ads to support us..

Python Code:

l = input().split()
n, k = int(l[0]), int(l[1])
arr = [int(x) for x in input().split()]

s = [0] * n
for i in range(n):
    s[i] = arr[i] if i == 0 else arr[i] + s[i-1]

ans = -1
min_s = float('inf')
for i in range(k-1, n):
        v = 0 if i - k < 0 else s[i-k]
    if s[i] - v < min_s:
        min_s = s[i] - v
        ans = i-k+2
print(ans)

C++ Code:

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
using namespace std;
int arr[200005];
int main() {
    fast;
ll n,k,idx,sum,temp,t,l;
cin>>n>>k;
vector<ll>v;
ll x;cin>>x;
v.push_back(x);
    for (int i = 1; i <n ; ++i) {
     cin>>x;
     v.push_back(v[i-1]+x);
    }
    t=l=k;
    l-=1;
    sum=v[l];
    idx=l;
    for (ll i = t; i < n; ++i) {
       temp=v[i]-v[i-k];
       if(temp<sum){
           sum=temp;
           idx=i;

       }
    }
    idx-=k;
    idx+=1;
cout<<idx+1<<"\n";
}


Comments

Submit
0 Comments
More Questions

Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people