1742E - Scuza - CodeForces Solution


binary search greedy math

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
  n, q = map(int, input().split())
  height = [int(i) for i in input().split()]
  question = [int(i) for i in input().split()]
  
  sort_question = []
  for i in range(q):
    sort_question.append([question[i], i])
  
  sort_question = sorted(sort_question, key = lambda x: (x[0], x[1]))
  
  ans = [0] * q
  cur_step = 0
  sum = 0
  for item in sort_question:
  	question_height = item[0]
  	question_index = item[1]
  	while cur_step < n and (question_height >= height[cur_step]):
  		sum += height[cur_step]
  		cur_step += 1
  	ans[question_index] = sum

  print(*ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define fastIO  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define ll long long
#define max(a,b) (((b)>(a)) ? b : a )
#define min(a,b) (((a)<(b)) ? a : b)
#define endl "\n"
#define space " "

long long binarySearch(long long key, long long mouse[], long long s, long long e)
{
    while(s<=e)
    {
        ll mid=(s+e)/2;

        if (mouse[mid]==key)          
            return mid;
        else if (mouse[mid]>key)      
            e=mid-1;
        else                        
            s=mid+1;
    }
    return -1;

    //O(logn), O(1)
}

long long gcd(long long a, long long b)
{
    return b == 0 ? a : gcd(b, a % b);
    //O(log(min(a,b)), O(log(min(a,b))
}

long long LCM(ll a, ll b)
{
    return a*b/gcd(a,b);
}

//---------------------------------------------------------------------------------------------------//

void solve()
{
    
    ll n,q;
    
    cin>>n>>q;
    pair<ll,ll> arr[n]; //index of maximum step before current index
    pair<ll,ll> question[q];
    
    arr[0].second=0;
    for(ll i=0;i<n;i++)
    {
        int temp;
        cin>>temp;
        arr[i].first=temp;
        
        if (i-1>=0)
        {
            arr[i].second=arr[i-1].second;
            if(temp>arr[arr[i-1].second].first)
            {
                arr[i].second=i;
            }
        }
    }
    
    for(ll i=0;i<q;i++)
    {
        ll temp;
        cin>>temp;
        question[i].first=temp;
        question[i].second=i;

    }

    // for(ll i=0;i<q;i++)
    // {
        
    //     ll legsLength=question[i].first;
        
    //     ll currentHeight=0;
    //     for(ll j=0;j<n;j++)
    //     {
    //         if (legsLength<arr[j])
    //         {
    //             break;
    //         }
    //         currentHeight+=arr[j];
            
    //     }
    //     cout<<currentHeight<<space;
    // }
    // cout<<endl;


    sort(question, question+q);
    ll answer[q];
    
 
    
    ll legsLength=question[q-1].first;
    ll currentHeight=0;
    ll currentStepIndex=0;
    
    for(ll i=0;i<n;i++)
    {
        if (legsLength<arr[i].first)
            break;
        currentHeight+=arr[i].first;
        currentStepIndex=i;
    }

    answer[question[q-1].second]=currentHeight;

    ll maxStepIndexUntilNow=arr[currentStepIndex].second;
    for(ll i=q-2;i>=0;i--)
    {
        legsLength=question[i].first;

        if (legsLength==0)
        {
            answer[question[i].second]=0;
            continue;
        }
        if (currentStepIndex>=0)
            maxStepIndexUntilNow=arr[currentStepIndex].second;
        while(maxStepIndexUntilNow>=0 && legsLength<arr[maxStepIndexUntilNow].first)
        {
            while(currentStepIndex>=0 && currentStepIndex>=maxStepIndexUntilNow)
            {
                currentHeight=currentHeight-arr[currentStepIndex].first;
                currentStepIndex--;
            }
            if (currentStepIndex>=0)
                maxStepIndexUntilNow=arr[currentStepIndex].second;
            else
                maxStepIndexUntilNow=-1;
        }

        answer[question[i].second]=currentHeight;
    }


    for(ll i=0;i<q;i++)
    {
        cout<<answer[i]<<space;
    }
    cout<<endl;
    

}

int main()
{
    fastIO;
    ll t;
    cin>>t;
    while(t--)
    {
        
        solve();
        

    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing