UVA - 11039 B - Building designing

https://odzkskevi.qnssl.com/516b2b3ad824ada50248f23d20f53083?v=1502089438

 1 /*
 2 n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增
 3 */
 4 #include <iostream>
 5 #include <string.h>
 6 #include <stdio.h>
 7 #include <algorithm>
 8 using namespace std;
 9 const int N=500005;
10 int t,n,num[N];
11  bool cmp(int a,int b){
12     return abs(a)<abs(b);  //升序排序
13 }
14  void init(){
15     scanf("%d",&n);
16     for(int i=0;i<n;i++)
17     scanf("%d",&num[i]);
18     sort(num,num+n,cmp);     //sort函数应用
19  }
20  int solve(){
21     int ans=1,flag=0;
22     if(num[0]<0) flag=0;
23     else flag=1;
24     for(int i=0;i<n;i++){
25         if(flag==0&&num[i]>0){
26             flag=1;ans++;
27         }
28         else if(flag==1&&num[i]<0){
29             flag=0;ans++;
30         }
31     }
32     return ans;
33  }
34 int main(){
35     scanf("%d",&t);
36     while(t--){
37         init();
38         printf("%d\n",solve());
39     }
40     return 0;
41 }
时间: 2024-08-24 02:00:47

UVA - 11039 B - Building designing的相关文章

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); }

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

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 11039 - Building designing

题目:有n个绝对值不为0的数字,从中找到一个序列,正负交替,绝对值递增,求序列最大长度. 分析:dp,动态规划.因为绝对值要递增,所以先按绝对值排序. 设前k个数组成大的序列最长为f(k),则有如下地推关系: f(k)= f(k-1)        { data[k]*data[k-1] > 0,最后量元素不同时取 } = f(k-1)+ 1  { data[k]*data[k-1] < 0,最后量元素同时取 } (所有数据均不相同,且不为零) 说明:(⊙v⊙). #include <a

UVa 11039 (排序+贪心) Building designing

白书上的例题比较难,认真理解样例代码有助于提高自己 后面的练习题相对简单,独立思考解决问题,增强信心 题意:n个绝对值各不相同的非0整数,选出尽量多的数排成序列,使得该序列正负交错且绝对值递增. 解法:先按绝对值从小到大排序,然后第一个数先入队,然后依次比较后面的数,如果和队尾的数符号相反则入队,直到所有的数遍历完为止 这里用了异或运算,因为这里面没有0,所谓符号相同的两个数异或值为正,不同符号的数异或值为负 1 //#define LOCAL 2 #include <algorithm> 3

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

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

UVa 1605 (构造) Building for UN

题意: 有n个国家,要设计一栋长方体的大楼,使得每个单位方格都属于其中一个国家,而且每个国家都要和其他国家相邻. 分析: 紫书上有一种很巧妙的构造方法: 一共有2层,每层n×n.一层是每行一个国家,另一层是每列一个国家. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 50; 7 char b[2][max

大白书第一章

UVA 11292 The Dragon of Loowater(简单贪心) 题意: n条恶龙,m个勇士,用勇士来杀恶龙.一个勇士只能杀一个恶龙.而且勇士只能杀直径不超过自己能力值的恶龙.每个勇士需要支付能力值一样的金币.问杀掉所有恶龙需要的最少金币. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20000 + 5; 4 int n, m; 5 int night[maxn], dragon[maxn