UVA 11039(模拟,贪心)

来源:互联网 发布:r语言与数据挖掘 谢 编辑:程序博客网 时间:2024/04/29 12:42
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, the designer (who is a fan of a famous Spanish football team) wants to paint
the building in blue and red, each floor a colour, and in such a way that the colours of two consecutive
floors are different.
To design the building the architect has n available floors, with their associated sizes and colours.
All the available floors are of different sizes. The architect wants to design the highest possible building
with these restrictions, using the available floors.
Input
The input file consists of a first line with the number p of cases to solve. The first line of each case
contains the number of available floors. Then, the size and colour of each floor appear in one line.
Each floor is represented with an integer between -999999 and 999999. There is no floor with size 0.
Negative numbers represent red floors and positive numbers blue floors. The size of the floor is the
absolute value of the number. There are not two floors with the same size. The maximum number of
floors for a problem is 500000.
Output
For each case the output will consist of a line with the number of floors of the highest building with
the mentioned conditions.
Sample Input
2
5
7
-2
6
9
-3
8
11
-9
2
5
18
17
-15
4
Sample Output
2

5


题意:给你一堆数字,让你构造一个新数列,要求是绝对值递增,而且正负值交替


题解:先绝对值排序。明显第一个数字是将绝对值较小的那个作为数列的开头,然后直接模拟就好啦。


#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<string>#include<algorithm>#include<set>#include<cmath>#include<map>#define LL long long using namespace std;vector<int>a,b;int solve(){int f,tmp,t1=0,t2=0;if(a[0]<b[0]){f=1;tmp=a[0];}else{f=0;tmp=b[0];}int ans=1;int over=0;while(t2<a.size()&&t1<b.size()){if(f){while(tmp>b[t1]){t1++;if(t1==b.size()){over=1;break;}}f=0;if(!over){tmp=b[t1];ans++;}else return ans;}else{while(tmp>a[t2]){t2++;if(t2==a.size()){over=1;break;}}f=1;if(!over){tmp=a[t2];ans++;}else return ans;}}return ans;}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endif    int t,n,x;scanf("%d",&t);while(t--){a.clear();b.clear();scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&x);if(x>0)a.push_back(x);else b.push_back(-x);}sort(a.begin(),a.end());sort(b.begin(),b.end());printf("%d\n",solve());}    return 0;}


0 0
原创粉丝点击