题目

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 + ;
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 = ; ti <= T && scanf("%d", &n) == ; ti++) {
int acnt = ;
int bcnt = ;
for (int i = ; i < n; i++) {
int tmp;
scanf("%d", &tmp);
if (tmp > )a[acnt++] = tmp;
else b[bcnt++] = -tmp;
}
sort(a, a + acnt);
sort(b, b + bcnt);
int ans = ;
int start = ;
int astart = ;
int bstart = ;
if (astart < acnt && bstart < bcnt && b[bstart] < a[astart]) {
ans = ;
bstart++;
}
while (true) {
while (bstart && astart < acnt && a[astart] < b[bstart - ]) {
astart++;
}
if (astart < acnt) {
ans++;
astart++;
}
else {
break;
}
while (astart && bstart < bcnt && a[astart - ] > b[bstart]) {
bstart++;
}
if (bstart < bcnt) {
ans++;
bstart++;
}
else {
break;
}
}
printf("%d\n", ans);
} return ;
}

UVa 11039 - Building designing 贪心,水题 难度: 0的更多相关文章

  1. UVA 11039 Building designing 贪心

    题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量 ...

  2. UVa 10340 - All in All 水题 难度: 0

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

  3. UVa 3602 - DNA Consensus String 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  4. UVa LA 3213 - Ancient Cipher 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  5. 贪心水题。UVA 11636 Hello World,LA 3602 DNA Consensus String,UVA 10970 Big Chocolate,UVA 10340 All in All,UVA 11039 Building Designing

    UVA 11636 Hello World 二的幂答案就是二进制长度减1,不是二的幂答案就是是二进制长度. #include<cstdio> int main() { ; ){ ; ) r ...

  6. UVA 11039 - Building designing 水题哇~

    水题一题,按绝对值排序后扫描一片数组(判断是否异号,我是直接相乘注意中间值越界)即可. 感觉是让我练习sort自定义比较函数的. #include<cstdio> #include< ...

  7. UVa 11039 Building designing (贪心+排序+模拟)

    题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...

  8. UVa 10970 - Big Chocolate 水题 难度: 0

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

  9. UVa 11636 - Hello World! 二分,水题 难度: 0

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

随机推荐

  1. 力扣(LeetCode)728. 自除数

    自除数 是指可以被它包含的每一位数除尽的数. 例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0. 还有,自除数不允许包含 0 . 给定上边 ...

  2. JNI手动释放内存(避免内存泄露)

    . 哪些需要手动释放? 不要手动释放(基本类型): jint , jlong , jchar 需要手动释放(引用类型,数组家族): jstring,jobject ,jobjectArray,jint ...

  3. 围棋规则 - AlphaGO

    参考:4分钟了解围棋规则 看懂柯洁和AlphaGo的对决并不难 围棋规则: 1. 19X19的棋盘上有361个落子点: 2. 黑白棋子依次落子: 3. 比赛结束时,占地多者胜: 4. 上下左右相邻的棋 ...

  4. 最大似然估计实例 | Fitting a Model by Maximum Likelihood (MLE)

    参考:Fitting a Model by Maximum Likelihood 最大似然估计是用于估计模型参数的,首先我们必须选定一个模型,然后比对有给定的数据集,然后构建一个联合概率函数,因为给定 ...

  5. Shell Trap信号管理

    trap命令用于指定在接收到信号后将要采取的动作.常见的用途是在脚本程序被中断时完成清理工作.不过,这次我遇到它,是因为客户有个需求:从终端访问服务器的用户,其登陆服务器后会自动运行某个命令,例如打开 ...

  6. MVC,MVVM,MVP的区别/ Vue中忽略的知识点!

    按照顺序学习: https://scotch.io/courses/build-an-online-shop-with-vue/hello-world Vue Authentication And R ...

  7. 在Java、Web和移动开发方面最值得关注的12大开源框架

    在这篇文章中,我将分享一些值得开发者学习的优秀框架,以提高他们在移动开发.Web 开发以及大数据方面的开发技能. 1.AngularJS 这是一个JavaScript框架,我已经把它加入到我的2018 ...

  8. yield生成器

    def say_hi(): while True: print("before") ret0 = "返回值" msg = yield ret0 #yield后的 ...

  9. vue嵌套路由--params传递参数

    在嵌套路由中,父路由向子路由传值除了query外,还有params,params传值有两种情况,一种是值在url中显示,另一种是值不显示在url中. 1.显示在url中index.html <d ...

  10. 突破本地离线存储5M限制的JS库localforage简介

    http://www.zhangxinxu.com/wordpress/2018/06/js-localforage-localstorage-indexdb/