题意大致是说,某建筑师想建造一座高楼,该楼的建造有两个要求:第一,楼下一层的size一定比紧邻的楼上这层的要大;第二,由于是西甲某萨的球迷,所以楼层之间必须是一层蓝色、一层红色间隔地造。
输入的数据中,用负数代表红色的楼层,其size大小为该数值的绝对值,整数代表蓝色楼层,size大小为该数本身,取值范围在-999999到999999之间,任何两层的size大小均不同,且不会有楼层的size大小为0。
以下代码是我基于造楼至少为2层的假设写的(因为说了要红蓝间隔地造),碰巧还AC了,但事实上题目中只说明了楼层最高不超过500000(The maximum number of floors for a problem is 500000.),没说不能是0层(不造楼),或者只造1层(uDebug上该题目有一组测试数据就是测试这个0层或1层的)。所以严谨地说,代码还需要完善,不过修改起来也不难,但既然已经AC了,我这里就懒得改了。
python版本AC代码
testcase = int(input())
while testcase > 0:
testcase -= 1
n = int(input())
blue = []
red = []
floor = 0
while n > 0:
n -= 1
floor = int(input())
if floor < 0:
red.append(-floor)
else:
blue.append(floor)
red.sort()
blue.sort()
floor = 2
i = len(red)-1
j = len(blue)-1
while True:
if red[i] > blue[j]:
previous = blue[j]
while red[i] > previous and i > 0:
i -= 1
if red[i] < previous:
floor += 1
previous = red[i]
else:
break
while blue[j] > previous and j > 0:
j-= 1
if blue[j] < previous:
floor += 1
previous = blue[j]
else:
break
else:
previous = red[i]
while blue[j] > previous and j > 0:
j -= 1
if blue[j] < previous:
floor += 1
previous = blue[j]
else:
break
while red[i] > previous and i > 0:
i -= 1
if red[i] < previous:
floor += 1
previous = red[i]
else:
break
print(floor)
C++版本AC代码
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//#define ZANGFONG
const int maxn = 500010;
int blue[maxn],red[maxn];
int cmp(int a, int b)
{
return a > b;
}
int main()
{
#ifdef ZANGFONG
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ZANGFONG
int testcase, floor,num_r,num_b,i,j,n;
int previous;
scanf("%d\n",&testcase);
while(testcase--)
{
memset(blue,0,sizeof(blue));
memset(red,0,sizeof(red));
scanf("%d\n",&n);
num_r = num_b = 0;
for(i = 0; i < n; i++)
{
scanf("%d\n",&floor);
if(floor < 0) red[num_r++] = -floor;
else blue[num_b++] = floor;
}
sort(red,red+num_r);
sort(blue,blue+num_b);
i = num_r-1;
j = num_b-1;
floor = 2;
while(true)
{
if(red[i] > blue[j])
{
previous = blue[j];
while(red[i]>previous && i>0) i--;
if(red[i]<previous)
{
floor++;
previous = red[i];
}
else break;
while(blue[j]>previous && j>0) j--;
if(blue[j]<previous)
{
floor++;
previous = blue[j];
}
else break;
}
else
{
previous = red[i];
while(blue[j]>previous && j>0) j--;
if(blue[j]<previous)
{
floor++;
previous = blue[j];
}
else break;
while(red[i]>previous && i>0) i--;
if(red[i]<previous)
{
floor++;
previous = red[i];
}
else break;
}
}
printf("%d\n", floor);
}
return 0;
}