Codeforces #208 div2前兩題及思維風暴

果7發表於2013-10-26

昨晚原本準備在宿舍打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程式碼:

#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;
}

/*
7
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 old kick in the pants for the second sample from the statement.




這個題目如果看懂的話,相當easy,關鍵是要看懂。。當時第一題沒搞出來就直接沒看了。前面給你n個串,在每個串前面加上<3後面加上>3連結在一起。最後在連結好的串裡插入任意欄位。再給你一個串,問滿足條件否?


開始原先想的就是先比較<3然後跟串一個一個挨著比較,每比較完一個,就再匹配一個<3。具體實現見程式碼。最後還需要匹配一個<3.

程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

string a[100005];
string p;

int main()
{
    int n,i,j;
    while(cin>>n)
    {
        for(i=0;i<n;i++)
            cin>>a[i];
        cin>>p;
        int flag=0;
        int fla=0;
        int s=0,t=0;
        for(i=0;i<p.length();i++)
        {
            if(s==n)
            {
                fla=1;
                break;
            }
            if(flag==0)  //先匹配<
            {
                if(p[i]=='<')
                    flag=1;
            }
            else if(flag==1)  //匹配3
            {
                if(p[i]=='3')
                    flag=2;
            }
            else
            {
                if(p[i]==a[s][t]&&t==a[s].length()-1)
                {
                    s++;
                    t=0;
                    flag=0;   //匹配一個串後需要重新匹配<3
                }
                else if(p[i]==a[s][t])
                {
                    t++;
                }
            }
        }

        //cout<<s<<" "<<i<<endl;
        flag=0;
        if(fla)   //匹配最後的<3
        {
            for(j=i;j<p.length();)
            {
                if(flag==0)  //匹配<
                {
                    if(p[j]=='<')
                        flag=1;
                    j++;
                }
                else if(flag==1)  //匹配3
                {
                    if(p[j]=='3')
                    {
                          fla=2;
                          break;
                    }
                    j++;
                }
            }
        }

        if(fla==2) puts("yes");
        else puts("no");
    }
    return 0;
}

/*
3
i
love
you
<3i<3lo<3ve<3y<<<<<<<ou3<3
1
a
<3a<2
2
a
i
<3ai<3
*/

//46MS


後來仔細想可以先把滿足條件最簡單的串構造出來,再匹配。估計是用了string +,時間有點多。

程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

string a;
string p;
string tmp;

int main()
{
    int n,i,j;
    while(cin>>n)
    {
        a="<3";  //先加在最前面
        for(i=0;i<n;i++)
        {
            cin>>tmp;
            a=a+tmp;
            a+="<3";  //每一串後面都加一個<3
        }

        //現在a串是最短長度的解
        cin>>p;
        int t=0,len=a.length();
        for(i=0;i<p.length();i++)
        {
            if(t==len) break;
            if(p[i]==a[t])
                t++;
        }
        if(t==len) puts("yes");
        else puts("no");
    }
    return 0;
}

//1684 ms


然後演算法設計老師講的一個題目蠻有意思的,給你兩根長短不同,粗細不均勻的繩子,我們僅僅知道每根繩子燒完需要1h,問怎麼才能使得兩根繩子都燒完,且剛好用時45min.



think........

用火機先點燃一根繩子的兩頭和另一根繩子的一頭。這樣用時30min,之後將剩下的繩子的另一頭點燃。


 還有一個題目是關於公倍數的,求解%3=a,%5=b,%7=c,這樣的最小的正整數t,我們假設t=(5*7*s1)*a+(3*7*s2)*b+(3*5*s3)*c.我們只需要保證5*7*s1%3=1即可,因此s1取2,依次s2=1,s3=1。我們可以將他的一個解找到,然後+lcm(3,5,7)*k就是他的通解了,就可以求解了。

相關文章