A. Watching a movie
time limit per test
memory limit per test
input
output
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.
- 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).
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
n, x (1 ≤ n ≤ 50, 1 ≤ x ≤ 105) — the number of the best moments of the movie and the value of x
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).
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
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
100000
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
struct node
{
int l;
int r;
}q[100100];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
}
int t = 1;
int p;
int k = 0;
int cnt = 0;
while(t<=q[n-1].r)
{
p = (q[k].l - t)%m;
cnt += p + (q[k].r - q[k].l + 1);
t = q[k].r+1;
k++;
}
printf("%d\n",cnt);
}
return 0;
}
B. Lecture
time limit per test
memory limit per test
input
output
You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.
You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.
You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.
You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.
Input
n and m (1 ≤ n ≤ 3000, 1 ≤ m ≤ 3000) — the number of words in the professor's lecture and the number of words in each of these languages.
m lines contain the words. The i-th line contains two strings ai, bi meaning that the word ai belongs to the first language, the word bi
n space-separated strings c1, c2, ..., cn — the text of the lecture. It is guaranteed that each of the strings cibelongs to the set of strings {a1, a2, ... am}.
10
Output
n
Sample test(s)
input
4 3 codeforces codesecrof contest round letter message codeforces contest letter contest
output
codeforces round letter round
input
5 3 joll wuqrd euzf un hbnyiyc rsoqqveh hbnyiyc joll joll euzf joll
output
hbnyiyc joll joll un joll
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
char x[21];
char y[21];
}q[3101];
char a[3101][21];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<m;i++)
{
scanf("%s%s",q[i].x,q[i].y);
}
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(strcmp(a[i],q[j].x) == 0 && strlen(q[j].x)>strlen(q[j].y))
{
strcpy(a[i],q[j].y);
}
}
}
for(int i=0;i<n;i++)
{
if(i == 0)
{
printf("%s",a[i]);
}
else
{
printf(" %s",a[i]);
}
}
printf("\n");
}
return 0;
}