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个数表示对应冰淇淋的品种 走到连续的几个摊位 会买下走过的摊位的所有的冰淇淋 求 要买下所 ...
随机推荐
- hdu 4990 Reading comprehension(等比数列法)
题目链接:pid=4990" style="color:rgb(255,153,0); text-decoration:none; font-family:Arial; line- ...
- ubutu14.04无法使用sudo,也无法切换到root用户去解决问题怎么办?
一不小心,修改了/etc/sudoers文件. 惨了. 无法使用sudo了,啥都干不成了. 最最关键的是,也无法用root登录. 本想着要重装系统了. 后来发现了神奇的ubuntu安全模式. 1.重启 ...
- IDEA 的使用(快捷键、括号对齐的方式)
Java IDE 工具不是只有一个 Eclipse,还有同样十分优秀的 IDEA. 0. 常用快捷键 查看与设置:[File]⇒ [Settings]⇒ [Keymap] back/forward:c ...
- bzoj3270
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 474 Solved: 261[Submit][Status][Discuss] ...
- Spark 分布式环境--连接独立集群管理器
Spark 分布式环境:master,worker 节点都配置好的情况下 : 却无法通过spark-shell连接到 独立集群管理器 spark-shell --master spark://soyo ...
- Windows Java环境变量配置
安装步骤略过... 环境变量配置 新建环境变量: JAVA_HOME C:\Program Files\Java\jdk1.6 将路径替换为自己的安装路径. 新建环境变量: classpath ...
- Linux 用户管理(2)
Linux 用户管理2 添加修改和删除用户,必须是超级管理员root账号才可以进行的操作,所以当当前账号不是超级管理员root账号时,首先要先切换为root账号. 如图,ylq为普通用户,执行添加用户 ...
- 17年day3
/* 嗯,又一天. 时日无多了,还能蹦哒几天? 上午依旧考试,日常挂T1,读错题.还是好困. 兔子说明天晚上要请我们吃水饺~~~~去年就没这待遇. 下午打开邮箱一看,咦?嗯. 昨晚做噩梦NOIP考了状 ...
- 测试神器Swagger的相关使用
1.Swagger简介 swagger官网地址: https://swagger.io/ swagger官网文档介绍地址: https://swagger.io/about/ swagge是一个易 ...
- 51nod1298 圆与三角形
1298 圆与三角形 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三 ...