Maximum Angriness Problem of CodeChef

 This is a code to the mentioned problem statement but it doesn't pass all the Test Cases.

Basically the logic that I have understood is that the no. of swaps that we have, we will spend all of them to get the max elems at the beginning, bcz that will be most beneficial and then we just have to consider 3 things, max elems at front, elems at mid(which are untouched) and lastly the elems at the end. I have calculated this many times and think that I am considering all the possible scenarios but still can't figure out what is going wrong in this code, at first I had thought that there maybe some issue with the range of long in cpp, so I tried the same logic in Python but I was wrong, the problem still exists. 

So, I will be trying to figure this out tomorrow and hopefully will get something concrete.

Cya...

#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
// int foo(int n){
//     if(n==1){
//         return 1;
//     }
//     //  Will return the sum till 1 from any number
//     return n+foo(n-1);
// }

// The below implementation is an optimized version of the above function
int foo(int n){
    return n*((n+1)/2.0);
}

int bar(int max,int swaps){
    // int ans=0;
    // while(swaps>0){
    //     ans+=max-swaps;
    //     swaps--;
    // }
    // cout<<"The ans inside the bar function is "<<ans<<endl;
    // return ans;
    
    // Instead of the above lines of code we can replace them with just a single line of code as shown below
    return foo(max-1)-foo(max-1-swaps);
}
int main() {
    int T;
    cin>>T;
    while(T>0){
        
        int n;
        int minutes;
        cin>>n>>minutes;
        if(n==1){
            cout<<0<<endl;
            T--;
            continue;
        }
        // int minutes;
        // cin>>minutes;
        
        cout<<((minutes>=floor(n/2))?foo(n-1):bar(n,minutes)+foo(minutes-1)
        +minutes*(n-minutes*2))<<endl;
   
        T--;
    }
    
    return 0;
}
// Will perform the same thing in Python later.
#include<iostream> #include<math.h> #include<vector> using namespace std; // int foo(int n){ // if(n==1){ // return 1; // } // // Will return the sum till 1 from any number // return n+foo(n-1); // } // The below implementation is an optimized version of the above function long foo(long n){ return n*((n+1)/2.0); } long bar(long max,long swaps){ // int ans=0; // while(swaps>0){ // ans+=max-swaps; // swaps--; // } // cout<<"The ans inside the bar function is "<<ans<<endl; // return ans; // Instead of the above lines of code we can replace them with just a single line of code as shown below return foo(max-1)-foo(max-1-swaps); } int main() { int T; cin>>T; while(T>0){ long n; long minutes; cin>>n>>minutes; if(n==1){ cout<<0<<endl; T--; continue; } // int minutes; // cin>>minutes; cout<<((minutes>=floor(n/2))?foo(n-1):bar(n,minutes)+foo(minutes-1)+minutes*(n-minutes*2))<<endl; T--; } return 0; }
// Python Approach
from math import floor as fl
def foo(n):
    return n*((n+1)/2)
def bar(n,swaps):
    return foo(n-1)-foo(n-1-swaps)
def main():
    # for _ in range(5):
    #     print()
    T=int(input())
    while(T):
        n,s=map(int,input().split())
        if(n==1 or s==0):
            print(0) 
            T-=1
            continue
        print(int(foo(n-1))) if s>=fl(n//2) else print(int(foo(s-1))+int(bar(n,s))+int(s*(n-(s*2))))        
        T-=1
    # print(foo(7))
if __name__=="__main__":
    main()
    

Comments

Popular Posts