UVA - 311 Packets(贪心)

智能算法优化的包裹打包问题解决方案
本文探讨了如何通过智能算法解决工厂生产并配送不同尺寸的包裹至客户时,如何最小化所需的配送箱数量的问题。通过分析各种可能的打包方案,文章提供了优化算法的实现细节,包括对不同尺寸包裹的合理分配,以及对特定尺寸包裹的特殊处理策略。这种方法不仅节省了成本,也提高了物流效率。


 Packets 

A factory produces products packed in square packets of the same height h and of the sizes tex2html_wrap_inline27 , tex2html_wrap_inline29 , tex2html_wrap_inline31 , tex2html_wrap_inline33 , tex2html_wrap_inline35 , tex2html_wrap_inline37 . These products are always delivered to customers in the square parcels of the same height h as the products have and of the size tex2html_wrap_inline37 . Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.

Input

The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size tex2html_wrap_inline27 to the biggest size tex2html_wrap_inline37 . The end of the input file is indicated by the line containing six zeros.

Output

The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.

Sample Input

0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0

Sample Output

2
1


题目大意:
一个工厂生产包裹,有6种包裹,分别为1*1、2*2、3*3……6*6,每种包裹的高度相同都为h,现在要将这些包裹装进6*6的箱子中,问最少需要多少个6*6的箱子。

解析:贪心,不难想到应该先装大的包裹,再装小的包裹。
所以按照这个思路来分析一下该题。不妨把每种情况画出来。

(1)6*6,1个6*6
(2)5*5,1个5*5+11个1*1
(3)4*4,1个4*4+5个2*2
(4)3*3应该分4种情况讨论,

4个3*3

1个3*3+5个2*2+7个1*1

2个3*3+3个2*2+6个1*1

3个3*3+1个2*2+5个1*1

(5)2*2
9个2*2
(6)1*1
36个1*1

注意上面的每种情况,每次用到2*2的包裹就要判断,如果2*2的包裹用完的话,要用1*1的包裹来填充。

#include <cstdio>
#include <cstring>
using namespace std;
int a[7];
void judge() {
	if(a[2] < 0) {
		a[1] += 4 * a[2];
		a[2] = 0;
	}
}
int main() {
	int sum,tmp;
	while(1) {
		bool ok = false;
		sum = 0;
		for(int i = 1; i <= 6; i++) {
			scanf("%d",&a[i]);
			ok = ok || a[i];
		}
		if(!ok) {
			break;
		}
		//6*6
		sum += a[6];
		//5*5
		sum += a[5];
		a[1] -= 11 * a[5];
		//4*4
		sum += a[4];
		a[2] -= 5 * a[4];
		judge();
		//3*3
		sum += a[3] / 4;
		tmp = a[3] % 4;
		if(tmp) {
			sum++;
			switch(tmp) {
				case 1:
					a[2] -= 5;
					a[1] -= 7;
					judge();
					break;
				case 2:
					a[2] -= 3;
					a[1] -= 6;
					judge();
					break;
				case 3:
					a[2] -= 1;
					a[1] -= 5;
					judge();
					break;
			}
		}
		//2*2
		if(a[2] > 0) {
			sum += a[2] / 9;
			tmp = a[2] % 9;
			if(tmp) {
				sum++;
			}
			a[1] -= (36 - tmp*4);
		}
		//1*1
		if(a[1] > 0) {
			sum += a[1] / 36;
			tmp = a[1] % 36;
			if(tmp) {
				sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}


// 搜索时间戳匹配且跨度足够的数据包 for (i = 0; i < jitter->buffer_size; i++) { if (jitter->packets[i].data && jitter->packets[i].timestamp == jitter->pointer_timestamp && GE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp + desired_span)) break; } // 其他搜索逻辑... if (i != jitter->buffer_size) { spx_int32_t offset; jitter->lost_count = 0; if (jitter->arrival[i] != 0) { update_timings(jitter, ((spx_int32_t)jitter->packets[i].timestamp) - ((spx_int32_t)jitter->arrival[i]) - jitter->buffer_margin); } // 复制数据包 if (jitter->destroy) { packet->data = jitter->packets[i].data; packet->len = jitter->packets[i].len; } else { if (jitter->packets[i].len > packet->len) { speex_warning_int("jitter_buffer_get(): packet too large to fit. Size is", jitter->packets[i].len); } else { packet->len = jitter->packets[i].len; } for (j = 0; j < packet->len; j++) packet->data[j] = jitter->packets[i].data[j]; speex_free(jitter->packets[i].data); } jitter->packets[i].data = NULL; offset = (spx_int32_t)jitter->packets[i].timestamp - (spx_int32_t)jitter->pointer_timestamp; if (start_offset != NULL) *start_offset = offset; else if (offset != 0) speex_warning_int("jitter_buffer_get() discarding non - zero start_offset", offset); packet->timestamp = jitter->packets[i].timestamp; jitter->last_returned_timestamp = packet->timestamp; packet->span = jitter->packets[i].span; packet->sequence = jitter->packets[i].sequence; packet->user_data = jitter->packets[i].user_data; jitter->pointer_timestamp = jitter->packets[i].timestamp + jitter->packets[i].span; jitter->buffered = packet->span - desired_span; if (start_offset != NULL) jitter->buffered += *start_offset; return JITTER_BUFFER_OK; } 解释
最新发布
09-11
### 代码功能概述 这段C语言代码的主要功能是在一个抖动缓冲区(`jitter`)中搜索时间戳匹配且跨度足够的数据包。若找到符合条件的数据包,就对其进行后续处理,像更新时间信息、复制数据包内容、释放内存等,最后返回操作成功的状态码。 ### 代码详细解释 #### 搜索符合条件的数据包 ```c for (i = 0; i < jitter->buffer_size; i++) { if (jitter->packets[i].data && jitter->packets[i].timestamp == jitter->pointer_timestamp && GE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp + desired_span)) break; } ``` - `for` 循环遍历抖动缓冲区中的所有数据包,范围是从 `0` 到 `jitter->buffer_size - 1`。 - `if` 语句用于检查当前数据包是否符合条件: - `jitter->packets[i].data`:判断当前数据包是否有数据。 - `jitter->packets[i].timestamp == jitter->pointer_timestamp`:检查当前数据包的时间戳是否与 `jitter->pointer_timestamp` 相等。 - `GE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp + desired_span)`:判断当前数据包的时间戳加上其跨度是否大于等于期望的时间戳加上期望的跨度。`GE32` 可能是一个宏,用于进行32位无符号整数的比较。 - 若符合条件,就使用 `break` 语句跳出循环。 #### 检查是否找到符合条件的数据包 ```c if (i != jitter->buffer_size) { // 找到符合条件的数据包,进行后续处理 } ``` - 若 `i` 不等于 `jitter->buffer_size`,就表明找到了符合条件的数据包,接着进行后续处理。 #### 重置丢失数据包计数 ```c spx_int32_t offset; jitter->lost_count = 0; ``` - 定义一个32位有符号整数 `offset`,用于存储时间戳偏移量。 - 将丢失数据包的计数 `jitter->lost_count` 重置为 `0`。 #### 更新时间信息 ```c if (jitter->arrival[i] != 0) { update_timings(jitter, ((spx_int32_t)jitter->packets[i].timestamp) - ((spx_int32_t)jitter->arrival[i]) - jitter->buffer_margin); } ``` - 若 `jitter->arrival[i]` 不为 `0`,就调用 `update_timings` 函数更新时间信息。`update_timings` 函数的参数是当前数据包的时间戳减去到达时间戳再减去缓冲区余量。 #### 复制数据包 ```c if (jitter->destroy) { packet->data = jitter->packets[i].data; packet->len = jitter->packets[i].len; } else { if (jitter->packets[i].len > packet->len) { speex_warning_int("jitter_buffer_get(): packet too large to fit. Size is", jitter->packets[i].len); } else { packet->len = jitter->packets[i].len; } for (j = 0; j < packet->len; j++) packet->data[j] = jitter->packets[i].data[j]; speex_free(jitter->packets[i].data); } jitter->packets[i].data = NULL; ``` - 若 `jitter->destroy` 为真,就直接将当前数据包的指针和长度赋值给 `packet`。 - 若 `jitter->destroy` 为假,就检查当前数据包的长度是否大于 `packet` 的长度。若大于,就输出警告信息;若小于等于,就将当前数据包的长度赋值给 `packet`,并逐字节复制数据包内容。 - 释放当前数据包的内存,并将其指针置为 `NULL`。 #### 计算时间戳偏移量 ```c offset = (spx_int32_t)jitter->packets[i].timestamp - (spx_int32_t)jitter->pointer_timestamp; if (start_offset != NULL) *start_offset = offset; else if (offset != 0) speex_warning_int("jitter_buffer_get() discarding non - zero start_offset", offset); ``` - 计算当前数据包的时间戳与 `jitter->pointer_timestamp` 的偏移量。 - 若 `start_offset` 不为 `NULL`,就将偏移量赋值给 `start_offset` 指向的变量。 - 若 `start_offset` 为 `NULL` 且偏移量不为 `0`,就输出警告信息。 #### 更新数据包信息 ```c packet->timestamp = jitter->packets[i].timestamp; jitter->last_returned_timestamp = packet->timestamp; packet->span = jitter->packets[i].span; packet->sequence = jitter->packets[i].sequence; packet->user_data = jitter->packets[i].user_data; ``` - 将当前数据包的时间戳、跨度、序列号和用户数据赋值给 `packet`。 - 更新 `jitter->last_returned_timestamp` 为当前数据包的时间戳。 #### 更新指针时间戳和缓冲区余量 ```c jitter->pointer_timestamp = jitter->packets[i].timestamp + jitter->packets[i].span; jitter->buffered = packet->span - desired_span; if (start_offset != NULL) jitter->buffered += *start_offset; ``` - 更新 `jitter->pointer_timestamp` 为当前数据包的时间戳加上其跨度。 - 计算缓冲区余量,即当前数据包的跨度减去期望的跨度。 - 若 `start_offset` 不为 `NULL`,就将偏移量加到缓冲区余量上。 #### 返回操作结果 ```c return JITTER_BUFFER_OK; ``` - 返回操作成功的状态码 `JITTER_BUFFER_OK`。 ### 相关问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值