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+;
int vis[maxn];
int N,K,ans;
vector<int> v[maxn*];
int main(){
//FRE();
while(scanf("%d%d",&N,&K)!=EOF){
for(int i = ; i<maxn*; i++){
v[i].clear();
}
memset(vis,,sizeof(vis));
for(int i = ; i<N; i++){
int t,a;
scanf("%d",&t);
for(int j = ; j<t; j++){
scanf("%d",&a);
v[i].push_back(a);
v[i+N].push_back(a);//数组加倍
}
}
ans = 1e8;
int st = ,en = ;
int k = ,res = ;
while(en <= *N){
res += v[en].size();
for(int i = ; i<v[en].size(); i++){
int index = v[en][i];
if(vis[index] == ){
k++;
}
vis[index]++;
}
while(k==K && st<=en){//起点加一,直到不符合条件的情况出现
ans = min(ans, res);
res -= v[st].size();
for(int i = ; i<v[st].size(); i++){
int index = v[st][i];
vis[index]--;
if(vis[index] == ){
k--;
}
}
st++;
}
en++;
}
if(ans==1e8){
printf("-1\n");
}
else
printf("%d\n",ans);
}
return ;
}
/*
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
*/
Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)的更多相关文章
- Gym - 101670A Amusement Anticipation(CTU Open Contest 2017 签到题)
题目&题意: 倒着找处于最后位置的等差数列的开头的位置. 例: 1 5 3 4 5 6 3 4 5 6是等差数列,它的开头的位置是3 PS: 读题真的很重要!!!!多组输入,上来就读错了!! ...
- Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)
题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: ...
- Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)
题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...
- Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)
题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下) Bishop(斜 ...
- Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)
题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...
- Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)
题目: The park management finally decided to install some popular boxing machines at various strategic ...
- Gym 101194D Ice Cream Tower
被一道数位DP折磨得欲仙欲死之后,再做这道题真是如同吃了ice cream一样舒畅啊 #include<bits/stdc++.h> using namespace std; #defin ...
- Ice cream samples Gym - 101670G 滑动扫描
题目:题目链接 思路:比赛中读错了题,题目要求选一个连续区间,却读成了随便选取几个柜台,英语要好好学啊,读懂题就很简单了,扫一遍就出结果了 AC代码: #include <iostream> ...
- CTU OPEN 2017 Ice cream samples /// 尺取法
题目大意: 给定n k 接下来n行 给定n个摊位的冰淇淋信息 首先给一个t 表示这个摊位有t个冰淇淋 接下来t个数表示对应冰淇淋的品种 走到连续的几个摊位 会买下走过的摊位的所有的冰淇淋 求 要买下所 ...
随机推荐
- Bootstrap 模态窗口源码分析
前言: bootstrap的 js插件的源码写的非常好,也算是编写jquery插件的模范写法,本来还想大篇详细的分析一下呢,唉,没时间啊,很早之前看过的源码了,现在贴在了博客上, 300来行的代码,其 ...
- Called attach on a child which is not detached
问题:Called attach on a child which is not detached: ViewHolder#出现问题的原因 经过google后发现,出现该问题的原因是由于recycle ...
- 并不对劲的spoj1812
题意是求多个串的lcs. 这也是道后缀自动机的模板题.对于任意一个字符串建后缀自动机,用其他串查询就行.对于后缀自动机的每个状态要额外记匹配到当前状态的最大长度. 和spoj1811的区别在于这道题不 ...
- FreeMarker:目录
ylbtech-FreeMarker:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://yl ...
- mac apache虚拟主机配置
<VirtualHost *:80> ServerAdmin slin DocumentRoot "/Users/slin/work/phpStudy/myPh ...
- Linux C编程之二:Linux基础
1.Linux的特点 (1)Linux就是一个操作系统(作为用户和计算机之间接口的软件程序) 注:操作系统的功能:命令解释,进程管理,内存管理,输入输出(I/O)操作和外围设备管理,文件管理 (2)特 ...
- redis+mysql读写方案
前言:在web服务端开发的过程中,redis+mysql是最常用的存储解决方案,mysql存储着所有的业务数据,根据业务规模会采用相应的分库分表.读写分离.主备容灾.数据库集群等手段.但是由于mysq ...
- scala-基础-映射(1)
//映射(1)-构建,获取,更新,迭代,反转,映射(可变与不可变 互换) class Demo1 extends TestCase { //构建与获取 def test_create_^^(){ // ...
- 通过机智云APP来学习安卓
效果非常之好,安卓6.0之后就进行了动态授权.按照网上的视频一步一步调试的非常成功,非常舒服.
- 个人觉得比较好用的chrome插件
印象笔记·悦读 "悦读"可使博文.文章和网页变得简明而又易于阅读.将其保存至印象笔记以便随时随地阅读. Anything to QRcode 通过右键菜单或地址栏按钮将当前页面地址 ...