lightoj 1425 - The Monkey and the Oiled Bamboo

    PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB

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?"

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 be 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). Thus 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 (≤ 12), denoting the number of test cases.

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

Output

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

Sample Input

Output for Sample Input

2

5

1 6 7 11 13

4

3 9 10 14

Case 1: 5

Case 2: 6


简单的二分加模拟,怕的就是题目没读懂。
求最小的满足条件的k值。
A[i] - a[i-1] <= k。
相等的话,k--。
显然就是二分。。。
点击打开链接
       :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 100010;
int A[maxn];
int n;
bool Judge(int mid){
	int Max = mid;
	for (int i = 2;i <= n;++i){
		if (A[i] - A[i - 1] > Max) return false;
		else if (A[i] - A[i - 1] == Max) Max--;
	}
	return true;
}
void Solve(){
	if (n == 1) printf("%d\n",A[1]);
	else{
		int ans;
		int high = A[n], low = A[1];
		while(high >= low){
			int mid = (high + low) / 2;
			if (Judge(mid)) {
				high = mid - 1;
				ans = mid;
			}
			else low = mid + 1;
		}
		printf("%d\n",ans);
	}
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for (int i = 1;i <= n;++i)
			scanf("%d",&A[i]);
		printf("Case %d: ", ++icase);
		Solve();
	}
	return 0;
}


你可能感兴趣的:(二分,lightoj)