Codeforces Round #284 (Div. 2)

题目链接:http://codeforces.com/contest/499

A. 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?

题意:你正在观看电影,现在有两个按钮1和2,如果你在电影t分钟时选择按钮1,电影会进入到t+1分钟;如果你在t分钟选择按钮2,电影在跳到t+x分钟。现在给出了电影中n个你想要看的精彩连续片段,假如你此时在电影的开头(即第一分钟),求出你最少要观看电影多少分钟。

解法:题目中给出的n个片段[Li,Ri]都是按照时间顺序排好了的(Ri-1<Li),所以我们不需要排序,直接模拟这个过程即可。

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<cmath>

 6 #include<algorithm>

 7 #define inf 0x7fffffff

 8 using namespace std;

 9 const int maxn=55;

10 int main()

11 {

12     int n;

13     int l,L,R;

14     int x;

15     while (scanf("%d%d",&n,&x)!=EOF)

16     {

17         int total=0;

18         l=1;

19         for (int i=0 ;i<n ;i++)

20         {

21             scanf("%d%d",&L,&R);

22             while (l+x<=L) {l+=x;if (l+x>L) break; }

23             total += R-l+1;

24             l=R+1;

25         }

26         printf("%d\n",total);

27     }

28     return 0;

29 }
View Code

B. Lecture

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.

题意:给出n行单词表,每一行两个意思相同、拼写不同的单词,每个单词出现一次。接下来给出m个上述单词表出现过的单词,要求对m个单词中每个词输出单词表中对应的一行中的一个单词,输出要求:输出单词长度短的那一个;如果两个单词长度一样,则输出单词表中对应那一行的第一个单词。

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<cmath>

 6 #include<algorithm>

 7 #define inf 0x7fffffff

 8 using namespace std;

 9 const int maxn=3000+10;

10 char str[maxn][2][15];

11 int n,m;

12 char S[15];

13 int main()

14 {

15     while (scanf("%d%d",&n,&m)!=EOF)

16     {

17         for (int i=0 ;i<m ;i++) scanf("%s%s",str[i][0],str[i][1]);

18         for (int i=0 ;i<n ;i++)

19         {

20             scanf("%s",S);

21             for (int j=0 ;j<m ;j++)

22             {

23                 int len=strlen(str[j][0]);

24                 int len2=strlen(str[j][1]);

25                 if (strcmp(str[j][0],S)==0 || strcmp(str[j][1],S)==0) {

26                 if (len>len2)

27                 {

28                     if (i) printf(" ");

29                     printf("%s",str[j][1]);

30                     break;

31                 }

32                 else

33                 {

34                     if (i) printf(" ");

35                     printf("%s",str[j][0]);

36                     break;

37                 }

38                 }

39             }

40         }

41         printf("\n");

42     }

43     return 0;

44 }
View Code

C. Crazy Town

Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and biare not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.

Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).

Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.

题意:给出n条不重叠的直线,把平面划分了很多区域,给出了你家的位置和学校的位置(都保证在区域里,不会在交点和直线上),求出从家的位置到学校的最少步数(一步定义为从一个区域内部到和它有公共的长度不为0的边界的区域内部)。

Codeforces Round #284 (Div. 2)

说明:上图中A和B的距离就为2(因为A和B区域没有公共的不为0的边界,不能直接到达)。

解法:比赛最开始想到的就是求出每个区域,然后给每个区域编号,相邻区域连一条边,权值为1,不相邻的为无限大,然后Dijkstra算法或者Floyd算法搞。后来想想MD这么复杂。思考了一会,发现我们知道了A和B的位置,如果给出当前一条直线,这条直线让AB两点同侧的话,就对AB的最短路径没有影响,如果让AB异侧,就等同于在AB路径上加1即可,这样就简单多了。

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<cmath>

 6 #include<algorithm>

 7 #define inf 0x7fffffff

 8 #define exp 1e-10

 9 #define PI 3.141592654

10 using namespace std;

11 typedef long long ll;

12 int main()

13 {

14     ll x1,y1,x2,y2;

15     int n;

16     ll a,b,c;

17     while (scanf("%I64d%I64d",&x1,&y1)!=EOF)

18     {

19         scanf("%I64d%I64d",&x2,&y2);

20         scanf("%d",&n);

21         ll ans=0;

22         for (int i=0 ;i<n ;i++)

23         {

24             scanf("%I64d%I64d%I64d",&a,&b,&c);

25             ll num=a*x1+b*y1+c;

26             ll num2=a*x2+b*y2+c;

27             if ((num<0&&num2>0)||(num>0&&num2<0)) ans++;

28         }

29         printf("%I64d\n",ans);

30     }

31     return 0;

32 }
View Code

 

 

 

 

 

 

 

后续:感谢提出宝贵的意见。。。

 

你可能感兴趣的:(codeforces)