问题链接:UVA11039 Building designing。基础级练习题,用C++语言编写程序。
问题简述:输入n个绝对值各不相等的非0整数,选择尽量多的数,排成一个序列,使得正负交替并且绝对值递增。输出其最大长度。
问题分析:按照绝对值排序后,看一遍就可以了。
程序说明:(略)
AC的C++语言程序如下:
/* UVA11039 Building designing */ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int MAXN = 500000; int val[MAXN]; int cmp(int& a, int& b) { return abs(a) < abs(b); } int main() { int p, n, ans, i; scanf("%d", &p); while(p--) { scanf("%d", &n); for(i=0; i<n; i++) scanf("%d", &val[i]); sort(val, val+n, cmp); ans = 1; for(i=1; i<n; i++) { if((val[i-1] > 0 && val[i] < 0) || (val[i-1] < 0 && val[i] > 0)) ans++; } printf("%d ", ans); } return 0; }