-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum_average_waiting_time.cpp
95 lines (81 loc) · 2.08 KB
/
minimum_average_waiting_time.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
90
91
92
93
94
95
/*
* Problem Link https://www.hackerrank.com/challenges/minimum-average-waiting-time/problem
* Time complexity O(shit)
*/
#include <iostream>
using namespace std;
typedef unsigned long long ull;
typedef struct {
ull arrival;
ull pizza_time;
} customer;
typedef struct node {
customer *data;
struct node *next;
} node;
node *new_node(ull arrival, ull pizza_time) {
customer *person= new customer;
person->arrival = arrival;
person->pizza_time = pizza_time;
node *node_in_list = new node;
node_in_list->data = person;
node_in_list->next = NULL;
return node_in_list;
}
void sortedInsert(struct node** head_ref, struct node* new_node)
{
struct node* current;
if (*head_ref == NULL || (*head_ref)->data->pizza_time >= new_node->data->pizza_time)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
current = *head_ref;
while (current->next!=NULL &&
current->next->data->pizza_time < new_node->data->pizza_time)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
void delete_node(node *ptr, node *prev) {
if (ptr->next != NULL) {
prev->next = ptr->next;
}
else {
prev->next = NULL;
}
free(ptr);
}
int main()
{
int n;
cin >> n;
ull arrival;
ull pizza_time;
cin >> arrival >> pizza_time;
node *linked_list = new_node(arrival, pizza_time);
for (int i = 1; i < n; ++i) {
cin >> arrival >> pizza_time;
sortedInsert(&linked_list, new_node(arrival, pizza_time));
}
//node *it = linked_list->next;
node *prev = linked_list;
ull result = 0;
for (int t = 0; t <= 10e5; ++t) {
node *it = linked_list->next;
while (it != NULL) {
if (it->data->arrival > (ull)t) { //possible
result += it->data->pizza_time;
delete_node(it, prev);
break;
}
prev = it;
it = it->next;
}
}
}