[ABC422D]Least Unbalanced题解

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score : 400 points

Problem Statement

Let N be a positive integer. Define the imbalance of a sequence A=(A1​,A2​,…,A2N​) of non-negative integers of length 2N as the non-negative integer value obtained by the following operation:

  • Initially, set X=0.
  • Perform the following series of operations N times:
    • Update X to max(X,max(A)−min(A)), where max(A) and min(A) denote the maximum and minimum values of sequence A, respectively.
    • Form a new sequence of half the length by pairing elements from the beginning two by two and arranging their sums. That is, set A←(A1​+A2​,A3​+A4​,…,A∣A∣−1​+A∣A∣​).
  • The final value of X is the imbalance.

For example, when N=2,A=(6,8,3,5), the imbalance is 6 through the following steps:

  • Initially, X=0.
  • The first series of operations is as follows:
    • Update X to max(X,max(A)−min(A))=max(0,8−3)=5.
    • Set A to (6+8,3+5)=(14,8).
  • The second series of operations is as follows:
    • Update X to max(X,max(A)−min(A))=max(5,14−8)=6.
    • Set A to (14+8)=(22).
  • Finally, X=6.

You are given a non-negative integer K. Among all sequences of non-negative integers of length 2N with sum K, construct a sequence that minimizes the imbalance.

讯飞听见 翻译

###问题陈述

设 N 为正整数。

将长度为 2N 的非负整数序列 A=(A1​,A2​,…,A2N​) 的不平衡定义为通过FOL获得的非负整数值减速操作:

-最初,设置 X=0 。

-执行以下一系列操作 N 次:

-更新 X 至 max(X,max(A)−min(A)) ,

其中 max(A) 和 min(A) 分别表示序列 A 的最大值和最小值。-通过从开始两个两个地配对元素并排列它们的和来形成一半长度的新序列。即设置 A←(A1​+A2​,A3​+A4​,…,A∣A∣−1​+A∣A∣​) 。

  • X 的最终值是不平衡。

例如,当 N=2,A=(6,8,3,5) 时,通过以下步骤,不平衡为 6 :

-最初, X=0 。

-第一系列操作如下:

-更新 X 至 max(X,max(A)−min(A))=max(0,8−3)=5 。

-将 A 设置为 (6+8,3+5)=(14,8) 。

-第二系列操作如下:

-更新 X 至 max(X,max(A)−min(A))=max(5,14−8)=6 。

-将 A 设置为 (14+8)=(22) 。

-最后, X=6 。

您将得到一个非负整数 K 。

在所有长度为 2N 且和为 K 的非负整数序列中,构造一个使不平衡最小化的序列。

Constraints

  • 1≤N≤20
  • 0≤K≤109
  • N and K are integers.

讯飞听见 翻译

###约束

  • 1≤N≤20

  • 0≤K≤109

  • N 和 K 是整数。


Input

The input is given from Standard Input in the following format:

N K

讯飞听见 翻译

###输入

输入来自标准输入,格式如下:

N K

Output

Let B=(B1​,B2​,…,B2N​) be a sequence with minimum imbalance. Let U be the imbalance of B. Output a solution in the following format:

U
B1​ B2​ … B2N​

If there are multiple solutions, any of them will be considered correct.

讯飞听见 翻译

###输出

设 B=(B1​,B2​,…,B2N​) 是具有最小不平衡度的序列。设 U 为 B 的不平衡。按以下格式输出解决方案:

U
B1​ B2​ … B2N​

如果有多个解决方案,其中任何一个都将被认为是正确的。


Sample Input 1

Copy

1 11

Sample Output 1

Copy

1
5 6

(5,6) is a sequence with imbalance 1, which is the minimum imbalance among sequences satisfying the condition.


Sample Input 2

Copy

3 56

Sample Output 2

Copy

0
7 7 7 7 7 7 7 7

思路

尽量均衡即可。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long n,k,a[2000006],lk=0;
void abc(long long l,long long r,long long z){
	if(l==r){
		a[l]=z;
		return ;
	}
	long long mid=(l+r)/2,d=z/2;
	abc(l,mid,d);
	abc(mid+1,r,z-d);
}
int main(){
	cin>>n>>k;
	abc(1,(1ll<<n),k);
	for(int i=n;i>=0;i--){
		if(k%(1ll<<i)==0){
			lk=n-i;
			break;
		}
	}
    if(lk!=0){
        cout<<1<<endl;
    }
	else{
        cout<<0<<endl;
    }
	for(int i=1;i<=(1ll<<n);i++){
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
}

posted @ 2025-10-04 19:30  bz02_2023f2  阅读(2)  评论(0)    收藏  举报  来源