cf499A-Watching a movie
2015-07-22 22:07
309 查看
http://codeforces.com/problemset/problem/499/A
A. Watching a movie
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 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 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.
Sample test(s)
input
output
input
output
Note
In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.
In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie.
代码:
A. Watching a movie
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 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 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.
Sample test(s)
input
2 3 5 6 10 12
output
6
input
1 1 1 100000
output
100000
Note
In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.
In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie.
代码:
#include <fstream> #include <iostream> using namespace std; int main(){ //freopen("D:\\input.in","r",stdin); //freopen("D:\\output.out","w",stdout); int n,x,l,r,t=1,ans=0; scanf("%d%d",&n,&x); for(int i=1;i<=n;i++){ scanf("%d%d",&l,&r); ans+=(l-t)%x+r-l+1; t=r+1; } printf("%d\n",ans); return 0; }
相关文章推荐
- c++类
- PLsql 不安装Oracle也能使用
- 2015年7月22日20:04:31
- java容易忽略的基础知识
- [树状数组] 区间求和的三种模型
- 【BZOJ】1006 神奇的国度
- Android之——申请应用系统管理员权限
- 杭电1082Matrix Chain Multiplication
- 单链表的相关操作
- java获取视频缩略图
- 嵌入式编程:四平台免驱的usb设备(一)
- js刷新页面方法大全
- SpringMVC
- 测试
- HDU-1228-A+B(字符串处理)
- [Leetcode 117, Hard] Populating Next Right Pointers in Each Node II
- 对Java事务的认识
- IO流读取资源时类加载器的使用
- ubtuntu安装ssh服务
- LS -R实现