UVA 11039 Building designing

大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增。

思路:按照绝对值大小排一次序,然后扫描一次,顺便做个标记即可。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAXN = 500010;

int A[MAXN];
int n;

int cmp(int a, int b)
{
	return abs(a) < abs(b);
}

void read_case()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &A[i]);
	sort(A+1, A+1+n, cmp);
}

void solve()
{
	read_case();
	int count = 0;
	int flag = 0;
	for(int i = 1; i <= n; i++)
	{
		if(flag == 0)
		{
			if(A[i] > 0) flag = 1;
			else flag = 2;
			count++;
		}
		else if(flag == 1)
		{
			if(A[i] < 0) {flag = 2; count++;}
		}
		else if(flag == 2)
		{
			if(A[i] > 0) {flag = 1; count++;}
		}
	}
	printf("%d\n", count);
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值