-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
210 lines (191 loc) · 4.22 KB
/
Solution.cs
File metadata and controls
210 lines (191 loc) · 4.22 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
public class Solution
{
public long MinimumCost(int[] nums, int k, int dist)
{
long ans = long.MaxValue;
int n = nums.Length;
Container ct = new(k - 1);
for (int i = 0; i <= dist; i++)
{
ct.Add(nums[i + 1]);
}
ans = Math.Min(ans, nums[0] + ct.Sum());
for (int i = 2; i < n - k + 2; i++)
{
ct.Erase(nums[i - 1]);
if (i + dist < n)
{
ct.Add(nums[i + dist]);
}
ans = Math.Min(ans, nums[0] + ct.Sum());
}
return ans;
}
}
public class Container
{
private int k;
private PriorityQueue<int, int> st1;
private PriorityQueue<int, int> st2;
private Dictionary<int, int> cnt1;
private Dictionary<int, int> cnt2;
private Dictionary<int, int> del1;
private Dictionary<int, int> del2;
private int st1Size;
private int st2Size;
private long sm;
public Container(int k)
{
this.k = k;
this.st1 = new PriorityQueue<int, int>();
this.st2 = new PriorityQueue<int, int>();
this.cnt1 = new Dictionary<int, int>();
this.cnt2 = new Dictionary<int, int>();
this.del1 = new Dictionary<int, int>();
this.del2 = new Dictionary<int, int>();
this.st1Size = 0;
this.st2Size = 0;
this.sm = 0;
}
private static void Inc(Dictionary<int, int> dict, int key)
{
if (dict.TryGetValue(key, out int v))
dict[key] = v + 1;
else
dict[key] = 1;
}
private static void Dec(Dictionary<int, int> dict, int key)
{
int v = dict[key] - 1;
if (v == 0)
dict.Remove(key);
else
dict[key] = v;
}
private void Prune1()
{
while (st1.Count > 0)
{
int x = st1.Peek();
if (del1.TryGetValue(x, out int d) && d > 0)
{
st1.Dequeue();
if (d == 1)
del1.Remove(x);
else
del1[x] = d - 1;
}
else
{
break;
}
}
}
private void Prune2()
{
while (st2.Count > 0)
{
int x = st2.Peek();
if (del2.TryGetValue(x, out int d) && d > 0)
{
st2.Dequeue();
if (d == 1)
del2.Remove(x);
else
del2[x] = d - 1;
}
else
{
break;
}
}
}
private int ExtractMax1()
{
Prune1();
int x = st1.Dequeue();
Dec(cnt1, x);
st1Size--;
sm -= x;
return x;
}
private int ExtractMin2()
{
Prune2();
int x = st2.Dequeue();
Dec(cnt2, x);
st2Size--;
return x;
}
private int Min2()
{
Prune2();
return st2.Peek();
}
private void Insert1(int x)
{
st1.Enqueue(x, -x);
Inc(cnt1, x);
st1Size++;
sm += x;
}
private void Insert2(int x)
{
st2.Enqueue(x, x);
Inc(cnt2, x);
st2Size++;
}
private void Adjust()
{
while (st1Size < k && st2Size > 0)
{
int x = ExtractMin2();
Insert1(x);
}
while (st1Size > k)
{
int x = ExtractMax1();
Insert2(x);
}
}
// insert element x
public void Add(int x)
{
if (st2Size > 0)
{
int mn = Min2();
if (x >= mn)
Insert2(x);
else
Insert1(x);
}
else
{
Insert1(x);
}
Adjust();
}
// delete element x
public void Erase(int x)
{
if (cnt1.TryGetValue(x, out int c1) && c1 > 0)
{
Dec(cnt1, x);
st1Size--;
sm -= x;
Inc(del1, x);
}
else
{
Dec(cnt2, x);
st2Size--;
Inc(del2, x);
}
Adjust();
}
// sum of the first k smallest elements
public long Sum()
{
return sm;
}
}