地址 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. Python3 并发编程2

    目录 进程互斥锁 基本概念 互斥锁的使用 IPC 基本概念 队列 生产者消费者模型 基本概念 代码实现 线程 基本概念 创建线程 线程互斥锁 进程互斥锁 基本概念 临界资源: 一次仅允许一个进程使用的 ...

  2. UWP 中的全局异常处理

    问题 在开发一款应用的过程中,我们开发者很难考虑到所有问题,往往会忘记处理一些可能发生的异常.随之而来的结果就是用户使用过程中接连不断的崩溃.所以,我们有必要处理所有未被我们处理的异常. 本文介绍了 ...

  3. vue中$attrs和$listeners以及inheritAttrs的用法

    官方文档说明: 一.解释:包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外). 意思就是父组件往子组件传没有在props里声明过的值时,子组件可以通 ...

  4. C++构造函数的几种使用方法

    在C++中,有一种特殊的成员函数,他的名字和类相同,没有返回值,不需要用户显示调用,用户也无法调用,而是在创建对象的时候自动执行. 这种特殊的函数就是构造函数 Constructor 构造函数的名字与 ...

  5. SpringBoot微服务电商项目开发实战 --- 分布式文件系统实现

    SpringBoot分布式开发系列文章已经持续了一段时间了,每一篇都有核心内容讲给大家.比如:分环境部署配置及服务端口号统一配置,子模块版本号管理及第三方jar依赖管理,单点登录实现,接口安全(签名+ ...

  6. Redis两种持久化策略分析

    Redis专题地址:https://www.cnblogs.com/hello-shf/category/1615909.html SpringBoot读源码系列:https://www.cnblog ...

  7. JS---案例---左右焦点轮播图(tb)

    案例---左右焦点轮播图(tb) <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  8. SAP B1:水晶报表中用Code128制作条型码的方法

    一.在[文件系统]中新建一个[Fonts文件夹],然后添加[Code128.ttf]文件. 二.在水晶报表里的[字段资源管理器]的[公式字段]中新建一个公式字段.点击[使用编辑器]之后弹出[公式工作室 ...

  9. 数据库学习笔记day03

    创建两个表,一个名为emp,一个名为dept,并且插入数据 create table emp(empno number(4,0),ename varchar2(10),job varchar2(9), ...

  10. Linux MySQL的root无法登录数据库ERROR 1045 (28000)

    Linux环境下,脚本自动安装完数据库,命令行用mysql -uroot -ppasswaord 登录却报了这么个错: ERROR 1045 (28000): Access denied for us ...