Manas Mishra’s Post

View profile for Manas Mishra, graphic

Specialist (1551) @Codeforces | USACO Tutor | React Developer | DM for C++ and DSA Lessons

CP experts your help Needed! Le: CF casually teaching me that same time complexity notation doesn't result in the same runtime 😕 Link to the problem :- https://lnkd.in/dqgDCnFb Idea to the solution: Iterate through each university one by one and calculate its contribution (cumulative strength) to every k from 1 to n. (observation: if the students in University are x ,it can only contribute to k from 1 to x.) Implementation 1: https://lnkd.in/dmUH-yRs -> Used an ordered map (int to vector<int>) to store universities and their corresponding student's strength. Complexity: O(nlogn) -> sorted all university vectors in reverse order [O(nlogn)] -> maintained suffix sum array( prefix sum array can also be maintained with sorting in no decreasing order) to calculate contribution for any university for any k [O(n)] ->Then Iterated through each vector and for every k i calculated the contribution in O(1) time for 1≤k≤n [O(n)] Total Time complexity: O(nlogn) result : TLE Implementation 2: https://lnkd.in/d336dfNg -> Rather than using a map stored in uni and students as a pair in a vector v. [O(n)] -> Sorted the vector v with help of custom comparator fn ,such that it maintains ranges of students of same universities one by one in a decreasing fashion. (O(nlogn) -> then constructed the prefix sum subArrays this time in the vector v for every university O(n) -> Now again Iterated through v for every university and calculated its contribution for every k O(n) total time complexity: O(nlogn) result: accepted (249ms) My idea is that the second approach doesn't use a map and it saves time but still I'm not quite sure about why the first one gave TLE. Summoning all cp enthusiasts and experts to help me to figure out what could be the exact reason that the first code didn't get ac but second one did? tagging some cp gods for help : Pranav Mehta (snippet v sir ka hi hai 😉) Aryan C. Badal Kumar Kartik Singh Kushwah #codeforces #cp

  • graphical user interface, text, application
Manas Mishra

Specialist (1551) @Codeforces | USACO Tutor | React Developer | DM for C++ and DSA Lessons

6mo

Ps: The issue is resolved thanks Badal Kumar for debugging my silly mistake, it was this statement where I was creating a vector 'a' for every k rather being it in the outer loop I put it in the inside loop.

Badal Kumar

SDE + Associate Instructor @ NST || Expert (1800) @ CodeForces || 5 🌟 (2030) @ CodeChef || Knight at LeetCode

6mo

The bug was in the line where you were creating vector a again and again in that for loop

  • No alternative text description for this image

Thanks for reminding me. Have to start this again..

Like
Reply
See more comments

To view or add a comment, sign in

Explore topics