CodeForces - 750B New Year and North Pole (模拟)
2018-01-07 20:18
471 查看
题目链接:http://codeforces.com/problemset/problem/750/B点击打开链接
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 000kilometers.
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 dirithat
is one of: "North", "South
e90c
", "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
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.
简单的走地球模拟
设立一个flag 1表示从北往南 -1 表示从南往北
然后根据每次操作进行模拟看是否符合题意即可
当处在非南北极点时 向东向西走多少无所谓
#include <bits/stdc++.h>
using namespace std;
int flag=1;
int num;
int main()
{
int ans=1;
int n;
cin >> n;
int t;
string s;
while(n--)
{
cin >> t ;
cin >> s;
if(s=="South")
{
if(flag==1)
{
num+=t;
}
else if(flag==-1)
{
num-=t;
}
}
else if(s=="North")
{
if(flag==-1)
{
num+=t;
}
else if(flag==1)
{
num-=t;
}
}
else
{
if(num==0)
{
ans=0;
}
}
if(num==20000)
{
flag=-flag;
num=0;
}
else if(num>20000||num<0)
{
ans=0;
}
}
if(ans&&flag==1&&num==0)
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
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 000kilometers.
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 dirithat
is one of: "North", "South
e90c
", "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.
简单的走地球模拟
设立一个flag 1表示从北往南 -1 表示从南往北
然后根据每次操作进行模拟看是否符合题意即可
当处在非南北极点时 向东向西走多少无所谓
#include <bits/stdc++.h>
using namespace std;
int flag=1;
int num;
int main()
{
int ans=1;
int n;
cin >> n;
int t;
string s;
while(n--)
{
cin >> t ;
cin >> s;
if(s=="South")
{
if(flag==1)
{
num+=t;
}
else if(flag==-1)
{
num-=t;
}
}
else if(s=="North")
{
if(flag==-1)
{
num+=t;
}
else if(flag==1)
{
num-=t;
}
}
else
{
if(num==0)
{
ans=0;
}
}
if(num==20000)
{
flag=-flag;
num=0;
}
else if(num>20000||num<0)
{
ans=0;
}
}
if(ans&&flag==1&&num==0)
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
相关文章推荐
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- CodeForces 1B. Spreadsheets(模拟)
- Codeforces--616A--Comparing Two Long Integers(模拟水题)
- CodeForces 550A Two Substrings(模拟)
- CodeForces 691C Exponential notation(模拟)
- CodeForces - 584A Olesya and Rodion (模拟)水
- codeforces 839B(模拟)
- CodeForces - 839A Arya and Bran(简单模拟)
- codeforces 719C (复杂模拟-四舍五入-贪心)
- CodeForces 689A Mike and Cellphone (模拟+水题)
- codeforces-626-B. Cards(简单模拟)
- CodeForces - 557A Ilya and Diplomas (模拟)水
- codeforces 614B(div.2) 模拟
- Codeforces 472B - Design Tutorial: Learn from Life(模拟)
- Codeforces 381A Sereja and Dima(模拟)
- Codeforces 821C Okabe and Boxes 思维模拟
- Codeforces 202B Brand New Easy Problem(模拟)
- 【CodeForces 779A】Pupils Redistribution(模拟)
- CodeForces 811B——Vladik and Complicated Book ——思维,模拟
- Codeforces 812A Sagheer and Crossroads[模拟]