zoukankan      html  css  js  c++  java
  • UVa 11039

    题目

    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
    ", ans);
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    mina之小小总结(标准的菜鸟级别,行家勿入)
    tomcat(就一句话,自己的日记性质)
    MINA转自itoyo
    java web(没含量的,百科上的)
    解决VS.NET 2008中aspx文件没有设计界面
    正则表达式教程
    [VB] Option Explicit
    sql自定義函數 包含遊標
    sql 中 null+others=?
    实用手机号、IP、身份证号、歌曲查询接口
  • 原文地址:https://www.cnblogs.com/xuesu/p/10442692.html
Copyright © 2011-2022 走看看