UVA 11039(p78)----Building designing

#include<bits/stdc++.h>
#define debu
using namespace std;
const int maxn=5*1e5+50;
int a[maxn];
int b[maxn];
int n,num1,num2;
void solve()
{
    int ans=0,pos,flag,now;
    sort(a,a+num1);
    sort(b,b+num2);
    if(a[0]<b[0])
    {
        flag=1;
        ans++;
        now=a[0];
    }
    else
    {
        flag=0;
        ans++;
        now=b[0];
    }
    while(1)
    {
        if(flag)
        {
            pos=upper_bound(b,b+num2,now)-b;
            if(pos==num2) break;
            now=b[pos];
            ans++;
            flag=!flag;
        }
        else
        {
            pos=upper_bound(a,a+num1,now)-a;
            if(pos==num1) break;
            now=a[pos];
            ans++;
            flag=!flag;
        }
    }
    printf("%d\n",ans);
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        num1=num2=0;
        for(int i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            if(x<0) a[num1++]=-x;
            else b[num2++]=x;
        }
        solve();
    }
    return 0;
}

题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1980

题解:贪心。将小于0与大于0分别从小到大排序,每次分别从两个序列中选择尽量小的数,注意同一序列不可连续选。

你可能感兴趣的:(UVA 11039(p78)----Building designing)