1738B - Prefix Sum Addicts - CodeForces Solution


constructive algorithms math sortings

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    a=[int(i) for i in input().split()]
    b=[int(i) for i in input().split()]
    c=[]
    if a[1]==1:
        print("YES")
        continue
        for i in range(1,a[1]):
        c.append(b[i]-b[i-1])
        if c!=sorted(c,reverse=False):
        print("NO")
        continue
    if c[0]*(a[0]-a[1]+1)>=b[0]:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i=a; i<n ; i++)
#define ll long long
using namespace std;

void solve()
{
    int n,k;
    cin>>n>>k;
    int a[k];
    for(int i=0;i<k;i++){
        cin>>a[i];
    }
    vector<int>v;
    for(int i=k-1;i>0;i--){
        int x=a[i]-a[i-1];
        v.push_back(x);
    }
   
    int x=v[v.size()-1];
   
    
    

    int space=n-k+1;
    int x1=abs(a[0]);
    int m= x1/space;
    int l=x1%space;
    if(a[0]<0){
        // cout<<space<<" " <<x1<<endl;
        while(space--){
            v.push_back(-m);
        }
        int i=n-1;
        while(l-- && i>=0){
            v[i]-=1;
            i--;
        }

    }
    else{
        int i=v.size();
      while(space--){
            v.push_back(m);
      }
      while(l-- && i<v.size()){
        v[i]+=1;
        i++;

      }
    }
     reverse(v.begin(),v.end());
    // for(auto x:v){
    //     cout<<x<<" ";
    // }
for(int i=0;i<n-1;i++){
    if(v[i+1]<v[i]){
        cout<<"No"<<endl;
        return;
    }
}
cout<<"Yes";
    cout<<endl;


}

int main(){

   ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   int t;
   cin>>t;
   while(t--)
   {
      solve();
   }
   return 0;
}


Comments

Submit
0 Comments
More Questions

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
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence