You have decided to watch the best moments of some movie. There are two buttons on your player:
- Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie.
- Skip exactly x minutes of the movie (x is some fixed positive integer). If the player is now at the t-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (t + x).
Initially the movie is turned on in the player on the first minute, and you want to watch exactly n best moments of the movie, the i-th best moment starts at the li-th minute and ends at the ri-th minute (more formally, the i-th best moment consists of minutes: li, li + 1, ..., ri).
Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments?
Input
The first line contains two space-separated integers n, x (1 ≤ n ≤ 50, 1 ≤ x ≤ 105) — the number of the best moments of the movie and the value of x
The following n lines contain the descriptions of the best moments of the movie, the i-th line of the description contains two integers separated by a space li, ri (1 ≤ li ≤ ri ≤ 105).
It is guaranteed that for all integers i from 2 to n the following condition holds: ri - 1 < li.
Output
Output a single number — the answer to the problem.
Example
Input
2 3 5 6 10 12
Output
6
Input
1 1 1 100000
Output
100000
题目大意:你现在准备看电影,播放器上有一个按钮,点击按钮可以跳过x分钟,你想尽可能的看自己喜欢的片段。求看完自己喜欢的片段所需的最少时间。
输入:n(喜欢片段数目) x(一次可以跳过的·时间)
下面n行是每个片段的起止时间。
输出:所需的最少时间。
解题思路:在不损害自己喜欢片段的前提下,尽可能的跳过不喜欢的时间。
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n,t,x,y,cnt=0,m=1;
scanf("%d %d",&n,&t);
while(n--)
{
scanf("%d %d",&x,&y);
while(m+t<=x)
{
m+=t;
}
cnt+=(y-m+1);
m=y+1;
}
printf("%d\n",cnt);
}
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n,t,x,y,cnt=0,m=1;
scanf("%d %d",&n,&t);
while(n--)
{
scanf("%d %d",&x,&y);
while(m+t<=x)
{
m+=t;
}
cnt+=(y-m+1);
m=y+1;
}
printf("%d\n",cnt);
}
[cpp]
view plain
copy
1. <span style="font-size:14px;">#include <iostream>
2. #include<cstdio>
3. #include<cmath>
4. #include<string>
5. #include<cstring>
6. #include<algorithm>
7. using namespace std;
8. struct fav{
9. int beg,end;
10. }a[1000];//用结构体记录片段的起止时间。
11. int main() {
12. int n,t,sum=0,d;//sum记录最短时间。
13. // n:片段数目 t: 一次的跳过时间。
14. for (int i=1; i<=n; i++) {
15. cin>>a[i].beg>>a[i].end;
16. //先加上喜欢片段的时间。
17. }
18. a[0].beg=1;a[0].end=1;
19. for (int i=0; i<n; i++) {//这个for循环就是尽可能的跳过不喜欢的时间;我们每次只能跳过整数倍的t;不是整数倍的自已要看完。
20. if (i==0) {
21. //第一个时间间隔(a[i+1].beg-a[i].end),d是整数倍的次数
22. sum=sum+(a[i+1].beg-a[i].end)-d*t;
23. else{
24. //接下来每一个的时间间隔(a[i+1].beg-a[i].end-1),
25. sum=sum+(a[i+1].beg-a[i].end-1)-d*t;
26. }
27. }
28. cout<<sum<<endl;
29. return 0;
30. }</span>
31. 转载 gks 大佬
32. //
33. 第二种