General Problem Solving Techniques [Beginner-1]~B

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

解题思路:题目的大概意思就是,一个建筑工人要建一栋房子,它所需要的材料有瓷砖。现提供n块瓷砖,求出可以建造的最大的高度是多少。需要注意的是,当输入的数是正数时,代表这块地板的颜色是蓝色的,当输入的数是负数时,代表这块地板的颜色是红色的。题目中也要求不同颜色的地板才能镶嵌盖,相同颜色的地板是不能放在一起的,并且下面的地板的大小必须大于上面的地板的大小。注意:每进行一次测试时都需要将数组清零。

程序代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const long long N=1000000;
long long a[N],b[N],c[N],d[N];
int main()
{
    int t,i,j,k,f,g;
    long long maximun,n;
    char floag;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        f=0,g=0;
        floag=‘x‘;
        scanf("%lld",&n);
        for(i=0;i<n;i++)
            scanf("%lld",&c[i]);
        for(i=0,j=0,k=0;i<n;i++)
        {
             if(c[i]>0)
                {a[j]=c[i];j++;f=1;}
            else
                {b[k]=c[i]*(-1);k++;g=1;}
        }
        if(f==1&&g==1)maximun=2;
        else maximun=1;
        sort(a,a+j-1);
        sort(b,b+k-1);
        if(maximun==2)
        {
            for(i=0,j=0,k=0;i<n;i++)
        {
            if(a[j]<b[k]){d[i]=a[j];j++;if(floag==‘b‘)maximun++;floag=‘a‘;}
            else{d[i]=b[k];k++;if(floag==‘a‘)maximun++;floag=‘b‘;}
        }
        }

        printf("%lld\n",maximun);
    }
    return 0;
}
时间: 2024-10-27 12:47:41

General Problem Solving Techniques [Beginner-1]~B的相关文章

General Problem Solving Techniques [Intermediate-1]~A - Children&#39;s Game

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make a big integer by appending those in

General Problem Solving Techniques [Intermediate-1]~F - Tian Ji -- The Horse Racing

Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others. Both of Tian and the king have three horses in different classes,

General Problem Solving Techniques [Examples]~A

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out o

General Problem Solving Techniques [Intermediate-1]~G - The Bus Driver Problem

In a city there are n bus drivers. Also there are n morning bus routes and n afternoon bus routes withvarious lengths. Each driver is assigned one morning route and one evening route. For any driver, ifhis total route length for a day exceeds d, he h

General Problem Solving Techniques [Beginner-1]~E

Mohammad has recently visited Switzerland. As he loves his friends very much, he decided to buy some chocolate for them, but as this fine chocolate is very expensive (You know Mohammad is a little BIT stingy!), he could only afford buying one chocola

General Problem Solving Techniques [Beginner-1]~F

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inser

General Problem Solving Techniques [Intermediate-1]~D - Fabled Rooks

We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × nboard subject to the following restrictions• The i-th rook can only be placed within the rectanglegiven by its left-upper corner (xli, yli) and its rightlowercorner (xri, yri), where 1 ≤ i ≤ n,

AST11103 Problem Solving

AST11103 Problem Solving with Programming SkillsAdditional Individual Assignment: Min-Game Programming (40%)Learning Outcomes1. Use common application software and program development tools;2. Analyze simple problems with basic problem solving skills

【POJ 3265】Problem Solving

Problem Solving Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1645   Accepted: 675 Description In easier times, Farmer John's cows had no problems. These days, though, they have problems, lots of problems; they have P (1 ≤ P ≤ 300) pro