UVA 11039

来源:互联网 发布:linux 关闭swap分区 编辑:程序博客网 时间:2024/06/10 19:56

  Building designing 

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 

 257-269-3811-9251817-154

Sample Output 

 25

1Y的水题,不解释了,小模拟

#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>using namespace std;const int MAXN = 500000 + 50;const int maxw = 100 + 20;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e10-8#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define clr(a,b) memset(a,b,sizeof(a))#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)int a[MAXN];int main(){    #ifdef Online_Judge        freopen("in.txt","r",stdin);        freopen("out.txt","w",stdout);    #endif // Online_Judge    int T , n;    cin >> T;    while(T--)    {        scanf("%d",&n);        FOR(i , 0 , n)        {            scanf("%d",&a[i]);        }        a[n] = 0;        sort(a  , a + n + 1);//        FORR(i ,0 , n)cout << a[i]<<' ' ;//        cout << endl;        int key = 0;        FORR(i , 0 , n )        {            if(a[i] == 0)key = i;        }        int i , j , ans = 0;        i = key - 1;        j = key + 1;        int turn;///下一次向哪边取 , 1为右 , 0为左        int sign = 0;        if(abs(a[i]) < abs(a[j]))        {            turn = 1;            ans++;            sign = a[i];            i--;        }        else        {            turn = 0;            ans++;            sign = a[j];            j++;        }//        cout << "  " << sign <<endl;        while(i >= 0 || j <= n)        {            if(!turn)            {                if(abs(a[i]) > abs(sign))                {                    ans++;                    sign = a[i];                    i--;                    turn = 1;                }                else i--;            }            else            {                if(abs(a[j]) > abs(sign))                {                    ans++;                    sign = a[j];                    j++;                    turn = 0;                }                else j++;            }            if(i < 0&&!turn)break;            if(j > n&&turn)break;//            cout << ' ' << sign << endl;        }        printf("%d\n",ans);    }    return 0;}


原创粉丝点击