代码改变世界

NYOJ 528 位运算 STL

2012-04-21 17:20  javaspring  阅读(132)  评论(0编辑  收藏  举报

     这道题基本上算是水题了,主要卡的是内存。我是用STL中的map写的,跑了1300多ms,代码太搓了。主要就是用map浪费时间了,要一直删除,所以浪费时间了。后来才知道原来这道题可以用位运算,一直采用异或操作。因为0异或y偶数次的话还是0,异或y奇数次的话是y,所以可以利用这个性质。主要这道题大概花100多ms就可以了,内存也不超,因为根本不用开数组。

    因为没用位运算写,所以只贴个我写的搓代码吧。。。。。。

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int main(){
	int n;
	while(~scanf("%d",&n)){
	  map<int,int> mp;
	  map<int,int> ::iterator iter;
	  int x;
	  while(n--){
	    scanf("%d",&x);
		mp[x]++;
		if(mp[x]%2==0)
			mp.erase(x);
	  }
	  iter=mp.begin();
	  printf("%d\n",iter->first);
	}
	return 0;
}