Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the problem statement and solution approach. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions codeforces/540a_combination_locks.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Problem Statement: McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?

aman97kalra marked this conversation as resolved.
Show resolved Hide resolved
Solution:The answer is the sum of all the changes needed over the number if disks i.e. (n).Also for each disk we can go either forward or backward, so in order to minimise the number of moves we will select that combination which will give less number of moves.
For Example: Let's say we have to change form 1->7. So the first possibility is moving forward form 1->2->3->4->5->6->7 which gives the number of moves as 6.But if we move backward form 1->0->9->8->7 then no. of moves=4. Thus we will move backward and add 4 to answer.This can be easily visualised if you have visualised the lock system being talked about in the question.

#include "bits/stdc++.h"
#define MODULO 1000000007
#define endl '\n'
#define m(a) ((a)%MODULO)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long long int ll;


int main(){
Expand All @@ -14,11 +16,10 @@ int main(){
cin >> t;
string s1, s2;
int ans=0, z;

cin >> s1 >> s2;
for (int i=0; i<t; i++)
ans += min( abs(s1[i]-s2[i]) , 10-abs(s1[i]-s2[i]) );
ans += min( abs(s1[i]-s2[i]) , 10-abs(s1[i]-s2[i]) ); // ans is minimum of forward & backward moves where 10-abs(s1[i]-s2[i]) gives the no. of backward moves.
cout << ans << endl;

return 0;
}
}