E - New Year and North Pole CodeForces - 750B(状态处理下就好了)
2017-09-08 16:13
459 查看
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,
![](https://odzkskevi.qnssl.com/270251819dfa275bb83412acf24c054d?v=1504788702)
) — 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.
Example
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.
![](https://odzkskevi.qnssl.com/8484bd9b10440abaf8ff8c45cce3052c?v=1504788702)
状态处理下就ok
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<map>
using namespace std;
int main()
{
int n;
int level[100];
char dir[100][50];
int kk= 20000;//表示此刻在北极
scanf("%d",&n);
int tai=1;//表示此刻的状态在北极
// 1表示北 2表示南 3表示在中间
for(int i=1;i<=n;i++)
scanf("%d %s",&level[i],dir[i]);
for(int i=1;i<=n;i++)
{
if(dir[i][0]=='N')//1
{
if(tai==1)
{
printf("NO\n");
return 0;
}
else
{
kk=kk+level[i];
if(kk>20000)
{
printf("NO\n");
return 0;
}
}
if(kk==20000) tai=1;
else if(kk==0) tai=2;
else if(kk>0&&kk<20000) tai=3;
}
else if(dir[i][0]=='S')//2
{
if(tai==2)
{
printf("NO\n");
return 0;
}
else
{
kk=kk-level[i];
if(kk<0)
{
printf("NO\n");
return 0;
}
}
if(kk==20000) tai=1;
else if(kk==0) tai=2;
else if(kk>0&&kk<20000) tai=3;
}
else if(dir[i][0]=='E'||dir[i][0]=='W')
{
if(tai==1||tai==2)
{
printf("NO\n");
return 0;
}
}
}
if(tai==1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
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.
Example
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.
状态处理下就ok
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<map>
using namespace std;
int main()
{
int n;
int level[100];
char dir[100][50];
int kk= 20000;//表示此刻在北极
scanf("%d",&n);
int tai=1;//表示此刻的状态在北极
// 1表示北 2表示南 3表示在中间
for(int i=1;i<=n;i++)
scanf("%d %s",&level[i],dir[i]);
for(int i=1;i<=n;i++)
{
if(dir[i][0]=='N')//1
{
if(tai==1)
{
printf("NO\n");
return 0;
}
else
{
kk=kk+level[i];
if(kk>20000)
{
printf("NO\n");
return 0;
}
}
if(kk==20000) tai=1;
else if(kk==0) tai=2;
else if(kk>0&&kk<20000) tai=3;
}
else if(dir[i][0]=='S')//2
{
if(tai==2)
{
printf("NO\n");
return 0;
}
else
{
kk=kk-level[i];
if(kk<0)
{
printf("NO\n");
return 0;
}
}
if(kk==20000) tai=1;
else if(kk==0) tai=2;
else if(kk>0&&kk<20000) tai=3;
}
else if(dir[i][0]=='E'||dir[i][0]=='W')
{
if(tai==1||tai==2)
{
printf("NO\n");
return 0;
}
}
}
if(tai==1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
相关文章推荐
- New Year and North Pole CodeForces - 750B
- 【Codeforces 750 B New Year and North Pole 】+ 手速
- 【codeforces 750B】New Year and North Pole
- New Year and North Pole
- New Year and Curling CodeForces - 908C
- C - New Year and Hurry CodeForces - 750A(动态规划)
- New Year and Rating CodeForces - 750C
- Codeforces GoodBye2015 New Year and Three Musketeers Codeforces 611E(贪心)
- codeforces GoodBye2015 D.New Year and Ancient Prophecy (dp+lcp+树状数组)
- Codeforces GoodBye2015 New Year and Three Musketeers Codeforces 611E(贪心)
- I - New Year and Rating CodeForces - 750C 模拟
- New Year and Old Property CodeForces - 611B
- 【转】Codeforces GoodBye2015 New Year and Three Musketeers Codeforces 611E(贪心)
- B. New Year and North Pole
- goodbye 2016 B New Year and North Pole
- codeforces goodbye 2016 C. New Year and Rating
- New Year and North Pole
- CodeFroces--Good Bye 2016-B--New Year and North Pole(水题-模拟)
- New Year and Rating CodeForces - 750C
- New Year and Old Property CodeForces - 611B