2023 Donald is a postman
Time limit:1.0 Second
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 is cartoon characters. The Studios have three cases for the letters, with nine sections in each case. Every section had the name of the receiver on it. All cases stand in a row as it's shown at the picture below. Donald Duck has brought
NLetters 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 would he make until he puts all the letters into the respective sections, if he does this in the order they is in his bag? Inputthe first line contains an integer
NThat's the amount of letters in Donald ' s bag (1≤
N≤1 000). The following
NLines contain receivers of the letters in the order they is in the bag. Outputoutput the number of steps Donald should make to put all the letters into the cases. Sample
| input |
Output |
4AuroraTianaArielMulan |
5
|
Parsing: Pre-processing each name in which case, and then in the order of entry to move.
AC Code:
#include <cstdio> #include <string> #include <iostream>using namespace Std;int a[30];int main () {#ifde F sxk freopen ("In.txt", "R", stdin); #endif//sxk for (int i=0; i<26; i++) {if (i = = ' A '-' a ' | | i = = ' P '-' a ' | | i = = ' O '-' a ' | | i = = ' R '-' a ' ) A[i] = 1; else if (i = = ' B '-' a ' | | i = = ' M '-' a ' | | i = = ' S '-' a ') a[i] = 2; else if (i = = ' D '-' a ' | | i = = ' G '-' a ' | | i = = ' J '-' a ' | | i = = ' K '-' a ' | | i = = ' T '--' a ' | | i = = ' W '-' a ') A[i] = 3; else a[i] = 0; } int n; string S; while (scanf ("%d", &n) ==1) {int-ans = 0, now = 1; for (int i=0; i<n; i++) {cin>>s; Ans + = a[s[0]-' a '] > now? a[S[0]-' a ']-now:now-a[s[0]-' a ']; now = a[S[0]-' a ']; } printf ("%d\n", ans); } return 0;}
URAL 2023. Donald is a postman (preprocessing)