Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

來源:互聯網
上載者:User

標籤:mil   std   put   rand   例子   aci   mat   direction   set   

題目:

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 and follow systematically the discount directions to the next stands, they eventually traverse the whole circular path and return back to the stand they started at.

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 various 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 particular 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 particular 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.

題意:

這題的題意太難懂了,還是英語水平不夠啊。抽象出來的題意是在給出的範例中找一個連續的區間,使得這個區間中的數包含1,2,3...,k這些數,而且數的個數要求最小。

思路:

是一個環,所以先將序列加倍,然後利用尺取法。

舉個例子:N==3,K==3

1 1

1 2

3 1 2 3

尺取法做的時候第一次從左至右,1,2,3連在一起才是合格,但是單獨一個3也是合格,而且數的個數更小。

所以這裡就要注意:當得到一個合格值的時候,我們讓他的起點加一,直到不合格情況出現,這個時候終點繼續加一往後枚舉。

之前做的尺取法都是遇到合格情況後直接令起點等於終點繼續枚舉,思路還是沒有拓展開。

代碼:

#include <bits/stdc++.h>#define inf 0x3f3f3f3f#define FRE() freopen("in.txt","r",stdin)using namespace std;typedef long 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%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,a;            scanf("%d",&t);            for(int j = 0; j<t; j++){                scanf("%d",&a);                v[i].push_back(a);                v[i+N].push_back(a);//數組加倍            }        }        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){//起點加一,直到不合格情況出現                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 1PutOut:43-1*/
View Code

 

Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.