UVa 11039 (排序+贪心) Building designing

白书上的例题比较难,认真理解样例代码有助于提高自己

后面的练习题相对简单,独立思考解决问题,增强信心

题意:n个绝对值各不相同的非0整数,选出尽量多的数排成序列,使得该序列正负交错且绝对值递增。

解法:先按绝对值从小到大排序,然后第一个数先入队,然后依次比较后面的数,如果和队尾的数符号相反则入队,直到所有的数遍历完为止

这里用了异或运算,因为这里面没有0,所谓符号相同的两个数异或值为正,不同符号的数异或值为负

 1 //#define LOCAL
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7
 8 const int maxn = 500000 + 10;
 9 int a[maxn];
10 bool cmp(int a, int b)
11 {
12     return abs(a) < abs(b);
13 }
14
15 int main(void)
16 {
17     #ifdef LOCAL
18         freopen("11039in.txt", "r", stdin);
19     #endif
20
21     int T, n;
22     scanf("%d", &T);
23     while(T--)
24     {
25         scanf("%d", &n);
26         for(int i = 0; i < n; ++i)
27             scanf("%d", &a[i]);
28         sort(a, a+n, cmp);
29         int q = 0, cnt = 1;
30         for(int i = 1; i < n; ++i)
31             if((a[i] ^ a[q]) < 0)
32             {
33                 q = i;
34                 ++cnt;
35             }
36         printf("%d\n", cnt);
37     }
38     return 0;
39 }

代码君

时间: 2024-08-06 11:53:04

UVa 11039 (排序+贪心) Building designing的相关文章

uva 11039 Building designing (排序)

uva 11039 Building designing An architect wants to design a very high building. The building will consist of some floors, and each floor has a certain size. The size of a floor must be greater than the size of the floor immediately above it. In addit

UVA 11039 Building designing 贪心

题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量和每层楼的尺寸(楼层尺寸的大小没有按照顺序给出),求出满足这样要求的最大楼层数. 算法分析:把楼层尺寸按照从大到小排序,然后遍历一次的同时记录相邻楼层所染颜色不同,把不满足要求的楼层去掉即可. 1 #include<iostream> 2 #include<cstdio> 3 #inc

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() { int n,kas = 0; while(scanf("%d",&n),n>0){ int r = 0; for(n--;n;n>>=1) r++; printf("Case %d: %d\n",++kas,r); }

11039 - Building designing

  Building designing  An architect wants to design a very high building. The building will consist of some floors, and each floor has a certain size. The size of a floor must be greater than the size of the floor immediately above it. In addition, th

uva 1511 - Soju(贪心)

题目链接:uva 1511 - Soju 题目大意:给出两个点集,问说分别从两个点集中取一点的哈夫曼距离最小值.注意一个点集的x坐标小于0,另一个大于0. 解题思路:因为x2一定大于x1,所以对于x这一维,一定是+x2-x1,所以只需要考虑y这一维坐标即可. #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> u

2014 Super Training #8 B Consecutive Blocks --排序+贪心

当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就是滑动窗口的思想. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algor

HDU 4912 Paths on the tree LCA 排序贪心

lca... 排个序然后暴力保平安 _(:зゝ∠)_ #pragma comment(linker, "/STACK:102400000,102400000") #include"cstdio" #include"iostream" #include"set" #include"queue" #include"string.h" using namespace std; #define

CodeForces 1294B Collecting Packages(排序+贪心)

http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否收集到所有包裹,如果能,给出字典序最小的路径. 最开始当成搜索题了,其实可以排序+贪心写的. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string>

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

题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计数,easy!!! 代码如下: #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const int maxn = 5