1. 程式人生 > >Ice cream samples Gym - 101670G 滑動掃描

Ice cream samples Gym - 101670G 滑動掃描

blank fin ack name inf algo sample %d --

題目:題目鏈接

思路:比賽中讀錯了題,題目要求選一個連續區間,卻讀成了隨便選取幾個櫃臺,英語要好好學啊,讀懂題就很簡單了,掃一遍就出結果了

AC代碼:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <cstring>
 7 #include <vector>
 8 #include <string
> 9 #include <queue> 10 #include <map> 11 #include <set> 12 13 #define FRER() freopen("in.txt", "r", stdin); 14 #define INF 0x3f3f3f3f 15 16 using namespace std; 17 18 const int maxn = 1000000 + 5; 19 20 vector<int> stand[maxn << 1]; 21 22 int brand[maxn], N, K;
23 24 void init() { 25 for(int i = 0; i < (N << 1); ++i) 26 stand[i].clear(); 27 memset(brand, 0, sizeof(brand)); 28 } 29 30 int solve() { 31 int st = 0, ed = 0, ans = INF, k = 0, now = 0; 32 while(ed < (N << 1)) { 33 for(int i = 0; i < stand[ed].size(); ++i) {
34 ++now; 35 if(brand[stand[ed][i]] == 0) 36 ++k; 37 ++brand[stand[ed][i]]; 38 } 39 ++ed; 40 while(k == K && st < ed) { 41 ans = min(ans, now); 42 for(int i = 0; i < stand[st].size(); ++i) { 43 --now; 44 --brand[stand[st][i]]; 45 if(brand[stand[st][i]] == 0) 46 --k; 47 } 48 ++st; 49 } 50 } 51 return ans == INF ? -1 : ans; 52 } 53 54 int main() 55 { 56 //FRER() 57 while(~scanf("%d %d", &N, &K)) { 58 init(); 59 int n, num; 60 for(int i = 0; i < N; ++i) { 61 scanf("%d", &n); 62 for(int j = 0; j < n; ++j) { 63 scanf("%d", &num); 64 stand[i].push_back(num); 65 stand[i + N].push_back(num); 66 } 67 } 68 printf("%d\n", solve()); 69 } 70 return 0; 71 }

Ice cream samples Gym - 101670G 滑動掃描