Back and Forth 题解

FarmerJohn在两个挤奶棚间运输牛奶,通过不同容量的桶进行五天的牛奶转移,目标是计算周五后第一个挤奶棚可能的牛奶总量。此问题涉及枚举所有可能的转移组合,并统计最终可能的牛奶读数数量。

BackBackBack andandand ForthForthForth 题解

题目

题目描述
Farmer John有两个挤奶棚,每个挤奶棚里各有一个奶罐和一个装有10个各种尺寸的桶的储物柜。他喜欢将在两个挤奶棚之间来回运送牛奶作为一种锻炼方式。
周一,Farmer John量了恰好1000加仑的牛奶放在第一个挤奶棚的奶罐里,又量了恰好1000加仑的牛奶放在第二个挤奶棚的奶罐里。
周二,他从第一个挤奶棚里取出一个桶,并装满牛奶,然后将牛奶运到第二个挤奶棚,并将牛奶倒进奶罐。他把这个桶留在了第二个挤奶棚。
周三,他从第二个挤奶棚里取出一个桶(可能是周二留在这里的),并装满牛奶,然后将牛奶运到第一个挤奶棚,并将牛奶倒进奶罐。他把这个桶留在了第一个挤奶棚。
周四,他从第一个挤奶棚里取出一个桶(可能是周三留在这里的),并装满牛奶,然后将牛奶运到第二个挤奶棚,并将牛奶倒进奶罐。他把这个桶留在了第二个挤奶棚。
周五,他从第二个挤奶棚里取出一个桶(可能是周二或周四留在这里的),并装满牛奶,然后将牛奶运到第一个挤奶棚,并将牛奶倒进奶罐。他把这个桶留在了第一个挤奶棚。
此时Farmer John测量了第一个挤奶棚的奶罐里的牛奶。他总共可能得到多少种不同的读数?

输入
输入的第一行包含10个整数,为第一个挤奶棚里初始的桶的容积。输入的第二行也包含10个整数,为第二个挤奶棚里初始的桶的容积。所有桶的容积均在1100的范围内。

输出
输出Farmer John在周五之后测量第一个挤奶棚里的奶罐的牛奶时可能得到的读数的数量。

样例输入
1 1 1 1 1 1 1 1 1 2
5 5 5 5 5 5 5 5 5 5

样例输出
5

数据范围限制

提示
在这个例子中,最后第一个挤奶棚的奶罐中的牛奶量总共有5种可能的结果:
1000:FJ可以在每次往返的时候都携带同一个桶,从而不会改变第一个挤奶棚的奶罐的牛奶量。
1003:FJ可以在周二运送2个单位,周三5个单位,周四1个单位,周五1个单位。
1004:FJ可以在周二运送1个单位,周三5个单位,周四1个单位,周五1个单位。
1007:FJ可以在周二运送1个单位,周三5个单位,周四2个单位,周五5个单位。
1008:FJ可以在周二运送1个单位,周三5个单位,周四1个单位,周五5个单位。

解题方法

直接dfsdfsdfs或者暴力枚举,其实都是一样的。
我们只要枚举出所有情况,然后用一个标记数组来存(我怕会爆,所以没有用这种方法),最后统计。
我的方法:
把所有情况列出来并排序。
接下来设fif_ifi表示iii之前(包括iii)一共有多少种类型,则
fi={fi−1+1ai≠ai−1fi−1ai=ai−1f_i=\begin{cases} f_{i-1}+1 & a_i\not=a_{i-1}\\ f_{i-1} & a_i=a_{i-1} \end{cases}fi={fi1+1fi1ai=ai1ai=ai1
答案就是fnf_nfn
注:aaa表示排完序的序列,nnn表示aaa的元素个数。

c++题解,代码无注释 T-2 Rabbits and Grass 分数 35 作者 陈越 单位 浙江大学 There is a proverb(谚语)saying that “rabbits don’t eat grass around their nests”(兔子不吃窝边草). Now given several blocks of grass that are trimmed in a line. Assume that a rabbit starts eating from any block, and will never eat the neighboring blocks next. Your job is to tell the maximum amount of grass this rabbit can eat. Input Specification: Each input file contains one test case. For each case, two positive integers are given in the first line, namely N (≤10 5 ), the number of blocks, and i (1≤i≤N), the block index that the rabbit starts eating. Then N positive integers (≤10 3 ) are given in the next line, separated by spaces. The integers are the amount of grass in the corresponding blocks. Output Specification: Print in a line the maximum amount of grass this rabbit can eat. Then in the next line, print in order the indices of the blocks that rabbit eats. All the numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line. Note: we have to assume that the rabbit only jumps in one direction. Otherwise if it jumps back and forth, it can eat up all the grass. If both directions are okay for the rabbit, it always prefer jumping toward left. And more, if there are more than one block to choose from, the rabbit always prefer the nearest block it can jump to. Sample Input 1: 10 4 2 1 4 3 1 1 5 2 3 1 Sample Output 1: 11 4 7 9 Sample Input 2: 10 8 2 1 4 3 1 1 5 2 3 1 Sample Output 2: 9 8 6 3 1 代码长度限制 16 KB Java (javac) 时间限制 800 ms 内存限制 512 MB 其他编译器 时间限制 150 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值