2023. Donald is a postman
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Donald Duck works as a postman for the Walt Disney Studios. He delivers children’s letters from all over the world to his friends, which are cartoon characters. The Studios has three cases for the letters, with nine sections in each case. Every section has the name of the receiver on it. All cases stand in a row as it is shown at the picture below.
Donald Duck have brought n letters today. Initially, he stands near the leftmost case. He has to make one step to go to the neighboring case or to the previous one. How many steps will he make until he puts all the letters into the respective sections, if he does this in the order they are in his bag?
Input
The first line contains an integer n that is the amount of letters in Donald’s bag (1 ≤ n ≤ 1 000). The following n lines contain receivers of the letters in the order they are in the bag.
Output
Output the number of steps Donald should make to put all the letters into the cases.
Sample
input | output |
---|---|
4 Aurora Tiana Ariel Mulan |
5 |
Problem Author: Alex Samsonov
Problem Source: NEERC 2014, Eastern subregional contest
Problem Source: NEERC 2014, Eastern subregional contest
Tags: problem for beginners
Difficulty: 38 Printable version Submit solution Discussion (20)
All submissions (6914) All accepted submissions (3538) Solutions rating (2890)
All submissions (6914) All accepted submissions (3538) Solutions rating (2890)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 string str[3][9]= {{"Alice","Ariel","Aurora","Phil","Peter","Olaf","Phoebus","Ralph","Robin"}, 5 {"Bambi","Belle","Bolt","Mulan","Mowgli","Mickey","Silver","Simba","Stitch"}, 6 {"Dumbo","Genie","Jiminy","Kuzko","Kida","Kenai","Tarzan","Tiana","Winnie"} 7 }; 8 string str1; 9 int main () 10 { 11 int n; 12 scanf("%d",&n); 13 int last = 0,sum=0; 14 while(n--) 15 { 16 cin>>str1; 17 18 for(int i=0; i<3; i++) 19 { 20 for(int j=0; j<9; j++) 21 if(str1==str[i][j]) 22 sum+=abs(i-last),last=i,j=9,i=3; 23 } 24 } 25 printf("%d ",sum); 26 return 0; 27 }