You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/3_Silver/Intro_Ordered.mdx
+164-7
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
id: intro-ordered
3
3
title: 'More Operations on Ordered Sets'
4
-
author: Darren Yao, Benjamin Qi
4
+
author: Darren Yao, Benjamin Qi, Andrew Wang
5
5
prerequisites:
6
6
- intro-sets
7
7
- binary-search-sorted
@@ -146,7 +146,14 @@ Suppose that we replace `s.upper_bound(7)` with `upper_bound(begin(s),end(s),7)`
146
146
147
147
<JavaSection>
148
148
149
-
The ordered set also allows four additional operations: `first`, which returns the lowest element in the set, `last`, which returns the highest element in the set, `lower`, which returns the greatest element strictly less than some element, and `higher`, which returns the least element strictly greater than it.
149
+
The ordered set (TreeSet) allows for a multitude of additional operations:
150
+
151
+
-`first()`: returns the lowest element in the set
152
+
-`last()`: returns the greatest element in the set
153
+
-`lower(E v)`: returns the greatest element strictly less than `v`
154
+
-`floor(E v)`: returns the greatest element less than or equal to `v`
155
+
-`higher(E v)`: returns the least element strictly greater than `v`
156
+
-`ceiling(E v)`: returns the least element greater than or equal to `v`
150
157
151
158
```java
152
159
TreeSet<Integer> set =newTreeSet<Integer>();
@@ -351,10 +358,6 @@ We'll use iterators extensively.
351
358
352
359
<Spoiler title="Solution">
353
360
354
-
<LanguageSection>
355
-
356
-
<CPPSection>
357
-
358
361
Let the bit string be $s=s_0s_1s_2\ldots,s_{n-1}$. In the set `dif`, we store all indices $i$ such that $s_i\neq s_{i-1}$ (including $i=0$ and $i=n$). If the elements of `dif` are $0=dif_1<dif_2<\cdots<dif_k=n$, then the longest length is equal to
359
362
360
363
$$
@@ -365,6 +368,11 @@ We can store each of these differences in a multiset `ret`; after each inversion
365
368
366
369
Inverting a bit at a 0-indexed position `x` corresponds to inserting `x` into `dif` if it not currently present or removing `x` if it is, and then doing the same with `x+1`.Whenever we insert or remove an element of `dif`, we should update `ret` accordingly.
367
370
371
+
372
+
<LanguageSection>
373
+
374
+
<CPPSection>
375
+
368
376
```cpp
369
377
#include <bits/stdc++.h>
370
378
using namespace std;
@@ -385,7 +393,7 @@ void modify(int x) {
385
393
ret.insert(b-a);
386
394
dif.erase(it); // remove x from dif
387
395
} else { // x is not currently in dif, insert it
388
-
it = dif.insert(x).first; //inser x into dif
396
+
it = dif.insert(x).first; //insert x into dif
389
397
// it = iterator corresponding to x
390
398
int a =*prev(it), b =*next(it);
391
399
ret.erase(ret.find(b-a)); // update ret
@@ -409,10 +417,94 @@ int main() {
409
417
}
410
418
```
411
419
420
+
</CPPSection>
421
+
422
+
<JavaSection>
423
+
424
+
<Warning>
425
+
426
+
Java solutions are too slow for the CSES. UseC++ instead to get AC.
427
+
428
+
</Warning>
429
+
430
+
```java
431
+
432
+
import java.io.*;
433
+
import java.util.*;
434
+
435
+
classBitInversion{
436
+
publicstaticTreeMap<Integer, Integer> ret =newTreeMap<Integer, Integer>();
if(dif.higher(it) !=null) add(dif.higher(it) - it); // initialize ret
468
+
}
469
+
StringTokenizer st =newStringTokenizer(sc.readLine());
470
+
for (int i =0; i < m; i++){
471
+
int x =Integer.parseInt(st.nextToken()); // 1-indexed position
472
+
modify(x-1); modify(x);
473
+
out.println(ret.lastKey());
474
+
}
475
+
out.close();
476
+
}
477
+
478
+
staticvoidadd(intx){
479
+
if(ret.containsKey(x)){
480
+
ret.put(x, ret.get(x) +1);
481
+
} else {
482
+
ret.put(x, 1);
483
+
}
484
+
}
485
+
486
+
staticvoidremove(intx){
487
+
ret.put(x, ret.get(x) -1);
488
+
if(ret.get(x) ==0){
489
+
ret.remove(x);
490
+
}
491
+
}
492
+
}
493
+
494
+
```
495
+
496
+
</JavaSection>
497
+
498
+
</LanguageSection>
499
+
412
500
<br />
413
501
414
502
Note that multiset has a high constant factor, so replacing `ret` with a priority queue and an array that stores the number of times each integer occurs in the priority queue reduces the runtime by a factor of 2.
0 commit comments