Codeforces Round #284 (Div. 2)---A. Watching a movie (暴力 + 贪心)

来源:互联网 发布:软件缺陷预测 编辑:程序博客网 时间:2024/06/06 00:23

Watching a movie
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
2 35 610 12
output
6
input
1 11 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.





题意:看一场电影,电影中不是所有的片段都很好看,已知所有n个有趣的片段的开始和结束时间,并且是按时间顺序给出的,现规定不想看的片段,你可以跳过,但是每次必须跳过x分钟,当然你也可以选择不跳过。问要看完全部的有趣片段,最少需要多少时间。起始时间从1开始。


分析:这个应该就是想清楚里面的关系就行了,我们用一个变量t记录一下跳过之后的时间,t的初始值赋为1,由于是按时间顺序给出的,所以,我们先判断 t + x 是否小于第i个有趣片段的起始时间,若小于,则说明可以跳过一个x,此时令 t += x ,重复以上操作,直到 t + x > a[i](有趣片段的开始时间),此时用ans(ans 初始为0)纪录结果,ans += b[i] - t + 1,其中b[i]是第i个有趣片段的结束时间,同时修改 t = b[i] + 1。重复以上所有操作n次即可将所有的n个有趣片段全部看完。




AC代码:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7fffffffint a[55], b[55];           //a[]有趣片段起始时间,b[]有趣片段结束时间int main(){    #ifdef sxk        freopen("in.txt","r",stdin);    #endif    int n, x;    while(scanf("%d%d",&n, &x)!=EOF)    {        for(int i=0; i<n; i++)            scanf("%d%d", &a[i], &b[i]);        int t = 1, ans = 0;        for(int i=0; i<n; i++){            while(t + x<= a[i]){        //尽量跳过最多的时间(贪心)                t += x;            }            if(t <= b[i]){                              ans += b[i] - t + 1;     //纪录结果                t = b[i] + 1;            //修改t            }        }        printf("%d\n", ans);    }    return 0;}



PS:这种体就是乱搞题,也没什么算法思想,只是要能想的到,还有就是细节处理了,我比赛的时候脑残,就忘了更新t,结果WA了一发。。。



0 0
原创粉丝点击