跳到正文

题解

题解:CF773C Prairie Partition

ask_silently约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年10月7日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 3 分钟
题目
CF773C

版本

  1. v2当前
  2. v1

duel 的时候遇到了,顺便把题解补了。

题目思路

首先易知长度一定不超过 11 的个数,然后观察样例,发现长度是一段后缀,因为有单调性,所以考虑二分。

简单证明一下单调性,假设长度 x(x<cnt1)x(x<cnt_1) 成立,由于有剩下来的 11,所以一定有一个数是 1+11+1,因此将该数拆成 11 与 11,则长度 x+1x+1 合法,故具有单调性。

现在转化为判定性问题。将数分为两类,一类为 22 的次幂,一类不为 22 的次幂,则合法的 prairie partition 一定是若干个第一类数凑一个或零个第二类数。因为要尽量的用完所有数,所以每遇到可以添加的第一类数就肯定会添加。按位考虑,假设当前有 xx 段第一类数,有 yy 个当前位的第一类数,目前有 zz 个第二类数没有归属,分类讨论:

  • 若 x>yx>y,说明一定会留下 x−yx-y 段第一类数段,这些段可以接上一个小于等于 2k2^k 的第二类数或第一类数,则令 zz 减去 x−yx-y,令 xx 变为 yy。此时若 zz 小于 00,将 z=0z=0。

  • 若 x≤yx\le y,则说明会留下若干个 2k2^k 的数,此时其与第二类数本质相同,则令 zz 加上 y−xy-x,xx 不变。

同时每到一位加上当前位的第二类数。考虑完所有位之后判断剩下的第一类数段和未归属的第二类数个数大小。

注意判断无解情况。

AcCode

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INT_MAX (int)(1e18)

const int N=1e5+10;
const int M=45;

int n;
int a[N],num[M],num1[M];

inline int read(){
	int t=0,f=1;
	register char c=getchar();
	while(c<'0'||c>'9') f=(c=='-')?(-1):(f),c=getchar();
	while(c>='0'&&c<='9') t=(t<<3)+(t<<1)+(c^48),c=getchar();
	return t*f;
}

bool judge(int x){
	int sum=num[0]-x;
	for(int i=1;i<=41;i++){
		if(x<num[i]) sum+=num[i]-x;
		sum+=num1[i],sum-=max(x-num[i],0ll),sum=max(0ll,sum);
		x=min(x,num[i]);
	}
	if(x<sum) return false;
	return true;
}

signed main(){
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	int dang=0,v=1;
	for(int i=1;i<=n;i++){
		while(a[i]>v) v*=2,dang++;
		if(a[i]==v) num[dang]++;
		else num1[dang]++;
	}
	int l=(num[0]+1)/2,r=num[0],mid;
	while(l<r){
		mid=(l+r>>1);
		if(judge(mid)) r=mid;
		else l=mid+1;
	}
	if(judge(l)){
		for(int i=l;i<=num[0];i++) cout<<i<<" ";
		cout<<"\n";
	}
	else cout<<-1<<"\n";
	return 0;
}
原文