标签:
解题思路:给出 n个电影的精彩时段(a[i],b[i]),和每次可以跳过的时间x,问要看完所有的精彩时刻,至少需要看多长时间的电影。 因为要时间最少,所有除了精彩时刻的电影则能跳过就跳过(用取余来算),不能跳过则加到耗费的总时间里面。
反思:WA两次是因为没有搞清楚物理上的时刻和时间的关系,
-------------------------------------------------- 1 2 3 4 5 6 7 8
如果我们给出一个电影精彩时段为(5,6)(表示的意思是从第5min开始到第6min结束,共持续了2min) 那么在上图的数轴中,它的长度为7-5=2 因为6这一点是第6min的开始,7这一点是第6min的结束,所以区间(6,7)代表整个第6min
A. Watching a movie
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
#include<stdio.h> #include<string.h> int a[10010],b[10010]; int main() { int n,x,i,t,sum; while(scanf("%d %d",&n,&x)!=EOF) { sum=0; for(i=1;i<=n;i++) scanf("%d %d",&a[i],&b[i]); b[0]=0; for(i=1;i<=n;i++) { sum+=(a[i]-b[i-1]-1)%x+b[i]-a[i]+1; } printf("%d\n",sum); } }
Codeforces Round #284 (Div. 2) A
标签:
原文地址:http://www.cnblogs.com/wuyuewoniu/p/4185919.html