#include <iostream> #ifdef TEST_MODE #include <fstream> #endif using namespace std; const int kMaxOranges = 500000; typedef long long ll; int main() { #ifdef TEST_MODE std::ifstream in("1.in"); std::cin.rdbuf(in.rdbuf()); #endif std::ios_base::sync_with_stdio(false); int bottels[kMaxOranges]; int numberOfTypes[kMaxOranges+1]; int n, k; cin >> n >> k; for ( int i = 0; i < n; ++i ) { numberOfTypes[i] = 0; } int boundIdx = k - 1; for ( int i = 0; i < n; ++i ) { cin >> bottels[i]; numberOfTypes[bottels[i]]++; if ( numberOfTypes[bottels[i]] > 1 ) { if ( i <= boundIdx ) { boundIdx++; } } } if ( boundIdx >= n ) { cout << -1 << endl; } else { for ( int i = boundIdx + 1; i < n; ++i ) { numberOfTypes[bottels[i]]--; } ll result = 0; for ( int i = boundIdx - 1; i >= 0; --i ) { if ( numberOfTypes[bottels[i]] > 1 ) { numberOfTypes[bottels[i]]--; result += boundIdx - i; boundIdx--; } } cout << result << endl; } 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <iostream> #ifdef TEST_MODE #include <fstream> #endif using namespace std; const int kMaxOranges = 500000; typedef long long ll; int main() { #ifdef TEST_MODE std::ifstream in("1.in"); std::cin.rdbuf(in.rdbuf()); #endif std::ios_base::sync_with_stdio(false); int bottels[kMaxOranges]; int numberOfTypes[kMaxOranges+1]; int n, k; cin >> n >> k; for ( int i = 0; i < n; ++i ) { numberOfTypes[i] = 0; } int boundIdx = k - 1; for ( int i = 0; i < n; ++i ) { cin >> bottels[i]; numberOfTypes[bottels[i]]++; if ( numberOfTypes[bottels[i]] > 1 ) { if ( i <= boundIdx ) { boundIdx++; } } } if ( boundIdx >= n ) { cout << -1 << endl; } else { for ( int i = boundIdx + 1; i < n; ++i ) { numberOfTypes[bottels[i]]--; } ll result = 0; for ( int i = boundIdx - 1; i >= 0; --i ) { if ( numberOfTypes[bottels[i]] > 1 ) { numberOfTypes[bottels[i]]--; result += boundIdx - i; boundIdx--; } } cout << result << endl; } return 0; } |