B. New Year and North Pole
2017-01-05 21:59
555 查看
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,
![](http://codeforces.com/predownloaded/2f/c7/2fc7a5846c27efe154438e382ed07e7f7e71321e.png)
) —
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
output
input
output
input
output
input
output
input
output
input
output
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/predownloaded/b2/24/b224619452c99ddf47f15b867801f4191978aa1c.png)
解题说明:此题是一道模拟题,按照题目要求,保证位于南北极点时只能向相反方向移动,同时最后必须回到北极点。针对输入进行判断,并依据往南或往北移动的距离进行加减计算,最后确保距离为0即可。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;
int main()
{
int n,i,flag=0;
long sum=0,t;
char d[6];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%ld%s",&t,d);
if(sum==0&&strcmp(d,"South")!=0)
{
flag=1;
}
if(sum==20000&&strcmp(d,"North")!=0)
{
flag=1;
}
if(strcmp(d,"South")==0)
{
sum+=t;
}
else if(strcmp(d,"North")==0)
{
sum-=t;
}
if(sum>20000||sum<0)
{
flag=1;
}
}
if(sum==0&&flag==0)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
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,
![](http://codeforces.com/predownloaded/2f/c7/2fc7a5846c27efe154438e382ed07e7f7e71321e.png)
) —
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/predownloaded/b2/24/b224619452c99ddf47f15b867801f4191978aa1c.png)
解题说明:此题是一道模拟题,按照题目要求,保证位于南北极点时只能向相反方向移动,同时最后必须回到北极点。针对输入进行判断,并依据往南或往北移动的距离进行加减计算,最后确保距离为0即可。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;
int main()
{
int n,i,flag=0;
long sum=0,t;
char d[6];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%ld%s",&t,d);
if(sum==0&&strcmp(d,"South")!=0)
{
flag=1;
}
if(sum==20000&&strcmp(d,"North")!=0)
{
flag=1;
}
if(strcmp(d,"South")==0)
{
sum+=t;
}
else if(strcmp(d,"North")==0)
{
sum-=t;
}
if(sum>20000||sum<0)
{
flag=1;
}
}
if(sum==0&&flag==0)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
相关文章推荐
- E - New Year and North Pole CodeForces - 750B(状态处理下就好了)
- goodbye 2016 B New Year and North Pole
- New Year and North Pole CodeForces - 750B
- 【codeforces 750B】New Year and North Pole
- New Year and North Pole
- CodeFroces--Good Bye 2016-B--New Year and North Pole(水题-模拟)
- 【Codeforces 750 B New Year and North Pole 】+ 手速
- codeforces Good Bye 2016-B New Year and North Pole
- New Year and North Pole
- codeforces 908 C. New Year and Curling【暴力枚举+图解】
- 908A - New Year and Counting Cards
- Good Bye 2017 E. New Year and Entity Enumeration
- [CF750G]New Year and Binary Tree Paths
- 【CF908E】New Year and Entity Enumeration 位运算+DP
- E - New Year and Old Property
- [CF611B]New Year and Old Property(dfs)
- Good Bye 2017 F-New Year and Rainbow Roads
- New Year and Buggy Bot
- Good Bye 2016 C. New Year and Rating(二分)
- 【CodeForces】611A - New Year and Days(水)