Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

Codeforcer's blog

By Codeforcer, history, 3 years ago, In English

I cant figure out reason for TLE, code seems to be O(n) with some small constants, any help would be appreciated.

Problem Link : https://codeforces.com/contest/1251/problem/C

Code

Thanks!

  • Vote: I like it
  • -1
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +4 Vote: I do not like it

For inputs like $$$\underbrace{9999...9}_{n/2}\underbrace{8888...8}_{n/2}$$$, the number of swapping steps needed is $$$n/2$$$ for each of the $$$8$$$ digits, so it takes $$$(n/2)^2 = O(n^2)$$$ steps in total. Your code seems to simulate each swapping operation step by step, and hence $$$O(n^2)$$$ time complexity.

»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Err... It seems that the code you wrote was much faster.
Sorry to disturb :(