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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
4 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
8 | Dominater069 | 154 |
8 | nor | 154 |
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