Problem Description
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.
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.
Input
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.
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.
Output
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.
Sample Input
2 1 10 5 5 2 10 3 3 6
Sample Output
Case #1: 2 Case #2: 4
Source
思路:贪心的做法,显然假设每次都让青蛙在L+1的长度跳两次是最优的。那么问题来了:假设要这么跳的话,那么在L+1的地方有一个石子。在[0, L+1]的地方也要有一个位置,可是要考虑到前面青蛙已经跳到的位置。由于我们是希望它仅仅能从0的位置開始跳的,假设前面的位置last+x(这段距离多出来的部分)<L+1的话,那么青蛙就能够跳过这个部分,我认为画张图比較好理解一点
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 200005; int n, m, l; int num[maxn]; int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%d%d%d", &n, &m, &l); for (int i = 1; i <= n; i++) scanf("%d", &num[i]); num[0] = 0, num[++n] = m; int ans = 0; int last = l; sort(num, num+n); for (int i = 1; i <= n; i++) { int x = (num[i] - num[i-1]) % (l+1); int y = (num[i] - num[i-1]) / (l+1); if (last + x >= l+1) { last = x; ans += 2 * y + 1; } else if (last + x < l+1) { last = last + x; ans += 2 * y; } } printf("Case #%d: %d ", cas++, ans); } return 0; }
版权声明:本文博客原创文章。博客,未经同意,不得转载。