2021_CCPC_harbin_I. Power and Zero

10 篇文章 0 订阅
10 篇文章 1 订阅

传送门:https://codeforces.com/gym/103447/problem/I
二分+二进制拆分
题目大意:给定一个数组,每次操作可以选择任意个元素(可重复选择),对这多个元素分别 -1,-2,-4,-8…。
求最少操作多少次使得数组变为全0数组。

I. Power and Zero
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Given a sequence A1,A2,⋯,An whose elements are all positive integers. You should do some operations to make the sequence all zero. For each operation, you can appoint a sequence B1,B2,⋯,Bm(Bi∈{1,2,⋯,n}) of arbitrary length m and reduce ABi by 2i−1 respectively. Specially, one element in the given sequence can be reduced multiple times in one operation. Determine the minimum possible number of operations to make the given sequence all zero.

Input
The first line contains one integer T(1≤T≤1000), denoting the number of test cases.

For each test case:

The first line contains one integer n(1≤n≤1e5), denoting the length of given sequence.

The second line contains n integers A1,A2,⋯,An(1≤Ai≤1e9), denoting the given sequence.

It is guaranteed that ∑n≤1e5.

Output
For each test case:

Output one line containing one integer, denoting the answer.

Example
inputCopy
3
5
1 2 3 4 5
2
1 4
1
7
outputCopy
3
3
1
Note
For the first sample case, one possible scheme:

Appoint B={1,3,5}, then A will be {0,2,1,4,1}.
Appoint B={3,2,4}, then A will be {0,0,0,0,1}.
Appoint B={5}, then A will be {0,0,0,0,0}.
For the second sample case, one possible scheme:

Appoint B={1,2}, then A will be {0,2}.
Appoint B={2}, then A will be {0,1}.
Appoint B={2}, then A will be {0,0}.
For the third sample case, one possible scheme:

Appoint B={1,1,1}, then A will be {0}.

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll ay[40];
int n;
bool check(ll x){
	if(ay[0]>x) return 0;
	ll a[40];
	for(int i=0;i<=31;i++){
		a[i]=ay[i];
	}
	a[32]=0;
	for(int i=0;i<=31;i++){
		if(i&&a[i]>a[i-1]||a[i]>x) return false;
		ll y = i?a[i-1]-a[i]:x-a[i];
		a[i+1]-=y/2;
		a[i]+=y/2*2;
	}
	return true;
}
ll find()
{
	ll l=0 , r=1e16;
    while (l < r)
    {
        ll mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    return l;
}
int main()
{
	int _;scanf("%d",&_);
	while(_--){
		memset(ay,0,sizeof ay);
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			ll x;scanf("%lld",&x);
			for(int j=0;j<=31;j++){
				ay[j]+=( (x>>j)&1 );
			}
		}
		printf("%lld\n",find());
	}
	
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值