UVA 11039 Building Designing

【题意】有n个各不相同的非0的数,选出尽量多的数,排成一个排列,使得正负号交替,且绝对值递增。

【解题思路】贪心!

【AC代码】

//cpp try two pointers

#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
struct node{
   int val,flag;
   node(){}
   friend bool operator<(const node &a,const node &b){
        return a.val<b.val;
   }
}a[maxn];

int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i].val);
            a[i].flag = a[i].val>0?1:0;
            a[i].val = abs(a[i].val);
        }
        int l=1,ans=1;
        sort(a+1,a+n+1);
        int pos=1;
        while(l<=n){
            if(a[l].flag!=a[pos].flag){
                ans++;
                pos = l;
            }
            l++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(UVA 11039 Building Designing)