2014年北京区域赛 hdu 5037 贪心
2016-11-20 22:36
169 查看
Frog
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3675 Accepted Submission(s): 918
[align=left]Problem Description[/align]
Once upon a time, there is a little frog called Matt. One day, he came to a river.
The river could be considered as an axis.Matt is standing on the left bank now (at position 0). He wants to cross the river, reach the right bank (at position M). But Matt could only jump for at most L units, for example from 0 to L.
As the God of Nature, you must save this poor frog.There are N rocks lying in the river initially. The size of the rock is negligible. So it can be indicated by a point in the axis. Matt can jump to or from a rock as well as the bank.
You don't want to make the things that easy. So you will put some new rocks into the river such that Matt could jump over the river in maximal steps.And you don't care the number of rocks you add since you are the God.
Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.
[align=left]Input[/align]
The first line contains only one integer T, which indicates the number of test cases.
For each test case, the first line contains N, M, L (0<=N<=2*10^5,1<=M<=10^9, 1<=L<=10^9).
And in the following N lines, each line contains one integer within (0, M) indicating the position of rock.
[align=left]Output[/align]
For each test case, just output one line “Case #x: y", where x is the case number (starting from 1) and y is the maximal number of steps Matt should jump.
[align=left]Sample Input[/align]
2
1 10 5
5
2 10 3
3
6
[align=left]Sample Output[/align]
Case #1: 2
Case #2: 4
题意:
一只青蛙一步最多可以跳 L 长
(0,m)原本有 n 个石头,你是上帝,你可以在里面任意放石头
但是青蛙很聪明,每一次都选择最优的一块石头跳
问上帝你,青蛙最多跳多少次(因为上帝的使坏)
题解:贪心
注意:石头给出的序号不一定是升序
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 200005
int a[MAXN];
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
int T;
int cases=1;
int n,m,k;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=0,a[++n]=m;
qsort(a,n,sizeof(a[0]),cmp);
int cnt=0;
int last=k;//上一步跳的距离
for(int i=1;i<=n;i++){
int x=(a[i]-a[i-1])%(k+1);
int y=(a[i]-a[i-1])/(k+1);
if(last+x>=k+1){//判断是否可以和上一段合并(要选择最优解)
last=x;
cnt+=y*2+1;
}
else{
last+=x;
cnt+=y*2;
}
}
printf("Case #%d: %d\n",cases++,cnt);
}
return 0;
}
相关文章推荐
- hdu 5037 Frog(2014年北京区域赛网赛F题1006)
- HDU 5037 Frog(2014年北京网络赛 F 贪心)
- hdu 5037 Galaxy 2014鞍山区域赛D(最小方差 贪心 想法)
- HDU 4089 Activation 概率DP 2011年区域赛北京现场赛I题
- HDU -- 5037 Frog (贪心)(2014北京网络赛)
- HDU 5037(Frog-贪心青蛙跳石子)
- hdu 5037 Frog 贪心
- hdu 5037 Frog(贪心)
- hdu 5040(2014年北京赛区网络赛)
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
- HDU 5035 Delivery (2014年北京赛区网络赛D题)
- HDU 4802 && HDU 4803 贪心,高精 && HDU 4804 轮廓线dp && HDU 4805 计算几何 && HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)
- HDU 4468 Spy KMP + 贪心 (2012成都区域赛E题)
- Hdu 5119 HappyMatt Friends (2014年北京现场赛H题)
- HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)
- hdu 5037 Frog(贪心)
- HDU 5037 FROG 贪心 2014北京网络赛F
- (复习) hdu 5037 frog~! 贪心策略
- HDU 5037 FROG (贪心)