2021_CCPC_harbin_I. Power and Zero

本文介绍了一道编程竞赛题,题目要求通过一系列特定操作将一个正整数数组变为全零数组,每次操作可以对任意元素进行2的幂次减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;
}
2021CCPC女生程序设计竞赛部分题解如下: - **A题公交路线题解**: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, x, y, arr[15]={0}; cin >> n >> x >> y; for (int i = 1; i <= n; i++) { cin >> arr[i]; } int m, xx, brr[15]={0}; cin >> m; if(x<y){ for(int i=1;i<=m;i++){ cin>>brr[i]; if(brr[i]!=arr[i+x]){ cout<<"Wrong"<<endl; return 0; } } if(x-1<1||x-m<1){ cout<<"Right"<<endl; return 0; } for(int i=1;i<=m;i++){ if(arr[x-i]!=brr[i]){ cout<<"Right"<<endl; return 0; } } cout<<"Unsure"<<endl; return 0; }else{ for(int i=1;i<=m;i++){ cin>>brr[i]; if(brr[i]!=arr[x-i]){ cout<<"Wrong"<<endl; return 0; } } if(x==n||x+m>n){ cout<<"Right"<<endl; return 0; } for(int i=1;i<=m;i++){ if(arr[x+i]!=brr[i]){ cout<<"Wrong"<<endl; return 0; } } cout<<"Unsure"<<endl; } return 0; } ``` - **C题题解**: 思路是寻找一个最短的 `t`,使得 `t` 不是 `s(l,r)` 的子序列,假设现在位于 `x`,下一步有 `m` 个选择,要使子序列尽可能小,就选择离 `x` 最远的那个字母,直到走出 `r` 为止。问题转化为从 `l` 开始沿着 `_next` 一路往右跳,要跳多少步才能跳到 `> r` 的地方,使用倍增的方法使得 `_next[i][j]` 是 `i` 从 `2^j` 步到达的最远位置。 ```cpp #include<bits/stdc++.h> using namespace std; char s[200100]; int _next[200100][26];//st表 int v[2000100]; int a[26]; int main(){ int m,n; scanf("%d%d",&m,&n); scanf("%s",s+1); for(int i=0;i<=25;i++){ a[i]=n+1; } for(int i=n;i>=0;i--){ for(int j=0;j<m;j++){ v[i]=max(v[i],a[j]); } _next[i][0]=v[i]; if(i)a[s[i]-'a']=i; } for(int j = 1; j < 20; j ++){ for(int i = 0; i <= n; i ++) { int t = _next[i][j - 1]; if(t <= n) _next[i][j] = _next[t][j - 1]; else _next[i][j] = n + 1; } } int q; scanf("%d",&q); while(q--){ int l,r; scanf("%d%d",&l,&r); int ans = 0, now = l - 1; for(int j = 19; ~j; j --) if(_next[now][j] <= r) ans += (1 << j), now = _next[now][j]; printf("%d\n",ans+1); } return 0; } ``` 以上代码分别对应2021CCPC女生程序设计竞赛A题和C题的题解 [^2][^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值