UVA 757 Gone Fishing(贪心 + 暴力)

 Gone Fishing 

John is going on a fishing trip. He has h hours available ( $1 \leŸ h \leŸ 16$), and there are n lakes in the area ( $2 \leŸ n \leŸ 25$) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each $i = 1, \dots, n- 1$, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti ( $0 < t_i \leŸ 192$). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4.


To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi ( $f_i \ge– 0$), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di ( $d_i \ge– 0$). If the number of fish expected to be caught in an interval is less than or equal to di, there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.


Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.

Input 

You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi ( $1 \leŸ i \leŸ n$), then a line of n integers di ( $1 \leŸ i \leŸ n$), and finally, a line of n - 1 integers ti ($1 \leŸ i Ÿ\le n - 1$). Input is terminated by a case in which n = 0.

Output 

For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected. If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.

Sample Input 

2
1
10 1
2 5
2
4
4
10 15 20 17
0 3 4 3
1 2 3
4
4
10 15 50 30
0 3 4 3
1 2 3
0

Sample Output 

45, 5
Number of fish expected: 31

240, 0, 0, 0
Number of fish expected: 480

115, 10, 50, 35
Number of fish expected: 724

题意:单位时间为5分钟,给定n个池塘,和h个小时。每个池塘有两个属性f,d,f代表池塘初始可以钓到的鱼,d代表每个单位时间池塘可以钓到的鱼会减少d。在给定每个池塘之间路程所需要的单位时间。要求出在h小时内,最多能钓到多少鱼以及在每个池塘花掉的时间,人一开始在第一个池塘。

思路:暴力 + 贪心。要掉到更多的鱼。就要尽可能利用每一分每一秒。所以人从第一个池塘开始肯定是往后走不回头的。

所以我们只要枚举每一个区间,1到1,1到2,1到3。。。1到n的情况,每个区间可以用的时间为:总时间 - 路程花费时间。 找出其中最大的情况。在每种情况在用贪心去求。贪心的策略是:一个个单位时间去考虑,每次去找最大可以钓到的鱼数。这样到最后一定是钓得最多的鱼。

代码:

#include <stdio.h>
#include <string.h>
using namespace std;

int n, h, t[30], Max;
struct Lake {
    int f;
    int d;
    int t;
} l[30], ll[30], out[30];

int main() {
    int bo = 0;
    while (~scanf("%d", &n) && n) {
	scanf("%d", &h);
	h *= 12; Max = -1;
	for (int i = 0; i < n; i ++)
	    scanf("%d", &ll[i].f);
	for (int i = 0; i < n; i ++)
	    scanf("%d", &ll[i].d);
	for (int i = 0; i < n - 1; i ++)
	    scanf("%d", &t[i]);
	for (int i = 0; i < n; i ++) {
	    int sum = 0;
	    int time = h;
	    memset(l, 0, sizeof(l));
	    for (int j = 0; j < n; j ++)
		l[j] = ll[j];
	    for (int j = 0; j < i; j ++)
		time -= t[j];
	    while (time > 0) {
		int Maxx = 0, Max_v = 0;
		for (int k = 0; k <= i; k ++){//找最大可以钓到的鱼
		    if (l[k].f > Maxx) {
			Maxx = l[k].f;
			Max_v = k;
		    }
		}
		sum += l[Max_v].f;
		if (l[Max_v].f - l[Max_v].d >= 0)
		    l[Max_v].f -= l[Max_v].d;
		else 
		    l[Max_v].f = 0;
		l[Max_v].t += 5;
		time --;
	    }
	    if (Max < sum) {
		Max = sum;
		for (int i = 0; i < n; i ++) {
		    out[i].t = l[i].t;
		}
	    }
	}
	if (bo ++) printf("\n");//注意输出格式
	for (int i = 0; i < n - 1; i ++)
	    printf("%d, ", out[i].t);
	printf("%d\n", out[n - 1].t);
	printf("Number of fish expected: %d\n", Max);
    }	
    return 0;
}


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值