UVA 11039.Building designing
Building designing
Time limit: 3.000 seconds
An architect wants to design a very high building. The building will consist of some ?oors, and each ?oor has a certain size. The size of a ?oor must be greater than the size of the ?oor 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 ?oor a colour, and in such a way that the colours of two consecutive ?oors are di?erent. To design the building the architect has n available ?oors, with their associated sizes and colours. All the available ?oors are of di?erent sizes. The architect wants to design the highest possible building with these restrictions, using the available ?oors.
Input
The input ?le consists of a ?rst line with the number p of cases to solve. The ?rst line of each case contains the number of available ?oors. Then, the size and colour of each ?oor appear in one line. Each ?oor is represented with an integer between -999999 and 999999. There is no ?oor with size 0. Negative numbers represent red ?oors and positive numbers blue ?oors. The size of the ?oor is the absolute value of the number. There are not two ?oors with the same size. The maximum number of ?oors for a problem is 500000.
Output
For each case the output will consist of a line with the number of ?oors 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
?
題意就是建樓,負數代表一種顏色,正數代表另一種顏色,要正負號交替且絕對值遞增。
絕對值排序然后標記正負。
?
代碼:
#include<bits/stdc++.h> const int N=5*1e5+10; using namespace std; int a[N]; bool cmp(int a,int b){return abs(a)<abs(b); } int main(){int t,n,flag,num;while(~scanf("%d",&t)){while(t--){scanf("%d",&n);flag=0;num=0;for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n,cmp);if(a[0]>0)flag=1; //這里一開始寫錯了,寫成flag=1了。。。else flag=2;num++;for(int i=1;i<n;i++){if(flag==1){if(a[i]<0){flag=2;num++;}}else if(flag==2){if(a[i]>0){flag=1;num++;}}}printf("%d\n",num);}}return 0; }?
轉載于:https://www.cnblogs.com/ZERO-/p/7182909.html
總結
以上是生活随笔為你收集整理的UVA 11039.Building designing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS进阶(十)position:rel
- 下一篇: node.js代码