码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11039 Building designing 贪心

时间:2015-04-08 23:17:38      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:UVA - 11039

题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大;第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色。现在给出楼层数量和每层楼的尺寸(楼层尺寸的大小没有按照顺序给出),求出满足这样要求的最大楼层数。

算法分析:把楼层尺寸按照从大到小排序,然后遍历一次的同时记录相邻楼层所染颜色不同,把不满足要求的楼层去掉即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 const int maxn=500000+10;
10 
11 int n,an[maxn];
12 
13 int cmp(int i,int j)
14 {
15     i=abs(i) ;j=abs(j) ;
16     return i>j;
17 }
18 
19 int main()
20 {
21     int t;scanf("%d",&t);
22     while (t--)
23     {
24         scanf("%d",&n);
25         for (int i=0 ;i<n ;i++) scanf("%d",&an[i]);
26         sort(an,an+n,cmp);
27         int cnt=1,flag= an[0]>0 ? 1 : -1 ;
28         for (int i=1 ;i<n ;)
29         {
30             int f= an[i]>0 ? 1 : -1 ;
31             while (f==flag && i<n)
32             {
33                 i++;
34                 f= an[i]>0 ? 1 : -1 ;
35             }
36             if (f != flag && i<n)
37             {
38                 cnt ++ ;
39                 flag=f;
40                 i ++ ;
41             }
42         }
43         printf("%d\n",cnt);
44     }
45     return 0;
46 }

 

UVA 11039 Building designing 贪心

标签:

原文地址:http://www.cnblogs.com/huangxf/p/4404556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!