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

 

转载于:https://www.cnblogs.com/xuesu/p/10442692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值