当前位置:编程学习 > C/C++ >>

Codeforces #208 div2前两题及思维风暴

昨晚原本准备在宿舍打cf的,结果吵吵闹闹的,也没打成,头也晕晕的,当时看了只看了第一个题,越想越麻烦,最后竟然陷入了误区,半小时也没解,虽然注册了,一发也没交。。。
 
 
 
 
A. Dima and Continuous Line
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
 
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 ≤ n ≤ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 ≤ xi ≤ 106) — the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Sample test(s)
input
4
0 10 5 15
output
yes
input
4
0 15 5 10
output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right.
 
 
 
题目意思很好懂,就看你怎么去建立模型,简化计算。早上起床灵光一闪。如果要是两线相交的话,必然存在公共区域。可以直接枚举任意的两条线,时间复杂度为O(10^6)。具体实现见代码。
 
 
 
AC代码:
 
 
[cpp]  
#include<iostream>  
#include<cstring>  
#include<string>  
#include<cstdio>  
#include<cmath>  
#include<algorithm>  
using namespace std;  
int a[1005];  
  
int main()  
{  
    int n,i,j;  
    while(cin>>n)  
    {  
        for(i=0;i<n;i++)  
            cin>>a[i];  
        int flag =0,mi1,mi2,ma1,ma2;  
        for(i=0;i<n-1;i++)  
        {  
            mi1=min(a[i],a[i+1]);  
            ma1=max(a[i],a[i+1]);  //第一条线mi1-ma1 第二条线mi2-ma2  
            for(j=i+2;j<n-1;j++)  
            {  
                mi2=min(a[j],a[j+1]);  
                ma2=max(a[j],a[j+1]);  
                if((mi2>mi1&&mi2<ma1&&ma2>ma1)||(mi2<mi1&&ma2>mi1&&ma2<ma1))  //相交  
                {  
                        flag=1;  
                        break;  
                }  
            }  
            if(flag) break;  
        }  
  
        if(flag) puts("yes");  
        else puts("no");  
    }  
    return 0;  
}  
  
/* 
4 6 7 1 2 3 5 
*/  
 
 
 
B. Dima and Text Messages
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.
Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (<) and the digit three (3). After applying the code, a test message looks like that: <3word1<3word2<3 ... wordn<3.
Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.
Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105.
The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
Output
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
Sample test(s)
input
3
i
love
you
<3i<3love<23you<3
output
yes
input
7
i
am
not
main
in
the
family
<3i<>3am<3the<3<main<3in<3the<3><3family<3
output
no
Note
Please note that Dima got a good o
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,