1 / 4
Oct 2012

http://www.spoj.pl/problems/OLOLO/

plz help the is 10th time i tried to optimize this code

should be AC cry

#include <iostream>
using namespace std;
int main()
{
	unsigned char* A= new unsigned char [125000000];
	unsigned char m=1;
	int t,I;
	unsigned long X;
	unsigned long long b=0;
	cin>>t;
	for(int i0=0;i0<t;++i0)
	{
		cin>>X;
		m=1<<(X%8);
		if(A[X/8] & m) { A[X/8]-=m; b-=X; } // if X apeared before
		else { A[X/8]+=m; b+=X; }
	}
	printf("%llu",b);
    return 0;
}

i tried to take 10^9 bit (125000000 unsigned char) that each bit denote to decimal num from 1 to 10^9.
i make bit(X) = 1 if X apear 1st time ( b increased by X), and =0 if 2ed (b decreased by X) .

thanks

  • created

    Oct '12
  • last reply

    Oct '12
  • 3

    replies

  • 194

    views

  • 2

    users

  • 1

    link

Never use cin or cout, use scanf. Even at that this will probably time out. The problem setter is looking for a very specific solution to this problem. Really think about what is going on in this problem. If you had taken discrete mathematics then that will help you.

specific solution ! there is over than 1000 user .
Is take size in Heap (new) take along time ??
thanks leppy

Yes heap will be too slow. An array will even be too slow. Spend a lot of time thinking about the problem and simulating by hand. When you get it, it will be very obvious.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 15 8d

Want to read more? Browse other topics in C and C++ or view latest topics.