題意:
有n個絕對值不同的整數(非0),選出儘量多的數,排列成一個序列,正負相間且元素絕對值遞增,求最長的序列的長度.
分析:
可以先存下來接着使用nlogn的排序也可,這裏用了hash的基數排序
Code:
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define PLUS 1
#define SUB 2
#define ALL 3
#define DIR 4
#define DIM 2
#define STATUS 2
#define MAXN 999999 + 10
#define oo (~0u)>>1
#define INF 0x3F3F3F3F
#define REPI(i, s, e) for(int i = s; i <= e; i ++)
#define REPD(i, e, s) for(int i = e; i >= s; i --)
static const double EPS = 1e-5;
int f[MAXN];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas, n;
scanf("%d", &cas);
REPI(k, 1, cas) {
scanf("%d", &n);
int v, max_v = 0;
memset(f, 0, sizeof(f));
REPI(i, 1, n) {
scanf("%d", &v);
if( v > 0 ) {
f[v] = PLUS;
}
else {
f[-v] = SUB;
}
max_v = max(max_v, abs(v));
}
int len = 0;
int idx = 1;
int pre = 0;
while( idx <= max_v && !f[idx] ) {
idx += 1;
}
pre = (PLUS == f[idx])? SUB : PLUS;
while( true ) {
if( idx > max_v ) {
break;
}
while( idx <= max_v && !f[idx] ) {
idx += 1;
}
if( (PLUS == pre && SUB == f[idx]) ) {
len += 1, pre = SUB;
}
else if( (SUB == pre && PLUS == f[idx]) ) {
len += 1, pre = PLUS;
}
idx += 1;
}
printf("%d\n", len);
}
return 0;
}
uva_11039 - Building designing( 基數排序 )
最新推荐文章于 2017-08-22 16:02:12 发布