poj3320尺取法
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.
A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.
Input
The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.
Output
Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.
Sample Input
5
1 8 8 8 1
Sample Output
2
题意:找最少的连续页数使得覆盖全书的所有知识点
题解:尺取法,也叫做two point 很形象就是两段不断改变来找到最值。时间复杂度减低了很多
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int p,a[N];
set<int>ss;
map<int,int>m; int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
scanf("%d",&p);
for(int i=;i<p;i++)
{
scanf("%d",&a[i]);
ss.insert(a[i]);
}
int n=ss.size();
int num=,st=,en=,ans=p;
for(;;)
{
while(en<p&&num<n){
if(m[a[en++]]++==)num++;
}
if(num<n)break;
ans=min(ans,en-st);
if(--m[a[st++]]==)num--;
}
printf("%d\n",ans);
return ;
}
还有就是不知道为啥用cin和cout居然TLE了,而且我还加了
ios::sync_with_stdio(false);
cin.tie(0);
代码也放上来!!
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int p,a[N]; int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>p;
set<int>ss;
for(int i=;i<p;i++)
{
cin>>a[i];
ss.insert(a[i]);
}
int n=ss.size();
map<int,int>m;
int num=,st=,en=,ans=p;
for(;;)
{
while(en<p&&num<n){
if(m[a[en++]]++==)num++;
}
if(num<n)break;
ans=min(ans,en-st);
if(--m[a[st++]]==)num--;
}
cout<<ans<<endl;
return ;
}
poj3320尺取法的更多相关文章
- POJ3320 尺取法的正确使用法
一.前言及题意: 最近一直在找题训练,想要更加系统的补补思维,补补漏洞什么的,以避免被个类似于脑筋急转弯的题目干倒,于是在四处找书,找了红书.蓝书,似乎都有些不尽如人意.这两天看到了日本人的白书,重新 ...
- poj3320(尺取法)
题目大意:给你一串数字,找出最小的能够覆盖所有出现过的数字的区间长度: 解题思路:依旧是尺取法,但要用map标记下出现过的书: 代码:别用cin输入: #include<iostream> ...
- poj3320 (尺取法)
n个数,求最小区间覆盖着n个数中所有的不相同的数字. 解题思路: AC代码: import java.util.HashMap; import java.util.HashSet; import ja ...
- 尺取法 poj3061 poj3320
尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
- 【尺取法】POJ3061 & POJ3320
POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...
- 【转】毛虫算法——尺取法
转自http://www.myexception.cn/program/1839999.html 妹子满分~~~~ 毛毛虫算法--尺取法 有这么一类问题,需要在给的一组数据中找到不大于某一个上限的&q ...
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
随机推荐
- 笔记本win10关机异常解决
自从使用了win10 以后,小编已经情不自禁的爱上了她——迄今为止最NB的windows系统 但令人头疼的问题也随之而来,前几天购置了一款三星的SSD固态硬盘,马上就装了win10,某天晚上关机以后发 ...
- JavaScript数据结构——链表的实现
前面楼主分别讨论了数据结构栈与队列的实现,当时所用的数据结构都是用的数组来进行实现,但是数组有的时候并不是最佳的数据结构,比如在数组中新增删除元素的时候需要将其他元素进行移动,而在javascript ...
- QQ互联 redirect uri is illegal(100010)的解决办法,很简单
我的地址栏内容是:http://openapi.qzone.qq.com/oauth/show?which=ConfirmPage&display=pc&response_type=c ...
- 使用Topshelf组件构建简单的Windows服务
很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解 ...
- node.js报错throw err; // Rethrow non-MySQL errors e:\serverTest\node_modules\mysql\lib\protocol\Parser.js:79 解决方法
今天在用node+angular做后台时,需要使用session保存登陆状态的时候,遇到了此问题,问题直译为非mysql问题,我也在后台取到的登陆用户名和密码,确实不是数据库问题.最后发现在使用ses ...
- 安装psacct或acct程序包
监视Linux用户活动 我认为,对每个想密切监视其服务器/系统上用户活动的Linux/Unix系统管理员来说,psacct或acct是优秀的.必需的应用程序之一. psacct或acct程序包提供了用 ...
- Mycat中的核心概念
Mycat中的核心概念 Mycat中的核心概念 1.数据库中间件 Mycat 是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而 Mycat 并没有 存储引擎,所以并 ...
- vue-router2.0简单路由嵌套
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Alamofire源码解读系列(十一)之多表单(MultipartFormData)
本篇讲解跟上传数据相关的多表单 前言 我相信应该有不少的开发者不明白多表单是怎么一回事,然而事实上,多表单确实很简单.试想一下,如果有多个不同类型的文件(png/txt/mp3/pdf等等)需要上传给 ...
- SQL Server 备份所有数据库代码
今天让我备份一下网上所有数据库,猛地一看,几百个呢, 坑爹呢,只好网上找找有没有简便的,没想到还真有 记下来,以后好用,哈哈... use master declare @DbName varchar ...