Question:
To encourage visitors active movement among the attractions, a circular path with ice cream stands was built in the park some time ago. A discount system common for all stands was also introduced. when a customer buys ice cream at some stand, he is automatically granted a discount for one day at the next stand on the path. when visitors start at any stand follow systematically the discount directions ctions to the next stands, they eventually traverse the whole circular path and return back to the stand they Ted.
Ice creams of various brands are sold at the stands. additionally, each stand sells a nice sample box which contains small samples of popular ice cream brands. the number of samples in the box depends on the stand and varous stands may put different brands into their sample boxes. each box contains samples of one or more brands. A brand may be represented by one or more samples in the box, or it may be completely missing. each stand sells only one type of sample box (the brands of the samples in the box are always the same for that particle stand ).
Quido and Hugo are going to exploit the discount system for their own benefit. they decided to start at some stand and then continue in the direction of the discounts buying one ice cream sample box at each stand they visit in a consecutive sequence. their goal is to collect at least one sample of each ice cream brand sold in the park. simultaneously, to respect their stomach capacities, they want to minimize the total number of ice cream samples they buy.
Input specification:
There are more test cases. each case starts with a line containing two integers n, k separated by space (1 ≤ n, k ≤106 ). n is the number of ice cream stands, k is the total number of different ice cream brands sold at all stands. the brands are labeled by numbers 1, 2 ,..., k. next, there are n lines describing stands in their visiting order. each such line contains the list of brands of all ice cream samples sold in the sample box at that particle stand. each list starts with one positive integer L, describing its length, followed by L integers. each list item represents the brand of one ice cream sample in the sample box sold at this stand. you may assume that even if a visitor buys one sample box at each stand, he/she will collect at most 107 ice cream samples.
Output specification:
For each test case, print a single line with one integer denoting the minimum number of ice cream samples quido and Hugo have to buy in order to obtain a sample of each ice cream brand sold in the park. if it is impossible to obtain samples of all brands output? 1.
Question:
The question is too difficult to understand, but the English level is not enough. The abstract question is to find a continuous interval in the given example so that the number in this interval contains 1, 2, 3 ..., k.
Ideas:
Is a ring, so first double the sequence, and then use the ruler acquisition method.
For example, n = 3, K = 3
1 1
1 2
3 1 2 3
The first time a ruler is connected from left to right, 1, 2, and 3, the matching conditions are met, but a single 3 is also qualified, and the number is smaller.
So pay attention to this: when we get a value that meets the condition, we need to add a value to its starting point until it does not meet the condition. At this time, we will continue to add one to the end and then enumerate.
In the previous steps, when conditions are met, the start point equals the end point to continue enumeration.
Code:
# Include <bits/stdc ++. h> # define INF 0x3f3f3f # define fre () freopen ("in.txt", "r", stdin) using namespace STD; typedef long ll; const int maxn = 1e6 + 10; int vis [maxn]; int N, K, ans; vector <int> V [maxn * 2]; int main () {// fre (); while (scanf ("% d", & N, & K )! = EOF) {for (INT I = 0; I <maxn * 2; I ++) {v [I]. clear ();} memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <n; I ++) {int T,; scanf ("% d", & T); For (Int J = 0; j <t; j ++) {scanf ("% d", & ); V [I]. push_back (a); V [I + N]. push_back (a); // array doubling} ans = 1e8; int ST = 0, en = 0; int K = 0, Res = 0; while (EN <= 2 * n) {res + = V [En]. size (); For (INT I = 0; I <V [En]. size (); I ++) {int Index = V [En] [I]; If (vis [Index] = 0) {k ++ ;} vis [Index] ++;} while (k = K & St <= EN) {// you can add one at the starting point until ans = min (ANS, res); res-= V [st]. size (); For (INT I = 0; I <V [st]. size (); I ++) {int Index = V [st] [I]; vis [Index] --; If (vis [Index] = 0) {k -- ;}} st ++;} en ++;} If (ANS = 1e8) {printf ("-1 \ n ");} else printf ("% d \ n", ANS);} return 0;}/* Putin: 4 34 1 3 1 31 22 3 31 15 31 21 32 1 12 2 21 13 22 1 11 13 1 1 1 putout: 43-1 */
View code
Gym-101670g ice cream samples (CTU open contest 2017 square meter acquisition)