I solved the problem with three pointers. But when I sort the vectors first and the resize, I get an AC. If I sort them after making them unique, I get a WA.
AC Code
WA Code
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I solved the problem with three pointers. But when I sort the vectors first and the resize, I get an AC. If I sort them after making them unique, I get a WA.
long triplets(vector<int> a, vector<int> b, vector<int> c) {
sort(all(a));
sort(all(b));
sort(all(c));
a.erase(unique(all(a)), a.end());
b.erase(unique(all(b)), b.end());
c.erase(unique(all(c)), c.end());
int n = a.size(), m = b.size(), l = c.size();
int i = 0, j = 0, k = 0;
long ans = 0;
for(int j = 0; j < m; j++) {
while(i < n and a[i] <= b[j]) i++;
while(k < l and c[k] <= b[j]) k++;
ans += 1LL*i*k;
}
return ans;
}
long triplets(vector<int> a, vector<int> b, vector<int> c) {
a.erase(unique(all(a)), a.end());
b.erase(unique(all(b)), b.end());
c.erase(unique(all(c)), c.end());
sort(all(a));
sort(all(b));
sort(all(c));
int n = a.size(), m = b.size(), l = c.size();
int i = 0, j = 0, k = 0;
long ans = 0;
for(int j = 0; j < m; j++) {
while(i < n and a[i] <= b[j]) i++;
while(k < l and c[k] <= b[j]) k++;
ans += 1LL*i*k;
}
return ans;
}
Name |
---|
Unique requires the vector to be sorted. All it does is check if adjacent elements are equal.
Thanks! Didn’t know that.
unique
only causes consecutive groups of identical elements to be condensed. So you need to sort first.Thank you! So I guess there is no way to extract unique elements, maintaining the given order?
^ using an std algorithm I mean.
In a way, you can. For instance if you do
unique
on the arrayyou would get