Question Name:Puchi and Luggage

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 100005;
const int MAX1 = 1000005;
int T,N;
int Weight[MAX],Frequency[MAX1],Aux[MAX],Copied[MAX],F[MAX1];
ll inv;
void mergeFunc(int left,int mid,int right)
{
    int p=left,q=mid+1,k=0,coun=0;
    for(int i=left;i<=right;i++)
    {
        if(p>mid)
        {
            Aux[k++] = Weight[q++];
        }
        else if(q>right)
        {
            Aux[k++] = Weight[p];
            Frequency[Weight[p]] += coun;
            p++;
        }
        else if(Weight[p] <= Weight[q])
        {
            Aux[k++] = Weight[p];
            Frequency[Weight[p]] += coun;
            p++;
        }
        else
        {
            Aux[k++] = Weight[q++];
            inv += mid-p+1;
            coun++;
        }
    }
    for(int i=0;i<k;i++)
    {
        Weight[left++] = Aux[i];
    }
}
void mergeSort(int left,int right)
{
    if(left<right)
    {
        int mid = (left+right)/2;
        mergeSort(left,mid);
        mergeSort(mid+1,right);
        mergeFunc(left,mid,right);
    }
}
int main()
{
   // freopen("input10.txt","r",stdin);
    //freopen("output10.txt","w",stdout);
    for(scanf("%d",&T);T;--T)
    {
        inv =0 ;
        for(int i=0;i<MAX1;i++)
        Frequency[i] = 0;
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%d",&Weight[i]);
            Copied[i] = Weight[i];
        }
        mergeSort(0,N-1);
        ll sum = 0;
        for(int i=0;i<N;i++)
        {
            if(i != N-1)
            printf("%d ",Frequency[Copied[i]]);
            else
            printf("%d\n",Frequency[Copied[i]]);
        }
    }
    return 0;
}
  • Problem Description
    Puchi hates to carry luggage, but unfortunately he got a job to carry the luggage of his N friends in office. Each day, one of his N friends, gives him the luggage of a particular weight to carry. You will be given the weight of luggage of each friend in the array Weight, where Weight i is the weight of luggage of ith friend carried by Puchi on ith day. It is given that all the luggages carried by Puchi are distinct in their weights. 

    As Prateek assigned this job to Puchi, so for each day, he wants to know the number of days in future when Puchi will have to carry the luggage , having weight less than the weight of luggage of current day.
    Please help Prateek for the same.

    Input:
    The first line contains a single integer T, denoting the number of test cases. In each test case, the following input will be present: 
    First line contains an integer N, where N represents the number of friends.
    Next N line contains N integers, where ith line contains ith integer, which represents Weight i.

    Output:
    Output exactly T lines. Each line contains N integer separated by a space, where ith integer represents the number of luggage of future, which are less than the weight of luggage of the current day.

    Constraints:

    Subtask 1:
    1 <= T <= 30
    1<= N <= 10^4
    1<= Weight i <= 10^6 

    Subtask 2:
    1 <= T <= 10
    1<= N <= 10^5
    1<= Weight i <= 10^6
  • Test Case 1
    Input (stdin)1
    4
    2
    1
    4
    3
    Expected Output1 0 1 0
  • Test Case 2
    Input (stdin)1
    3
    7
    4
    9
    Expected Output1 0 0

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro