UVa12032 - The Monkey and the Oiled Bamboo(贪心)
2014-07-08 21:48
771 查看
It's time to remember the disastrous moment of the old school math. Yes, the little math problemwith 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 2feet. Climbing up 3 feet takes 3 seconds. Slipping down 2 feet takes 1 second. If the pole is 12 feettall, how much time does the monkey need to reach
the top?"
When I was given the problem, I took it seriously. Butafter a while I was thinking of killing the monkey insteadof 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 traditionalmonkey. So, I tried first. I jumped and climbed up 3.5 feet(better than the monkey! Huh!) But in the very nextsecond I just slipped and fell off
to the ground. I couldn'tremember anything after that, when I woke up, I foundmyself 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 thebamboo. Initially I am on the ground. In each jump I can jump from the current rung (or theground) to the next rung only (can't skip rungs). Initially I set my strength
factor k. The meaning ofk is, in any jump I can't jump more thank feet. And if I jump exactly
k feet in a jump, k isdecremented by 1. But if I jump less thank feet,
k remains same.
For example, let the height of the rungs from the ground are 1, 6, 7, 11, 13 respectively andk be 5.Now the steps are:
Jumped 1 foot from the ground to the 1st rung (ground to 1). Since I jumped less thank feet,k remains 5.
Jumped 5 feet for the next rung (1 to 6). So, k becomes 4.
Jumped 1 foot for the 3rd rung (6 to 7). So, k remains 4.
Jumped 4 feet for the 4th rung (7 to 11). This k becomes 3.
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 theminimum strength factork, such that I can reach the top rung.
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. Thenext line containsn space separated integers,
r1, r2,..., rn (1
r1
<r2 < ... < rn
107) denoting theheights of the rungs from the ground.
For all cases, 1
n
10,
except 5 cases where 10 < n
105.
k as described above.
刚开始计算是从0到n-1,提交Wrong answer,例如1 2 3 6计算结果是4,但是从n-1到0计算结果是3
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100005;
int r
, a
;
int n;
bool input();
int solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
input();
int ans = solve();
printf("Case %d: %d\n", i, ans);
}
return 0;
}
bool input()
{
scanf("%d", &n);
r[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &r[i]);
}
}
int solve()
{
for (int i = 0; i < n; i++) {
a[i] = r[i + 1] - r[i];
}
int ans = -1;
for (int i = n - 1; i >= 0; i--) {
if (ans == a[i]) ans++;
ans = max(ans, a[i]);
}
return ans;
}
``A monkey is trying to reach the top of an oiled bamboo. When he climbs up 3 feet, he slips down 2feet. Climbing up 3 feet takes 3 seconds. Slipping down 2 feet takes 1 second. If the pole is 12 feettall, how much time does the monkey need to reach
the top?"
When I was given the problem, I took it seriously. Butafter a while I was thinking of killing the monkey insteadof 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 traditionalmonkey. So, I tried first. I jumped and climbed up 3.5 feet(better than the monkey! Huh!) But in the very nextsecond I just slipped and fell off
to the ground. I couldn'tremember anything after that, when I woke up, I foundmyself 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 thebamboo. Initially I am on the ground. In each jump I can jump from the current rung (or theground) to the next rung only (can't skip rungs). Initially I set my strength
factor k. The meaning ofk is, in any jump I can't jump more thank feet. And if I jump exactly
k feet in a jump, k isdecremented by 1. But if I jump less thank feet,
k remains same.
For example, let the height of the rungs from the ground are 1, 6, 7, 11, 13 respectively andk be 5.Now the steps are:
Jumped 1 foot from the ground to the 1st rung (ground to 1). Since I jumped less thank feet,k remains 5.
Jumped 5 feet for the next rung (1 to 6). So, k becomes 4.
Jumped 1 foot for the 3rd rung (6 to 7). So, k remains 4.
Jumped 4 feet for the 4th rung (7 to 11). This k becomes 3.
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 theminimum strength factork, such that I can reach the top rung.
Input
Input starts with an integer T (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. Thenext line containsn space separated integers,
r1, r2,..., rn (1
r1
<r2 < ... < rn
107) denoting theheights of the rungs from the ground.
For all cases, 1
n
10,
except 5 cases where 10 < n
105.
Output
For each case, print the case number and the minimum value ofk 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
刚开始计算是从0到n-1,提交Wrong answer,例如1 2 3 6计算结果是4,但是从n-1到0计算结果是3
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100005;
int r
, a
;
int n;
bool input();
int solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
input();
int ans = solve();
printf("Case %d: %d\n", i, ans);
}
return 0;
}
bool input()
{
scanf("%d", &n);
r[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &r[i]);
}
}
int solve()
{
for (int i = 0; i < n; i++) {
a[i] = r[i + 1] - r[i];
}
int ans = -1;
for (int i = n - 1; i >= 0; i--) {
if (ans == a[i]) ans++;
ans = max(ans, a[i]);
}
return ans;
}
相关文章推荐
- UVa 12032 - The Monkey and the Oiled Bamboo
- LightOJ - 1425 The Monkey and the Oiled Bamboo(贪心)
- LightOJ - 1425 The Monkey and the Oiled Bamboo (水)
- lightoj 1425 - The Monkey and the Oiled Bamboo
- lightoj 1425 - The Monkey and the Oiled Bamboo 二分答案or乱搞
- 【贪心】【uva11520】 Fill the Square
- uva - 539 - The Settlers of Catand
- UVA 11292 The Dragon of Loowater(贪心)
- 贪心结合动态规划-POJ-1069-Monkey and Banana
- UVA 10249 - The Grand Dinner(网络流 or 贪心)
- uva 11389 - The Bus Driver Problem(贪心)
- uva 1153 - Keep the Customer Satisfied(贪心+优先队列)
- uva11292 The Dragon of Loowater (排序+贪心)
- UVa 1153 Keep the Customer Satisfied 解题报告(贪心)
- UVALive 6263 The Dragon and the knights --统计,直线分平面
- UVa 243 - Theseus and the Minotaur (II)
- UVA 168 - Theseus and the Minotaur
- UVA 1344 - Tian Ji -- The Horse Racing(贪心)
- Codeforces Round #177 (Div. 1)C. Polo the Penguin and XOR operation【贪心】
- UVa 913 - Joana and the Odd Numbers