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
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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;
}
Название |
---|
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