【codeforces 750B】New Year and North Pole

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40 000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20 000 kilometers.

Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of n parts. In the i-th part of his journey, Limak should move ti kilometers in the direction represented by a string diri that is one of: “North”, “South”, “West”, “East”.

Limak isn’t sure whether the description is valid. You must help him to check the following conditions:

If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South.
If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North.
The journey must end on the North Pole.
Check if the above conditions are satisfied and print “YES” or “NO” on a single line.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 50).

The i-th of next n lines contains an integer ti and a string diri (1 ≤ ti ≤ 106, ) — the length and the direction of the i-th part of the journey, according to the description Limak got.

Output
Print “YES” if the description satisfies the three conditions, otherwise print “NO”, both without the quotes.

Examples
input
5
7500 South
10000 East
3500 North
4444 West
4000 North
output
YES
input
2
15000 South
4000 East
output
NO
input
5
20000 South
1000 North
1000000 West
9000 North
10000 North
output
YES
input
3
20000 South
10 East
20000 North
output
NO
input
2
1000 North
1000 South
output
NO
input
4
50 South
50 North
15000 South
15000 North
output
YES
Note
Drawings below show how Limak’s journey would look like in first two samples. In the second sample the answer is “NO” because he doesn’t end on the North Pole.

【题目链接】:http://codeforces.com/contest/750/problem/B

【题解】

细节题
当前的位置只要记录横纵坐标就可以了;
一开始纵坐标位置为20000
对于左右的处理
其他情况下都不用管
如果有一个向上或向下
但是它的值大于20000,则也直接输出no
如果位置x+t>20000或x-t<0也直接输出no
如果当前位置是20000,则如果操作不是往下也直接输出NO
如果当前位置是0,如果操作不是往上则也直接输出NO
最后判断当前的位置是不是20000

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
LL x;

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    x = 20000;
    rei(n);
    rep1(i,1,n)
    {
        LL ti;
        string s;
        cin >> ti >> s;
        if (x==20000)
        {
            if (s[0]!='S')
            {
                puts("NO");
                return 0;
            }
        }
        if (x==0)
        {
            if (s[0]!='N')
            {
                puts("NO");
                return 0;
            }
        }
        if (s[0]=='S'||s[0]=='N')
        {
            if (ti>20000)
            {
                puts("NO");
                return 0;
            }
        }
        if (s[0]=='S')
        {
            if (x-ti<0)
            {
                puts("NO");
                return 0;
            }
            else
                x-=ti;
        }
        if (s[0]=='N')
        {
            if (x+ti>20000)
            {
                puts("NO");
                return 0;
            }
            else
                x+=ti;
        }
    }
    if (x==20000)
        puts("YES");
    else
        puts("NO");
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626744.html