Skip to content

Commit 814c757

Browse files
authored
Importance Of Time
1 parent 852ab15 commit 814c757

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Hacker Blocks/Importance Of Time.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Hacker Blocks */
2+
/* Title - Importance Of Time */
3+
/* Created By - Akash Modak */
4+
/* Date - 27/08/2020 */
5+
6+
// There are N processes to be completed. All the processes have a unique number assigned to them from 1 to N.
7+
8+
// Now, we are given two things:
9+
10+
// 1)The calling order in which all the processes are called.
11+
// 2)The ideal order in which all the processes should have been executed.
12+
13+
// Executing a process takes 1 unit of time. Changing the position takes 1 unit of time.
14+
15+
// We have to find out the unit of time required to complete all the process such that a process is executed from the ideal order only when it exists at the same index in the calling order. We can push the first term from the calling order to the last thus rotating the order.
16+
17+
// Input Format
18+
// First line contains a single integer N.
19+
// Next line contains N space separated integers denoting the calling order.
20+
// Last line contains N space separated integers denoting the ideal order.
21+
22+
// Constraints
23+
// 1 <= N <= 10^6
24+
25+
// Output Format
26+
// The total time required
27+
28+
// Sample Input
29+
// 5
30+
// 5 4 2 3 1
31+
// 5 2 1 4 3
32+
// Sample Output
33+
// 7
34+
// Explanation
35+
// Iteration #1: Since the ideal order and calling order both has process #5 to be executed first. Process #5 is executed taking 1 unit of time. The new calling order is: 4 - 2 - 3 - 1. Time taken in step #1: 1.
36+
37+
// Iteration #2: Since the ideal order has process #2 to be executed firstly, the calling ordered has to be changed again, i.e., the first element has to be pushed to the last place. The new calling order is: 2 - 3 - 1 - 4 and process #2 is executed. Time taken in step #2: 2.
38+
39+
// Iteration #3: Since the ideal order has process #1 to be executed firstly, the calling ordered has to be changed again, i.e., the first element has to be pushed to the last place. The new calling order is: 1 - 4 - 3 and process #1 is executed. Time taken in step #2: 2.
40+
41+
// Iteration #4: Since the new first element of the calling order is same as the ideal order, that process will be executed. Time taken in step #4: 1.
42+
43+
// Iteration #5: Since the last element of the calling order is same as the ideal order, that process will be executed. Time taken in step #5: 1.
44+
45+
// Total time taken = 7
46+
47+
#include<bits/stdc++.h>
48+
using namespace std;
49+
int main() {
50+
int n;
51+
cin>>n;
52+
queue<int> ideal,call;
53+
for(int i=0;i<n;i++)
54+
{
55+
int temp;
56+
cin>>temp;
57+
call.push(temp);
58+
}
59+
for(int i=0;i<n;i++)
60+
{
61+
int temp;
62+
cin>>temp;
63+
ideal.push(temp);
64+
}
65+
int sum=0;
66+
for(int i=0;i<n;i++){
67+
while(ideal.front()!=call.front()){
68+
call.push(call.front());
69+
call.pop();
70+
sum++;
71+
}
72+
ideal.pop();
73+
call.pop();
74+
sum++;
75+
}
76+
cout<<sum<<endl;
77+
return 0;
78+
}

0 commit comments

Comments
 (0)