Picking Strawberries

FJNU.1754

Description
There are N hills distributing near Jesse’s home and they are numbered as 1,2,…,N from west to east. Now Jesse has H hours’ leisure time, and he wants to pick as many strawberries as possible in the H hours. Jesse starts from the first hill and walks eastward. He will stay in some hills for some time to pick strawberries. Suppose that Jesse will spend M i-1 minutes walking from the (i-1)th hill to the ith hill, and after arriving at the ith hill, he can pick Zi strawberries in the first one minute and then the amount of the strawberries which he can pick will reduce Ci for every successive minute on the ith hill. Please find out the maximum amount of the strawberries Jesse can pick in H hours.

Input
The first line of the input is a positive integer K denoting the number of test cases. The following are the K test cases each of which consists of several lines. For each test case, the first line includes two positive integers N (2<=N<=25) and H(H<=4) where the first integer N denotes the number of the hills and the second integer H denotes the number of the leisure hours Jesse has. In the ith line of the following N lines, there are two positive integers Zi (Zi<=200)and Ci (Ci<=Zi) , denoting respectively the amount of the strawberries Jesse can pick in the first one minute and the reducing amount of the strawberries he can pick for the every successive minute on the ith hill. The last line of each test case includes N-1 positive integers Mi (Mi<=10), where the ith integer denotes the minutes Jesse takes walking from the ith hill to the (i+1)th hill, i=1, 2, …, N-1. The integers in the same line are separated by spaces.

Output
For each test case, output a line including one integer indicating the maximum amount of the strawberries Jesse can pick in H hours.

Sample Input
2
4 2
150 3
50 1
90 2
70 1
2 4 8
3 1
30 1
40 2
20 1
6 3

Sample Output
7481
1011

Source
福建师范大学第四届程序设计竞赛

My Program

#include < stdio.h >
#define  N 25

int  main()
{
    
int z[N],c[N],m[N],y[N];
    
int n,h,k,i,j,s=0,d,t,f;
    scanf(
"%d",&k);
    
while(k)
    
{
        t
=0;
        scanf(
"%d%d",&n,&h);
        h
*=60;
        
for(i=0;i<n;i++)
            scanf(
"%d%d",&y[i],&c[i]);
        
for(i=1;i<n;i++)
            scanf(
"%d",&m[i]);
        
for(j=n;j>=1;j--)
        
{
            f
=h;
            d
=s=0;
            
for(i=0;i<n;i++)
                z[i]
=y[i];
            
for(i=1;i<j;i++)
                s
+=m[i];
            f
-=s;
            
while(f)
            
{
                s
=0;
                
while(z[s]<0&&s<j)
                    s
++;
                
if(s>=j)
                    
break;
                
for(i=1;i<j;i++)
                    
if(z[i]>z[s])
                        s
=i;
                d
+=z[s];
                z[s]
-=c[s];
                f
--;
            }

            
if(d>t)
                t
=d;
        }

        k
--;
        printf(
"%d ",t);
    }

    
return 0;
}

 

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

N座山上每分钟可以采摘Zi个草莓,每经过一次采摘该座山上每分钟可采摘的草莓数会减少Ci,从第i座山到第i+1座山需要消耗Mi分钟的走路时间。现在从第1座采草莓,问在H小时内最多可采摘到的草莓数量。


【粗略分析】
典型的贪心算法。我们可以先减去走路需要的时间,
然后选取可采摘最多的草莓数的山。
局部最优,符合贪心的特性。
同时我们要考虑可能不需要走遍全部山的可能性,
这个时候就要加上枚举。
枚举从1到1,从1到2,……,从1到N等N种情况后,
取出最多可采摘到的草莓数量即可。

【C源代码】

#include < stdio.h >
#define  N 25

int  main()
{
    
int z[N],c[N],m[N],y[N];
    
int n,h,k,i,j,s=0,d,t,f;
    scanf(
"%d",&k);
    
while(k)
    
{
        t
=0;
        scanf(
"%d%d",&n,&h);
        h
*=60;
        
for(i=0;i<n;i++)
            scanf(
"%d%d",&y[i],&c[i]);
        
for(i=1;i<n;i++)
            scanf(
"%d",&m[i]);
        
for(j=n;j>=1;j--)
        
{
            f
=h;
            d
=s=0;
            
for(i=0;i<n;i++)
                z[i]
=y[i];
            
for(i=1;i<j;i++)
                s
+=m[i];
            f
-=s;
            
while(f)
            
{
                s
=0;
                
while(z[s]<0&&s<j)
                    s
++;
                
if(s>=j)
                    
break;
                
for(i=1;i<j;i++)
                    
if(z[i]>z[s])
                        s
=i;
                d
+=z[s];
                z[s]
-=c[s];
                f
--;
            }

            
if(d>t)
                t
=d;
        }

        k
--;
        printf(
"%d ",t);
    }

    
return 0;
}

【注意事项】

※ 每次枚举新的数时都要用初始值噢。
※ 有可能减啊减啊所有的山上的草莓都<=0了,这个时候就不采摘了。


【点评】

 练手题。。注意事项注意下就好了。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值