#include <iostream>
#include <bitset>
using namespace std;
long long int n, m, ile, doW, odW;
long long int t[500001];
bitset<500001> B;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> t[i];
for (int i = 0; i < m; i++) {
if (!B[t[i]])
ile++;
else
doW += (i+1);
B[t[i]] = true;
}
ile = m - ile;
for (int i = m; ile > 0 && i != n; i++) {
if (!B[t[i]]) {
odW += (i + 1);
ile--;
B[t[i]] = true;
}
}
if (!ile)
cout << odW - doW;
else
cout << -1;
return 0;
}
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 | #include <iostream> #include <bitset> using namespace std; long long int n, m, ile, doW, odW; long long int t[500001]; bitset<500001> B; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> t[i]; for (int i = 0; i < m; i++) { if (!B[t[i]]) ile++; else doW += (i+1); B[t[i]] = true; } ile = m - ile; for (int i = m; ile > 0 && i != n; i++) { if (!B[t[i]]) { odW += (i + 1); ile--; B[t[i]] = true; } } if (!ile) cout << odW - doW; else cout << -1; return 0; } |
English