B. New Year and North Pole
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
题目大意:
一头熊从南极出发,每一个step都向一个方向走x米(地球直径为20000米),到达南极时只能往北走,到达北极时只能往南走,最后的终点需要为南极,问题为路径是否合法
其实只需要考虑纵坐标(南北方向)即可
这道题呗hack了一次。。。汗。。。原因是未考虑到y<0不合法的情况
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=60;
struct step
{
int x;
int d;
}a[MAXN];
int x,y;
// bool OK()
// {
// if(y>20000) return 0;
// return 1;
// }
int main()
{
//freopen("data.in","r",stdin);
int n;
while(cin>>n){
y=0;
string str;
for(int i=0;i<n;i++){
cin>>a[i].x;
cin>>str;
if(str=="North")a[i].d=1;
else if(str=="South")a[i].d=2;
else if(str=="West")a[i].d=3;
else if(str=="East")a[i].d=4;
}
bool flag=1;
for(int i=0;i<n;i++){
if(y>20000||y<0){
flag=0;
break;
}
if(y==0){
if(a[i].d!=2) {flag=0;break;}
}
else if(y==20000){
if(a[i].d!=1) {flag=0;break;}
}
switch(a[i].d){
case 1:y-=a[i].x;break;
case 2:y+=a[i].x;break;
case 3:/*x-=a[i].x;if(x>=40000) x%=40000;else if(x<0) x=40000+x;*/break;
case 4:/*x+=a[i].x;if(x>=40000) x%=40000;else if(x<0) x=40000+x;*/break;
}
}
if(y!=0)flag=0;
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}