校内测 11.27 T2 数字游戏【二分】


题目:

传送门


题意:

一共有 n n n个数,已知有 k k k对数的和为正数
问最多有多少对数的乘积为正数


分析:

因为要使得积为正数的对数尽可能的多,所以我们原本的数应该都是些非零的数
这样我们设有 x x x个正数,就会有 n − x n-x nx个负数
此时积为正数的对数为 x ∗ ( x − 1 ) 2 + ( n − x ) ∗ ( n − x − 1 ) 2 \frac{x*(x-1)}{2}+\frac{(n-x)*(n-x-1)}{2} 2x(x1)+2(nx)(nx1)
所以显然我们要使得两项中的任意一个尽可能的大,所以我们可以二分 x x x的最大值和最小值,在两者中取最大值作为答案


代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
inline LL read()
{
	LL s=0,f=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') {s=s*10+c-'0';c=getchar();}
	return s*f;
}
LL n=read(),k=read();
LL get(LL x)
{
	LL y=n-x;
	return (x?x*(x-1)/2:0)+(y?y*(y-1)/2:0);
}
int main()
{
	if(k>n*(n-1)/2) return !printf("-1");
	LL l=0,r=n,a1=-1;
	while(l<=r)
	{
		LL mid=(l+r)>>1;
		if(mid*(mid-1)/2<=k) l=mid+1,a1=mid;
		else r=mid-1;
	}
	l=0;r=n;LL a2=-1;
	while(l<=r)
	{
		LL mid=(l+r)>>1;
		if(mid*(n-mid-1)/2>=k) r=mid-1,a2=mid;
		else l=mid+1;
	}
	cout<<max(a1==-1?-1:get(a1),a2==-1?-1:get(a2));
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值