地址 http://poj.org/problem?id=3320

解答

使用双指针 在指针范围内是否达到要求

若不足要求则从右进行拓展  若满足要求则从左缩减区域

代码如下  正确性调整了几次 然后被输入卡TLE卡了很久都没意识到.........

 #include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <assert.h>
#include <stdio.h> using namespace std; int n, m; const int MAX_N = ; int v[MAX_N]; set<int> setint; int minLen = ; int main()
{
cin >> n; for (int i = ; i < n; i++) {
//cin >> v[i];
scanf("%d",&v[i]);
setint.insert(v[i]);
} int total = setint.size(); int l = ; int r = ;
map<int, int> mapcover;
mapcover[v[l]]++;
int count = ; while() {
while (count < total) {
r++;
if (r >= n) {
printf("%d\n", minLen);
return ;
}
mapcover[v[r]]++;
if (mapcover[v[r]] == ) {
//说明添加了一个新类型
count++;
}
} if (count < total) {
break;
} minLen = min(minLen, r - l + ); //尝试缩小整个范围 从左端减少
mapcover[v[l]]--;
if (mapcover[v[l]] == ) {
//说明减少了一个种类
count--;
}
l++;
} printf("%d\n", minLen);
return ;
}

ac code 1

 #include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <assert.h>
#include <stdio.h> using namespace std; int n, m; const int MAX_N = ; int v[MAX_N]; set<int> setint; int minLen = ; int main()
{
cin >> n; for (int i = ; i < n; i++) {
scanf("%d",&v[i]);
setint.insert(v[i]);
} int total = setint.size(); int l = ; int r = ;
map<int, int> mapcover;
mapcover[v[l]]++;
int count = ;
while () {
if (count < total) {
r++;
if (r >= n) break;
mapcover[v[r]]++;
//添加了一个新元素 那么元素种类计数+1
if (mapcover[v[r]] == ) count++; }
else if(count == total) {
minLen = min(minLen, r - l + );
mapcover[v[l]]--;
if (mapcover[v[l]] == ) {
count--;
}
l++;
if (l >= n) break;
if (l > r) {
r = l;
}
}
else {
assert();
} } printf("%d\n",minLen); return ;
}

ac code 2

<挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针的更多相关文章

  1. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  2. POJ 3320 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6001   Accept ...

  3. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  4. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  5. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  6. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  7. poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  8. POJ 3320 Jessica's Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  9. 题解报告:poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

随机推荐

  1. 基于Pact的契约测试

    背景 如今,契约测试已经逐渐成为测试圈中一个炙手可热的话题,特别是在微服务大行其道的行业背景下,越来越多的团队开始关注服务之间的契约及其契约测试. 什么是契约测试     关于什么是契约测试这个问题, ...

  2. 了解web漏洞-sql注入

    1:为什么要学web漏洞? 作为一个运维人员,日常工作就是保障服务器和网站的业务正常运行,平时也需要对服务器的安全工作加固,说到防护攻击问题,那么久必须去了解攻击者是怎么对服务器发动的一个流程,这样才 ...

  3. 《一头扎进》系列之Python+Selenium框架设计篇4- 价值好几K的框架,呵!这个框架有点意思啊

    1.简介 前面文章,我们实现了框架的一部分功能,包括日志类和浏览器引擎类的封装,今天我们继续封装一个基类和介绍如何实现POM.关于基类,是这样定义的:把一些常见的页面操作的selenium封装到bas ...

  4. Redis-API

    Redis-API 简介 Redis 是一个基于内存的高效的键值行非关系型数据库,存取效率极高. python提供了两个类:分别为Redis和StrictRedis来实现Redis的命令操作.Redi ...

  5. Python连载58-http协议简介

    一.http协议实战 1.URL(Uniform Resource Located) (1)使用FFTP的URL,例如:ftp://rtfm.mit.edu (2)使用HTTP的URL,例如:http ...

  6. 通过 Drone Rest API 获取构建记录日志

    Drone是一款CICD工具,提供rest API,简单介绍下如何使用API 获取构建日志. 获取token 登录进入drone,点头像,在菜单里选择token 复制token即可 API 介绍 Dr ...

  7. C#界面设计相关设置

    1.Anchor属性设置 对需要设置的控件,如主窗体中的TextBox,设置Anchor为上下左右都停靠,就会实现随着窗体的变化而变化. 2.AutoScaleMode属性的用法:<转自:htt ...

  8. Elasticsearch(GEO)数据写入和空间检索

    Elasticsearch简介 什么是 Elasticsearch? Elasticsearch 是一个开源的分布式 RESTful搜索和分析引擎,能够解决越来越多不同的应用场景. 本文内容 本文主要 ...

  9. Android utils 工具类之MD5加密 MD5Utils

    实际开发过程中,密码不会保存明文形式,容易被别人窃取. MD5加密是不可逆的加密算法,只能由明文转换成密文. 但是有些人通过将明文对应的密文保存在数据库中,进行查询破解密文.为防止这种解密,根据许多对 ...

  10. SAP中的数据库表索引

    数据库表中的索引可以加快查询的速度.索引是数据库表字段的有序副本.附加的字段包含指向真实数据库表行的指针.排序可以使访问表行的速度变快,例如,可以使用二分搜索.数据库表至少有一个主索引,由它的key字 ...