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.
用优先队列排序的话,就可以只考虑颜色而不管大小!往前推进就是了!
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
while (t-- > 0) {
int n = reader.nextInt();
PriorityQueue<Block> wait = new PriorityQueue<Block>();
while (n-- > 0) {
int size = reader.nextInt();
int cor = get > 0 ? 1 : 2;
wait.add(new Block(cor, Math.abs(size)));
}
// 看看最下面的是什么颜色(最大的),然后直接删掉,计数标1
boolean flag = wait.poll().cor == 1;
int count = 1;
while (!wait.isEmpty()) {
Block get = wait.poll();
if (flag) {
while (get.cor == 1 && !wait.isEmpty()) {
get = wait.poll();
}
} else {
while (get.cor == 2 && !wait.isEmpty()) {
get = wait.poll();
}
}
if ((flag && get.cor == 1) || (!flag && get.cor == 2)) {
break;
}
flag = !flag;
count++;
}
System.out.println(count);
}
}
}
class Block implements Comparable<Block> {
int cor;
int size;
public Block(int cor, int size) {
this.cor = cor;
this.size = size;
}
@Override
public int compareTo(Block o) {
// 从大到小按size排
return o.size - this.size;
}
}