先取正的和负的绝对值较小者为开头 。然后交替从正负数中取绝对值最小但比上一个大的。

证明:

1.开头选正负数中绝对值较小的:否则能再多放1个。

2.交替选的时候选最小的符合条件的:如果大的符合,换小的仍符合。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF,mod=;
int n,res;
struct nd{
int val;
nd(int a):val(a){}
bool operator<(const nd &rbs)const
{
return abs(val)>abs(rbs.val);
}
}; priority_queue<nd> pos,neg; void init()
{
for(;pos.size();pos.pop());
for(;neg.size();neg.pop());
cin>>n;
for(int i=;i<n;++i)
{
int tmp;
cin>>tmp;
if(tmp<)
{
neg.push(tmp);
}
else pos.push(tmp);
}
} void work()
{
res=;
if(pos.size()==||neg.size()==)return;
int pre;
bool prekind=;
int a=neg.top().val,b=pos.top().val;
if(abs(a)<abs(b))
{
prekind=;
pre=abs(a);
neg.pop();
}
else
{
prekind=;
pre=abs(b);
pos.pop();
}
for(;;)
{
if(prekind==)
{
for(;pos.size()!=;)
{
if(abs(pos.top().val)<pre)pos.pop();
else break;
}
if(pos.size()==)break;
else
{
++res;
prekind=;
pre=abs(pos.top().val);
pos.pop();
}
}
else
{
for(;neg.size()!=;)
{
if(abs(neg.top().val)<pre)neg.pop();
else break;
}
if(neg.size()==)break;
else
{
++res;
prekind=;
pre=abs(neg.top().val);
neg.pop();
}
}
}
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
cin>>casenum;
//cout<<casenum<<endl;
for(lon time=;time<=casenum;++time)
{
init();
work();
cout<<res<<endl;
}
return ;
}

Building designing UVA - 11039的更多相关文章

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

  2. UVA 11039 Building designing 贪心

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

  3. 11039 - Building designing

      Building designing  An architect wants to design a very high building. The building will consist o ...

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

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

  5. UVA 11039 - Building designing(DP)

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

  6. UVa 11039 - Building designing

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

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

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

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

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

  9. UVA 11039 - Building designing 水题哇~

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

随机推荐

  1. python ---16 初识面向对象

    面向对象 一 .面向对象和面向过程比较 ①面向过程:一切以事物的发展流程为核心    优点:负责的问题流程化,编写相对简单 缺点:可扩展性差 ②面向对象:一切以对象为中心. 一切皆为对象. 具体的某一 ...

  2. 在vim中 安装php的xdebug和 vdebug插件, 在vim中进行调试php代码

    在vim中 安装php的xdebug和 vdebug插件, 在vim中进行调试php代码 参考: http://www.cnblogs.com/qiantuwuliang/archive/2011/0 ...

  3. centos 查看USB接口的版本

    # lsusbBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hubBus 002 Device 001: ID 1d6b:000 ...

  4. Run tomcat on port 80 not 8080

    How to run Tomcat on Port 80 A standard Tomcat installation starts the webserver on port 8080 – whic ...

  5. Print a file's last modified date in Bash

    date -r <filename> #!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t ...

  6. 【链接】linuxCentOS权限问题修复(chmod777-R或者chmod755- http://www.cnblogs.com/kofxxf/p/5220836.html

    [链接]linuxCentOS权限问题修复(chmod777-R或者chmod755- http://www.cnblogs.com/kofxxf/p/5220836.html

  7. arcgis api for js 之发布要素服务

    1. 引言 如果我们要在网页端实现要素的增删改查操作,需要使用到要素服务(FeatureService),本篇文章将介绍如何发布要素服务. 1.1 什么是要素服务 在发布之前,我们先了解下要素服务:要 ...

  8. Spring项目JUnit测试报错ClassNotFoundException解决

    Eclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found UserTestjava.lang.ClassNotFoundExce ...

  9. _attribute_character

    职业属性控制表 comment 备注                                                                            classI ...

  10. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...