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个数表示对应冰淇淋的品种 走到连续的几个摊位 会买下走过的摊位的所有的冰淇淋 求 要买下所 ...
随机推荐
- Robot Framework 初学者上手资料
首先要声明一下这是从http://www.cnblogs.com/yufeihlf/p/5949984.html拷贝的. 在这里只是自己的一个笔记,方便日后添加.修改内容. 总结下Robot Fram ...
- Linux/Android——input系统之 kernel层 与 frameworks层交互 (五)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/42291149 之前的四篇博文记录的都是linux中的input体系相关的东西,最底层以我调试的u ...
- POJ1673 ZOJ1776 三角形四心模板
POJ1673 题中所述点即为三角形的垂心,用向量法可以轻松证明. 垂心 重心 外心 均位于三角形的欧拉线上,且三者有线性关系,于是,求出重心和外心即可求得垂心. 重心就是三点的平均值,外心可以通过解 ...
- bzoj1791
1791: [Ioi2008]Island 岛屿 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1680 Solved: 369[Submit][S ...
- springmvc 用fasterxml.jackson返回son数据
一,引入fasterxm.jackson包 <dependency> <groupId>com.fasterxml.jackson.core</groupId> & ...
- 栗染-github中搭建博客遇到的问题之一
运行命令:git push -u origin master To https://github.com/xuzhezhaozhao/Practice.git ! [rejected] master ...
- Odoo免费开源企业信息化平台助力企业成功
企业信息化变革之路 信息孤岛的真实由来 打开百度App,看更多图片 左边为当下企业现状,右边为Odoo的整体 企业信息孤岛的严重性,来自于企业的自身高速发展,企业以销售为生命主题围绕着客户会搭建一系列 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Train Seats Reservation
You are given a list of train stations, say from the station 11 to the station 100100. The passenger ...
- 专题七:UDP编程补充——UDP广播程序的实现
一.程序实现 UDP广播程序的实现代码: using System; using System.Net; using System.Net.Sockets; using System.Text; us ...
- 06使用NanoPiM1Plus在Android4.4.2下接U盘
06使用NanoPiM1Plus在Android4.4.2下接U盘 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本:V ...