UVA12032--贪心

Description

Download as PDF

It's time to remember the disastrous moment of the old school math. Yes, the little math problem with the monkey climbing on an oiled bamboo. It goes like:

``A monkey is trying to reach the top of an oiled bamboo. When he climbs up 3 feet, he slips down 2 feet. Climbing up 3 feet takes 3 seconds. Slipping down 2 feet takes 1 second. If the pole is 12 feet tall, how much time does the monkey need to reach the top?"

\epsfbox{p12032.eps}

When I was given the problem, I took it seriously. But after a while I was thinking of killing the monkey instead of doing the horrible math! I had rather different plans (!) for the man who oiled the bamboo.

Now we, the problem-setters, got a similar oiled bamboo. So, we thought we could do better than the traditional monkey. So, I tried first. I jumped and climbed up 3.5 feet (better than the monkey! Huh!) But in the very next second I just slipped and fell off to the ground. I couldn't remember anything after that, when I woke up, I found myself in a bed and the anxious faces of the problem setters around me. So, like old school times, the monkey won with the oiled bamboo.

So, I made another plan (somehow I want to beat the monkey), I took a ladder instead of the bamboo. Initially I am on the ground. In each jump I can jump from the current rung (or the ground) to the next rung only (can't skip rungs). Initially I set my strength factor k. The meaning of k is, in any jump I can't jump more than k feet. And if I jump exactly k feet in a jump, k is decremented by 1. But if I jump less than k feet, k remains same.

For example, let the height of the rungs from the ground are 1, 6, 7, 11, 13 respectively and k be 5. Now the steps are:

  1. Jumped 1 foot from the ground to the 1st rung (ground to 1). Since I jumped less than k feet, k remains 5.
  2. Jumped 5 feet for the next rung (1 to 6). So, k becomes 4.
  3. Jumped 1 foot for the 3rd rung (6 to 7). So, k remains 4.
  4. Jumped 4 feet for the 4th rung (7 to 11). This k becomes 3.
  5. Jumped 2 feet for the 5th rung (11 to 13). And so, k remains 3.

Now you are given the heights of the rungs of the ladder from the ground, you have to find the minimum strength factor k, such that I can reach the top rung.

Input

Input starts with an integer   T  ( $ \le$500), denoting the number of test cases.

Each case starts with a line containing an integer n denoting the number of rungs in the ladder. The next line contains n space separated integers, r1r2,..., rn ( 1$ \le$r1 < r2 < ... < rn$ \le$107) denoting the heights of the rungs from the ground.

For all cases, 1$ \le$n$ \le$10, except 5 cases where 10 < n$ \le$105.

Output

For each case, print the case number and the minimum value of   k  as described above.

Sample Input

2
5
1 6 7 11 13
4
3 9 10 14

Sample Output

Case 1: 5
Case 2: 6
//很简单的贪心思想,从后面往前面做。
#include <iostream>
#include <algorithm>
using namespace std;
int A[100008];
int B[100008];
int main()
{
	int t;
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		int n;
		cin>>n;
		A[0]=0;
		for(int j=1;j<=n;j++)
		{
			cin>>A[j];
		}
		int sum=0;
		sort(A+1,A+n+1);
		for(int j=1;j<=n;j++)
		{
			B[j]=A[j]-A[j-1];
		}
		int k=-1;
		for(int j=n;j>=1;j--)
		{
			if(k==B[j])
			{
				k++;
			}
			if(k<B[j])
			{
				k=B[j];
			}
		}
		cout<<"Case "<<i<<": "<<k<<endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值