您的位置:首页 > 其它

BZOJ 2761: [JLOI2011]不重复数字( )

2015-05-25 09:57 399 查看


电脑课来刷刷水题....这道题我居然WA 和 PE 了这么久.....不想活了..

这道题set , hash , sort ,平衡树 什么的都可以搞吧..

用set没有氧气优化好像会很慢的样子...但BZOJ好像有..

-------------------------------------------------------------------------------------------

#include<cstdio>#include<cstring>#include<algorithm>#include<set>#include<vector>#include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i )#define clr( x , c ) memset( x , c , sizeof( x ) ) using namespace std; const int maxn = 50000 + 5; set< int > MAP;vector< int > ans; int main() { int t; cin >> t; while( t-- ) { int n; cin >> n; MAP.clear(); ans.clear(); while( n-- ) { int x; scanf( "%d" , &x ); if( MAP.find( x ) == MAP.end() ) { MAP.insert( x ); ans.push_back( x ); } } int sz = ans.size(); rep( i , sz ) { printf( "%d" , ans[ i ] ); if( i != sz - 1 ) printf( " " ); } printf( "\n" ); } return 0;}

-------------------------------------------------------------------------------------------



从没写过hash...自己YY了一个写一下...

mod的值取得太小或太大都不行...

-------------------------------------------------------------------------------------

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i )#define clr( x , c ) memset( x , c , sizeof( x ) ) using namespace std; const int mod = 1000000; vector< int > hash[ mod ];vector< int > ans; void init() { rep( i , mod ) hash[ i ].clear(); ans.clear(); } inline void insert( int X ) { int x = X; while( x < 0 ) x += mod; hash[ x % mod ].push_back( X ); } inline bool find( int X ) { int x = X; while( x < 0 ) x += mod; x %= mod; rep( i , hash[ x ].size() ) if( X == hash[ x ][ i ] ) return true; return false; } int main() { int t; cin >> t; while( t-- ) { init(); int n; cin >> n; while( n-- ) { int x; scanf( "%d" , &x ); if( ! find( x ) ) { insert( x ); ans.push_back( x ); } } int sz = ans.size(); rep( i , sz ) { printf( "%d" , ans[ i ] ); if( i != sz - 1 ) printf( " " ); } printf( "\n" ); } return 0; }

-------------------------------------------------------------------------------------

2761: [JLOI2011]不重复数字

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 2237 Solved: 863
[Submit][Status][Discuss]

Description

给出N个数,要求把其中重复的去掉,只保留第一次出现的数。例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。

Input

输入第一行为正整数T,表示有T组数据。接下来每组数据包括两行,第一行为正整数N,表示有N个数。第二行为要去重的N个正整数。

Output

对于每组数据,输出一行,为去重后剩下的数字,数字之间用一个空格隔开。

Sample Input

2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6

Sample Output

1 2 18 3 19 6 5 4
1 2 3 4 5 6

HINT

对于30%的数据,1 <= N <= 100,给出的数不大于100,均为非负整数;

对于50%的数据,1 <= N <= 10000,给出的数不大于10000,均为非负整数;

对于100%的数据,1 <= N <= 50000,给出的数在32位有符号整数范围内。

提示:

由于数据量很大,使用C++的同学请使用scanf和printf来进行输入输出操作,以免浪费不必要的时间。

Source

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: