INVYCNT October Lunchtime Help

hey bro, can you please have a look at my code too ? I have checked the above test case which you gave for the OP. please.

https://www.codechef.com/viewsolution/27577362
Here is my solution if anyone want explaination , i will @vishalg8454
PS : Read from line 329 to 349

1 Like

Thank you , I will be waiting…

should i explain right now?

yes bro, but I will likely be unable to get good sleep if I am not able to find why i got WA ?:sob:

int main(){
fastIO
lli t=1;
cin>>t;
while(t--){
lli n,k;
cin>>n>>k;
lli a[n];
scanarr(a,n);
lli sum=0;
for(lli i=0;i<n;i++){
    lli age=0,piche=0;
    for(lli j=i-1;j>=0;j--){
        if(a[i]>a[j])
            piche++;
    }
    for(lli j=i+1;j<n;j++){
        if(a[i]>a[j])
            age++;
    }
    sum+=(age*((k*(k+1))/2))+(piche*((k*(k-1))/2));
}
cout<<sum<<endl;
}

The test case this fails for:
1
5 5
1 2 3 1 1
Expected output: 100
Your output: 90

Thank you, there can’t be any variable name self explanatory than this :joy::+1::+1:

1 Like

Can you explain that formula which you have used to calculate the answer? I am not getting why that formula is used and why the inversions is calculated from both side. Please explain the solution in detail!

Ah, finally I could sleep. Thank you so much !

count for each element such that for how many values it is greater than previous one ( i called them " piche " ) , similarly , count for each element such that for how many values it is greater than next one ( i called them " aage " )
Now for next values count (aage walo ke liye) total values is age*((k*(k+1))/2)
and for prev values count (piche walo ke liye) total values is piche*((k-1)*(k)))/2)
@anon20971295

Ah, I figured out the pattern with copy and pen :sweat_smile: probably which would be difficult to explain for me as well. Also my solution is incorrect so need to work on it!

https://www.codechef.com/viewsolution/27594229
this is my solution and it fails the test case you have given but i want to know that how can i know that which test case my code is failing

Sorry bro, everything is correct. Actually I had the data type as int instead of long int so the problem!:sweat_smile: Thanks you guys for the help @ssrivastava990

here is my AC code
#include
#include<bits/stdc++.h>
#include
#include<math.h>
#define int long long
#define foi(n) for(int i=0;i<n;i++)
using namespace std;
//??UVAISH ZAFRI??******

main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test;
cin>>test;
while(test–)
{
int n,k;
cin>>n>>k;
int a[n];
foi(n)
cin>>a[i];
int count=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
if(a[i%n]>a[j%n])
count+=1;
}
if(k==1)
cout<<count<<endl;
else{
//count for one
int b[n];
for(int i=0;i<n;i++){int tempcount=0;
for(int j=0;j<n;j++)
{
if(i!=j&&a[i]>a[j])
tempcount+=1;
}
b[i]=tempcount;
//cout<<b[i];
}
int tempcount=0;
tempcount=accumulate(b,b+n,0);
int res=count;
while(k>=2){res+=(count+(k-1)*tempcount);k–;}
cout<<res<<endl;

  }
 }
return 0;

}

What about this code? Please correct me if I have implemented your logic in a wrong way.
https://www.codechef.com/viewsolution/27595764

Here is my solution, You can extract logic by going through once.
https://www.codechef.com/viewsolution/27592531

This is one of my friend’s accepted solution which gives the answer of above test case as 90.
https://www.codechef.com/viewsolution/27589557
Please look into the case.

Here is my solution, i am getting wrong answer.
Please help me to know where i’m wrong?
https://www.codechef.com/viewsolution/27582475

A brute force solution will work here easily.
In this, we maintain the count of the number to the left and to the right which are greater then a[i] for each i
for lefts we will have a
left * (k-1 + k-2 + k-3 + … + 1) series
for rights we will have a
right * (k + k-1 + k-2 + k-3 + … + 1)

Solution