UVA 11039 Building Designing (sort)

Source: Internet
Author: User

UVA 11039 Building Designing


A architect wants to design a very high building. The building would consist of some floors, and each floor had a certain size. The size of a floor must is greater than the size of the floor immediately above it. In addition, the designer (who's a fan of a famous Spanish football team) wants to paint the building in blue and red, EA CH Floor a colour, and in such a by that the colours of the consecutive floors is different.

To design the building the architect have n available floors, with their associated sizes and colours. All the available floors is 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 all case contains the number of available floors. Then, the size and colour of all appear in one line. Each floor is a 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 is not a floors with the same size. The maximum number of floors for a problem is 500000.

Output

The output would consist of a line with the number of floors of the highest building with the mentioned Condi tions.

Sample Input
257-269-3811-9251817-154
Sample Output
25


Main topic: n 0 integers with different absolute values, select as many numbers as possible to form a sequence so that the positive and negative numbers are alternating and the absolute value is incremented.

problem-solving ideas: First, all the numbers are sorted by absolute value, then alternate to find positive negative numbers.


#include <stdio.h> #include <math.h> #include <algorithm>using namespace Std;int num[500005];int cmp ( int a, int b) {return abs (a) < ABS (b);} int main () {int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n), for (int i = 0; i < n; i++) {scanf ("%d", & Amp;num[i]);} Sort (num, num + N, CMP); int ans = 0, flag = 0;for (int i = 0; i < n; i++) {if (!flag) {//First number, flag flag positive and negative if (Num[i] > 0) flag = 1;else flag = 2;ans++;continue;} if (flag = = 1) {//previous number is positive, current number is negative if (Num[i] < 0) {flag = 2;ans++;continue;}} if (flag = = 2) {//previous number is negative. The current number is positive if (Num[i] > 0) {flag = 1;ans++;continue;}}} printf ("%d\n", ans);} return 0;}





UVA 11039 Building Designing (sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.