Building designing
Building designing |
An architect wants to design a very highbuilding. The building will consist of some floors, and each floor has a certainsize. The size of a floor must be greater than the size of the floorimmediately above it. In addition, the designer (who is a fan of a famous Spanishfootball 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 buildingthe architect has n available floors, withtheir associated sizes and colours. All the available floors are of different sizes. The architect wants todesign the highest possible building with these restrictions, using the available floors.
Input
The input file consists of a firstline with the number p of cases to solve. The first line of each case containsthe number of available floors. Then, the size and colour of each floor appear inone line. Each floor is represented with an integer between -999999 and 999999.There is no floor with size 0. Negative numbers represent red floors andpositive 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 consistof a line with the number of floors of the highest building with the mentionedconditions.
Sample Input
2 5 7 -2 6 9 -3 8 11 -9 2 5 18 17 -15 4
Sample Output
2 5
题目大意:给出一个n个数列,将数列按绝对值递增的方式排完续后,求相邻元素符号不同的最大序列长度。
思路:俺绝对值排序,n将减去相邻元素相同的数目为所求答案
/*
@author : liuwen
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=500000+5;
struct number{
int num;
int flag;
}num[maxn];
bool cmp(const number &a,const number &b)
{
return a.num<b.num;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--){
int t,n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&t);
num[i].num=abs(t);
if(t<0) num[i].flag=1;
else num[i].flag=0;
}
sort(num+1,num+1+n,cmp);
int cnt=n;
for(int i=2;i<=n;i++){
if(num[i].flag==num[i-1].flag){
cnt--;
}
}
printf("%d\n",cnt);
}
return 0;
}