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

题目:

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 3
4 1 3 1 3
1 2
2 3 3
1 1
5 3
1 2
1 3
2 1 1
2 2 2
1 1
3 2
2 1 1
1 1
3 1 1 1
PutOut:
4
3
-1
*/

原文地址:https://www.cnblogs.com/sykline/p/9878542.html

时间: 2024-10-10 14:11:07

Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)的相关文章

Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)

题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: 所以执行两次删除操作. 思路: 区间dp,关键在于确定大的区间是由哪些小的区间转化来的. 当a[l] == a[r]的时候,dp[l][r] = dp[l+1][r-1]+1(因为要得到最多的删除次数,大的区间的次数在相等的情况下肯定是由内部小的区间加一得来的): 当a[l] != a[r]的时候,dp[l

Ice cream samples Gym - 101670G 滑动扫描

题目:题目链接 思路:比赛中读错了题,题目要求选一个连续区间,却读成了随便选取几个柜台,英语要好好学啊,读懂题就很简单了,扫一遍就出结果了 AC代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cmath> 5 #include <algorithm> 6 #include <cstring> 7 #include <ve

The 2016 ACM-ICPC Asia China-Final Contest Gym - 101194D Ice Cream Tower

题意:熊猫先生想用一些冰淇淋球来做k层的冰淇淋,规定对于一个冰淇淋中的每一个冰淇淋球(最顶层除外),都大于等于上一层的冰淇淋球的两倍大小:现有n个冰淇淋球,问最多能做几个冰淇淋 思路:刚开始想的贪心,最后发现是不行的.比如对于 1 2 3 4 这种情况,1 后面是2还是3就没法用程序来进行抉择了.可以用二分的方法,枚举所有可能的冰淇淋个数x,然后再进行判断当前的冰淇淋球能否做成x个冰淇淋 1 #include <bits/stdc++.h> 2 using namespace std; 3 t

CTU Open Contest 2017 Go Northwest!(思维题+map)

题目: Go Northwest! is a game usually played in the park main hall when occasional rainy weather discourages the visitors from enjoying outdoor attractions. The game is played by a pair of players and it is based entirely on luck, the players can hardl

Ice Cream Tower

2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output file: Standard Ouptut Time limit: 6 seconds Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists of K ice cr

HackerRank Ice Cream Parlor

传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together have M dollars they want to spend on ice cream. The parlor offers N flavors, and they want to choose two flavors so that they end up spending the whol

codeforces 686A A. Free Ice Cream(水题)

题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using

【HackerRank】Ice Cream Parlor

Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N flavors available, they have to choose two distinct flavors whose cost equals M. Given a list of cost of N flavors, output the indices of two items wh

How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich

ShareThis - By Vikas Verma Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio technology, aimed at new, principally low-power and low-latency, applications for wireless devices within a short range. As I discussed in my previous