-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipopstack.cpp
89 lines (78 loc) · 2.29 KB
/
multipopstack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in stack: ";
cin >> n;
int arr[n];
srand(time(0));
cout << "Enter the elements of the stack:\n";
for (int i = 0; i < n; i++) {
cout << "Element " << i + 1 << ": ";
cin >> arr[i];
}
cout << "--------------------\n";
int cost = 0;
int accountingcost = 0;
int potentialcost = 0;
stack<int> stk;
for (int i = 0; i < n; i++) {
int k; //= rand() % 3;
cout << "Enter 0.push 1.pop 2.multipop" << " ";
cin >> k;
if (k == 0) {
stk.push(arr[i]);
cost += 1;
accountingcost += 1;
cout << stk.top() << " push accounting cost: " << "1" << endl;
potentialcost += 1;
}
else if (k == 1) {
if (!stk.empty()) {
cout << stk.top() << " pop accounting cost: " << "-1" << endl;
stk.pop();
cost += 1;
accountingcost -= 1;
potentialcost -= 1;
}
}
else {
int k = rand() % 5;
int s = stk.size();
int minimum = min(k, s);
for (int i = 0; i < minimum; i++) {
cout << stk.top() << " multipop accounting cost: " << "-1" << endl;
stk.pop();
accountingcost -= 1;
cost += 1;
potentialcost -= 1;
}
}
}
cout << "\nCost: " << cost << endl;
cout << "Aggregate cost: " << (float)cost / n << endl;
cout << "Accounting: " << accountingcost << endl;
cout << "Potential or Excess: " << potentialcost << endl;
cout << "Final stack size: " << stk.size() << endl;
return 0;
}
// Enter the number of elements in stack: 5
// Enter the elements of the stack:
// Element 1: 1
// Element 2: 2
// Element 3: 3
// Element 4: 4
// Element 5: 5
// --------------------
// Enter 0.push 1.pop 2.multipop 0
// 1 push accounting cost: 1
// Enter 0.push 1.pop 2.multipop 0
// 2 push accounting cost: 1
// Enter 0.push 1.pop 2.multipop 1
// 2 pop accounting cost: -1
// Enter 0.push 1.pop 2.multipop 0
// 4 push accounting cost: 1
// Enter 0.push 1.pop 2.multipop 2