【FZU】Problem 2178 礼物分配 中途相遇法

传送门:【FZU】Problem 2178 礼物分配


题目分析:

我们可以用01表示物品的归属。

用中途相遇法,将物品平均分成两堆,一堆暴力得到这一堆物品能构成的所有方案,另一堆对每一个方案,都在暴力的一堆中二分找到最接近的解,更新最优值。(奇数个物品则保证暴力的一堆比二分的一堆多一个)。暴力的一堆的方案可以按照属于其中一个人的物品的个数分类,然后每一类排个序,以便满足二分的性质。


代码如下:


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std ;

typedef long long LL ;

#pragma comment ( linker , "/STACK:1024000000" )
#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define rec( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] )
#define clr( a , x ) memset ( a , x , sizeof a )

const int INF = 0x3f3f3f3f ;

struct Node {
	int v , w ;
} ;

Node node[31] ;
int a[16][40000] ;
int n , half ;
int ans ;

int search ( int x , int o ) {
	int l = 1 , r = a[o][0] ;
	while ( l < r ) {
		int m = ( l + r ) >> 1 ;
		if ( a[o][m] >= x ) r = m ;
		else l = m + 1 ;
	}
	return l ;
}

void dfs ( int cur , int cnt = 0 , int val = 0 ) {
	if ( cur == half + 1 ) {
		a[cnt][++ a[cnt][0]] = val ;
		return ;
	}
	dfs ( cur + 1 , cnt + 1 , val + node[cur].v ) ;
	dfs ( cur + 1 , cnt + 0 , val - node[cur].w ) ;
}

void dfs2 ( int cur , int cnt = 0 , int val = 0 ) {
	if ( cur == n + 1 ) {
		if ( cnt > 1 && abs ( n-2*half-2 ) <= 1 ) {
			int x = search ( val , cnt - 1 ) ;
			ans = min ( ans , abs ( val - a[cnt - 1][x] ) ) ;
			if ( x > 1 ) ans = min ( ans , abs ( val - a[cnt - 1][x - 1] ) ) ;
			if ( x < a[cnt - 1][0] ) ans = min ( ans , abs ( val - a[cnt - 1][x + 1] ) ) ;
		}
		if ( cnt < half && abs ( n-2*half+2 ) <= 1 ) {
			int x = search ( val , cnt + 1 ) ;
			ans = min ( ans , abs ( val - a[cnt + 1][x] ) ) ;
			if ( x > 1 ) ans = min ( ans , abs ( val - a[cnt + 1][x - 1] ) ) ;
			if ( x < a[cnt + 1][0] ) ans = min ( ans , abs ( val - a[cnt + 1][x + 1] ) ) ;
		}
		int x = search ( val , cnt ) ;
		ans = min ( ans , abs ( val - a[cnt][x] ) ) ;
		if ( x > 1 ) ans = min ( ans , abs ( val - a[cnt][x - 1] ) ) ;
		if ( x < a[cnt][0] ) ans = min ( ans , abs ( val - a[cnt][x + 1] ) ) ;
		return ;
	}
	dfs2 ( cur + 1 , cnt + 0 , val - node[cur].v ) ;
	dfs2 ( cur + 1 , cnt + 1 , val + node[cur].w ) ;
}

void solve () {
	ans = INF ;
	scanf ( "%d" , &n ) ;
	For ( i , 1 , n ) scanf ( "%d" , &node[i].v ) ;
	For ( i , 1 , n ) scanf ( "%d" , &node[i].w ) ;
	half = ( n + 1 ) / 2 ;
	For ( i , 0 , half ) a[i][0] = 0 ;
	dfs ( 1 ) ;
	For ( i , 0 , half ) sort ( a[i] + 1 , a[i] + a[i][0] + 1 ) ;
	dfs2 ( half + 1 ) ;
	printf ( "%d\n" , ans ) ;
}

int main () {
	int T ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) solve () ;
	return 0 ;
}


### 如何计算神经网络参数量FLOPs #### 参数量 (Params) 参数量指的是模型中可训练参数的数量总和。对于卷积层而言,其参数数量可以通过下面的方式得出: \[ \text{params} = (\text{kernel height} * \text{kernel width} * \text{input channels} + 1) * \text{output channels} \] 其中,“+1”代表偏置项(bias),如果该层不使用偏置,则无需加上这一部分。 对于全连接层来说,参数数目等于输入节点数乘以输出节点数再加上一个偏置向量[^4]。 为了统计整个网络中的参数总量,在Python环境下可以利用PyTorch框架提供的接口遍历所有层并累加各层参数: ```python total_params = sum(p.numel() for p in model.parameters()) print(f&#39;Total number of parameters: {total_params}&#39;) ``` 这段代码会返回给定`model`对象内所有的可训练参数之和[^5]。 #### 浮点运算次数 (FLOPs) 浮点操作数(Floating Point Operations Per Second, FLOPs),用于描述执行一次前向传播过程中涉及了多少次基本数学运算(比如加法、减法、乘法)。它不仅取决于权重矩阵尺寸还关联到激活函数的选择等因素。 针对不同类型的层有不同的估算方式;例如标准二维卷积层的理论FLOPs可通过如下公式近似估计: \[ \text{FLOPs}_{conv2d} = 2 * W_{out}H_{out}\times C_{in}\times K_hK_w\times C_{out}/S^2 \] 这里\(W_{out}, H_{out}\)表示输出特征图宽高,\(C_{in}, C_{out}\)分别是输入输出通道数,\(K_h,K_w\)为核大小而\(S\)则是步幅(step size)[^3]。 实际应用中推荐借助第三方库如`thop`来自动化获取更精确的结果: ```python from thop import profile macs, params = profile(model, inputs=(input_tensor,)) print(&#39;Computational complexity:&#39;, macs) print(&#39;Number of parameters:&#39;, params) ``` 上述脚本能够方便快捷地得到指定模型及其输入张量组合下的MACs(Multiply-Accumulate operations)与参数计数[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值