Codeforces Round #180 (Div. 2) AB

A. Snow Footprints
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i+1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i-1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.

Codeforces Round 180 (Div. 2) AB - qhn999 - 码代码的猿猿

At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in thet-th block. It is known that Alice never moves outside of the road.

You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,t by looking at the footprints.

Input

The first line of the input contains integer n (3≤n≤1000).

The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).

It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.

Output

Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.

Sample test(s)
input
9
..RRLL...
output
3 4
input
11
.RRRLLLLL..
output
7 5
Note

The first test sample is the one in the picture.



 
#include <iostream>


using namespace std;


char str[1005];

int sR=0,sL=0;

int posR,posL;


int main()

{

    int begin,end;

    int n;

    cin>>n;

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

    {

        cin>>str;

        if(str=='R')

        {

            if(sR==0)

                posR=i;

            sR=1;

        }

        if(str=='L')

        {

            if(sL==0)

                posL=i;

            sL=1;

        }

    }


    if(sR&&sL)

        {

            begin=posR+1;

            end=posL;

        }


    else if(sR==1&&sL==0)

    {

        begin=posR+1;

        for(int i=posR;i<n;i++)

        {

            if(str=='.')

            {

                end=i+1;

                break;

            }

        }

    }

    else if(sL==1&&sR==0)

    {

        end=posL;

        for(int i=posL;i<n;i++)

        {

            if(str=='.')

            {

                begin=i;

                break;

            }

        }

    }


    cout<<begin<<" "<<end<<endl;


    return 0;

}


B. Sail
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The polar bears are going fishing. They plan to sail from (sx,sy) to (ex,ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x,y).

  • If the wind blows to the east, the boat will move to (x+1,y).
  • If the wind blows to the south, the boat will move to (x,y-1).
  • If the wind blows to the west, the boat will move to (x-1,y).
  • If the wind blows to the north, the boat will move to (x,y+1).

Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x,y). Given the wind direction for t seconds, what is the earliest time they sail to (ex,ey)?

Input

The first line contains five integers t,sx,sy,ex,ey (1≤t≤105,-109sx,sy,ex,ey≤109). The starting location and the ending location will be different.

The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).

Output

If they can reach (ex,ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).

Sample test(s)
input
5 0 0 1 1
SESNW
output
4
input
10 5 3 3 6
NENSWESNEE
output
-1
Note

In the first sample, they can stay at seconds 13, and move at seconds 24.

In the second sample, they cannot sail to the destination.

 



#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    char tx='X',ty='X';
    char c;
    int t,sx,sy,ex,ey;
    cin>>t>>sx>>sy>>ex>>ey;

    int dx=ex-sx;
    int dy=ey-sy;

    if(dx>0)
    {
        tx='E';
    }
    else if(dx<0)
    {
        tx='W';
    }

    if(dy>0)
    {
        ty='N';
    }
    else if(dy<0)
    {
        ty='S';
    }

    int nx=abs(dx),ny=abs(dy);
    int ans=-2;
 //   cout<<tx<<" "<<ty<<endl;
    for(int i=0;i<t;i++)
    {
        cin>>c;
 //       cout<<c<<" i: "<<i<<" "<<nx<<" "<<ny<<endl;

            if(c==tx&&nx!=0)
            {
                nx--;
            }
            if(c==ty&&ny!=0)
             {
                ny--;
             }

        if(nx==0&&ny==0)
        {
            ans=i;
            break;
        }
    }

    cout<<ans+1<<endl;
    return 0;
}



 
原文地址:https://www.cnblogs.com/CKboss/p/3351092.html