ODDSORT and ANTS on the INOI server

Has anybody solved these two? If so, how?

1 Like


Oddsort is a tricky problem. Note that a certain subsequence of numbers would not be moved at all, and the rest would be moved relative to it. Once you’ve thought this far, the solution is easy. Two observations — the subsequence that is fixed has to be in an increasing order. Secondly, the optimal solution is achieved when the sum of this subsequence is maximum. You can also prove this somewhat informally.

Ants is actually a problem from a SRM (446), namely AntsOnGraph. The solution is available at http://apps.topcoder.com/wiki/display/tc/SRM+446.



Thanks for your help. The topcoder link doesn’t seem to work though. I logged in but the server failed with a null pointer exception… If it works for you, could you take a screenshot/ paste the text somewhere? Edit: nevermind, I got it, nice problem with an interesting solution.

Indeed. In fact, after solving that, I found a few more interesting problems that relied on fun uses of the exponentiation by squaring idea for efficient solutions. Cheers!

Care to share them?

See http://hsin.hr/coci/archive/2012_2013/contest4_tasks.pdf, DLAKAVAC.

It seems that huge numbers are a giveaway for this sort of thing.

I have solved the ODDSORT Problem. Its pretty easy… Here’s my C++ Source Code…I got a 100:

#include “iostream”
#include “algorithm”

using namespace std;

int main()

int n, sum=0;
cin >> n;
int x[n+1], y[n+1];
for (int i = 1; i<=n; i++)
	cin >> x[i];
sort (y+1, y+n+1);
int mf[n+1][n+1];
for (int j = 0; j<=n; j++)

for (int p = 1; p<=n; p++)
for (int q = 1; q<=n; q++)
	if (x[p]==y[q])
	mf[p][q]=max(mf[p-1][q], mf[p][q-1]);

cout << sum;
return 0;


I read ANT’s prob. statement and I think I can solve that also…Please wait for no more than 3 hours!!! I’ll upload the solution…

No worries (or hurry), as you can see I’ve already got the solution from idraumr. (got both accepted as well)

A repeated operation to be performed for a specific large number of times seems to be the “gist” of it.

ok @superty!!! I was jst about to upload my source code…

and u dont hv to stress on the word HURRY every time!!!


@idraumr @superty although a complexity of O(N^2) is enough for the ODDSORT problem, do you know an algorithm with an O(NlogN) or O(N) complexity?

Yes. http://paste.ubuntu.com/9720318/
We can augment that code with segment trees.

Here’s the accepted NlogN code: http://paste.ubuntu.com/9720712/

@superty…pls provide ur soln. to ants…mine is not working for only one testcase!!!

There is an official solution available. Click on statement and download the attached .cpp file.