https://vjudge.net/problem/UVA-11039

题意:

有n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增。

思路:
正数存一个数组,负数存一个数组,排序后进行遍历即可。

 #include<iostream>
#include<algorithm>
#include<string>
using namespace std; const int maxn = + ; int n;
int a[maxn],b[maxn]; int main()
{
ios::sync_with_stdio(false);
//freopen("D:\\txt.txt", "r", stdin);
int T;
int x;
cin >> T;
while (T--)
{
cin >> n;
int cnt1 = , cnt2 = ;
for (int i = ; i < n; i++)
{
cin >> x;
if (x>) a[cnt1++] = x;
else b[cnt2++] = -x;
}
sort(a, a + cnt1);
sort(b, b + cnt2);
int ans = ;
int p1 = , p2 = ;
int flag,pre;
if (a[] < b[])
{
pre = a[];
flag = ;
}
else
{
pre = b[];
flag = ;
}
while (p1 < cnt1 && p2 < cnt2)
{
if (flag == )
{
while (p2 < cnt2 && b[p2] <= pre) p2++;
if (p2 >= cnt2) break;
pre = b[p2];
ans++;
flag = ;
}
else if (flag == )
{
while (p1 < cnt1 && a[p1] <= pre) p1++;
if (p1 >= cnt1) break;
pre = a[p1];
ans++;
flag = ;
}
}
cout << ans << endl;
}
}

UVa 11039 设计建筑物的更多相关文章

  1. UVA 11039 Building designing 贪心

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

  2. 贪心水题。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 ...

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

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

  4. UVA 11039 - Building designing(DP)

    题目链接 本质上是DP,但是俩变量就搞定了. #include <cstdio> #include <cstring> #include <algorithm> u ...

  5. UVa 11039 - Building designing

    题目大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 分析:按照绝对值大小排一次序,然后扫描一次,顺便做个标记即可. #include<cstdio ...

  6. UVa 11039 (排序+贪心) Building designing

    白书上的例题比较难,认真理解样例代码有助于提高自己 后面的练习题相对简单,独立思考解决问题,增强信心 题意:n个绝对值各不相同的非0整数,选出尽量多的数排成序列,使得该序列正负交错且绝对值递增. 解法 ...

  7. uva 11039

    水题  排序 判符号 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...

  8. Building designing UVA - 11039

    先取正的和负的绝对值较小者为开头 .然后交替从正负数中取绝对值最小但比上一个大的. 证明: 1.开头选正负数中绝对值较小的:否则能再多放1个. 2.交替选的时候选最小的符合条件的:如果大的符合,换小的 ...

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

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

随机推荐

  1. XMind思维导图主题操作要点

  2. 使用客户端等远程连接mysql数据库

    1:  远程数据库(D1)数据: 数据库用户:root,数据库密码:root,数据库ip 内网地址  192.168.100.91,数据库端口 3306 本地主机:ip  192.168.127.1 ...

  3. 置换检验(Permutation Test)学习[转载]

    转自:https://www.cnblogs.com/bnuvincent/p/6813785.html http://www.bioinfo-scrounger.com/archives/564 1 ...

  4. 支持向量机:Numerical Optimization,SMO算法

    http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988419.html 另外一篇:http://www.cnblogs.com/vivouni ...

  5. java调用存储过程mysql

    在java中调用带返回值的存储过程的实现 直接上代码: DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ PROCEDURE `t ...

  6. UVM中的sequence使用(一)

    UVM中Driver,transaction,sequence,sequencer之间的关系. UVM将原来在Driver中的数据定义部分,单独拿出来成为Transaction,主要完成数据的rand ...

  7. VS2010/MFC编程入门之四十(文档、视图和框架:各对象之间的关系)

    前面一节中鸡啄米进行了文档.视图和框架的概述,本节主要讲解文档.视图.框架结构中各对象之间的关系. 各个对象之间的关系 文档.视图.框架结构中涉及到的对象主要有:应用程序对象.文档模板对象.文档对象. ...

  8. 跑道标识和那些复杂的灯光系统 and 简介、编号、参数、标志及数量 and 飞机跑道标准与参数

    http://www.360doc.com/content/16/0616/12/32670666_568219786.shtml http://news.carnoc.com/list/365/36 ...

  9. spring mvc interceptors

    <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:e ...

  10. MySQL数据库----视图

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...