UVa 11039 - Building designing 贪心,水题 难度: 0

题目

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


题意

n个数,要求正负相间,绝对值增长,问n个数能组成的这样数列最长多长

 

思路

明显,分成正负两组,挨个在两组内取最小,直到不能取就行

 

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair;
const int MAXN = 5e5 + 1;
int a[MAXN];
int b[MAXN];

int main() {
#ifdef LOCAL_DEBUG
    freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
    //freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
    int n;
    int T;
    scanf("%d", &T);
    for (int ti = 1; ti <= T && scanf("%d", &n) == 1; ti++) {
        int acnt = 0;
        int bcnt = 0;
        for (int i = 0; i < n; i++) {
            int tmp;
            scanf("%d", &tmp);
            if (tmp > 0)a[acnt++] = tmp;
            else b[bcnt++] = -tmp;
        }
        sort(a, a + acnt);
        sort(b, b + bcnt);
        int ans = 0;
        int start = 0;
        int astart = 0;
        int bstart = 0;
        if (astart < acnt && bstart < bcnt && b[bstart] < a[astart]) {
            ans = 1;
            bstart++;
        }
        while (true) {
            while (bstart && astart < acnt && a[astart] < b[bstart - 1]) {
                astart++;
            }
            if (astart < acnt) {
                ans++;
                astart++;
            }
            else {
                break;
            }
            while (astart && bstart < bcnt && a[astart - 1] > b[bstart]) {
                bstart++;
            }
            if (bstart < bcnt) {
                ans++;
                bstart++;
            }
            else {
                break;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}
View Code

 

posted @ 2019-02-27 11:45  雪溯  阅读(78)  评论(0编辑  收藏  举报