CodeForces - 499 Watching a movie

You have decided to watch the best moments of some movie. There are two buttons on your player:

  1. 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.
  2. 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 nx (1 ≤ n ≤ 501 ≤ x ≤ 105) — the number of the best moments of the movie and the value of x for the second button.

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 liri (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行是每个片段的起止时间。

输出:所需的最少时间。

解题思路:在不损害自己喜欢片段的前提下,尽可能的跳过不喜欢的时间。

[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.     cin>>n>>t;// n:片段数目 t: 一次的跳过时间。  
  14.     for (int i=1; i<=n; i++) {  
  15.         cin>>a[i].beg>>a[i].end;  
  16.         sum=sum+(a[i].end-a[i].beg+1);//先加上喜欢片段的时间。  
  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.          d=(a[i+1].beg-a[i].end)*1.0/t;//第一个时间间隔(a[i+1].beg-a[i].end),d是整数倍的次数  
  22.          sum=sum+(a[i+1].beg-a[i].end)-d*t;  
  23.          }else{  
  24.             d=(a[i+1].beg-a[i].end-1)*1.0/t;//接下来每一个的时间间隔(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;  

  1. }</span>  
  2. 转载 gks 大佬
  3. //    

  1. 第二种
#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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值