地址 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. JavaScript 逻辑与(&&) 与 逻辑或(||) 运算规则

    逻辑与(&&) 逻辑与(&&)操作可以应用于任何的操作类型,不仅仅是布尔值, 在有一个操作数不是布尔值的情况下,&&操作符就不一定返回布尔值:遵循下面规 ...

  2. 《一头扎进》系列之Python+Selenium框架设计篇1-什么是自动化测试框架-价值好几K的框架,不看别后悔,过时不候

    1. 什么是自动化测试框架 在了解什么是自动化测试框架之前,先了解一下什么叫框架?框架是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法:另一种定义认为,框架是可被应用开发者定制的 ...

  3. Laravel 中使用 swoole 项目实战开发案例二 (后端主动分场景给界面推送消息)

    推荐阅读:Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信)​ 需求分析 我们假设有一个需求,我在后端点击按钮 1,首页弹出 “后端触发了按钮 1”.后端点了 ...

  4. Brett Beauregard大神的Arduino PID算法

    大神的全部PID http://brettbeauregard.com/blog/category/pid/ Improving the Beginner’s PID – Introduction I ...

  5. 将数据库中数据导出为excel表格

    public class Excel { private static Logger logger = LoggerFactory.getLogger(Excel.class); /** * 导出项目 ...

  6. Go 面试每天一篇(第 65 天)

    Go 面试每天一篇(第 65 天) 1.下面列举的是 recover() 的几种调用方式,哪些是正确的? A. 1func main() { 2 recover() 3 panic(1) 4} B. ...

  7. mybatis源码学习(四)--springboot整合mybatis原理

    我们接下来说:springboot是如何和mybatis进行整合的 1.首先,springboot中使用mybatis需要用到mybatis-spring-boot-start,可以理解为mybati ...

  8. Nginx 常用配置方式说明

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/7619763f-1 ...

  9. [ASP.NET Core 3框架揭秘] 配置[6]:多样化的配置源[上篇]

    .NET Core采用的这个全新的配置模型的一个主要的特点就是对多种不同配置源的支持.我们可以将内存变量.命令行参数.环境变量和物理文件作为原始配置数据的来源.如果采用物理文件作为配置源,我们可以选择 ...

  10. 如何下载Vimeo视频

    MediaHuman YouTube Downloader是应用在Mac上的一款非常优秀的YouTube视频下载工具,YouTube Downloader破解版将帮助你快速完成视频下载,而不会挂断.您 ...