i want u to act as the most skilled competetive programmer, u take time to think and…
- User
i want u to act as the most skilled competetive programmer, u take time to think and check if ur solution works and match up with the example tescase, u need to think thoroughly since u r in a contest. use need to use this template, make sure u have lowercase rlly short variable names and NO COMMENTS (in the code, at all) and since ur using this template always use int and not long long, PLS REVALUATE UR CODE AND MAKE SURE IT MAKES SURE IT MATCHES EVERY SINGLE SAMPLE INPUT AND OUTPUT GIVEN, THINK MULTIPLE TIMES BEFORE GIVING UR FINAL ANSWER AS THIS IS A CONTEST: #include <bits/stdc++.h> #define int long long using namespace std;
signed main() { cin.tie(0); ios_base::sync_with_stdio(false); }
Score : 200 points
Problem Statement There is a keyboard with 26 keys arranged on a number line.
The arrangement of this keyboard is represented by a string S, which is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ. The key corresponding to the character S x is located at coordinate x (1≤x≤26). Here, S x denotes the x-th character of S.
You will use this keyboard to input ABCDEFGHIJKLMNOPQRSTUVWXYZ in this order, typing each letter exactly once with your right index finger. To input a character, you need to move your finger to the coordinate of the key corresponding to that character and press the key.
Initially, your finger is at the coordinate of the key corresponding to A. Find the minimal possible total traveled distance of your finger from pressing the key for A to pressing the key for Z. Here, pressing a key does not contribute to the distance.
Constraints S is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ. Input The input is given from Standard Input in the following format:
S Output Print the answer.
Sample Input 1 Copy ABCDEFGHIJKLMNOPQRSTUVWXYZ Sample Output 1 Copy 25 From pressing the key for A to pressing the key for Z, you need to move your finger 1 unit at a time in the positive direction, resulting in a total traveled distance of 25. It is impossible to press all keys with a total traveled distance less than 25, so print 25.
Sample Input 2 Copy MGJYIZDKSBHPVENFLQURTCWOAX Sample Output 2 Copy 223
- Assistantcpp
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); string s; cin>>s; int p[26]; for(int i=0;i<26;i++) p[s[i]-'A']=i+1; int t=0, prev=p[0]; for(int i=1;i<26;i++){t+=abs(p[i]-prev); prev=p[i];} cout<<t; }