#include <stdio.h> int TailBrands[500001]; int N, K; int main() { scanf("%d %d", &N, &K); int time = 0; int bottleCount = 0; for(int i=0; i<K; i++) { int brand; scanf("%d", &brand); if(TailBrands[brand] == 1) { time += K - i; bottleCount++; } if(TailBrands[brand] == 0) TailBrands[brand] = 1; } for(int i=K; i<N && bottleCount > 0; i++) { int brand; scanf("%d", &brand); if(TailBrands[brand] == 0) { bottleCount--; time += i - K; TailBrands[brand] = 1; } } if(bottleCount > 0) printf("-1"); else printf("%d", time); 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 | #include <stdio.h> int TailBrands[500001]; int N, K; int main() { scanf("%d %d", &N, &K); int time = 0; int bottleCount = 0; for(int i=0; i<K; i++) { int brand; scanf("%d", &brand); if(TailBrands[brand] == 1) { time += K - i; bottleCount++; } if(TailBrands[brand] == 0) TailBrands[brand] = 1; } for(int i=K; i<N && bottleCount > 0; i++) { int brand; scanf("%d", &brand); if(TailBrands[brand] == 0) { bottleCount--; time += i - K; TailBrands[brand] = 1; } } if(bottleCount > 0) printf("-1"); else printf("%d", time); return 0; } |