<挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针
地址 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 双指针的更多相关文章
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 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 ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 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 ...
- 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 ...
- 题解报告: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 ...
随机推荐
- 笔记||Python3之文件的读写
[文件的读模式] 文件的对象:文件的读写通过文件操作对象进行. Python2 ----- File Python3 ----- TextIOWrapper ...
- Django 07
目录 ORM查询优化 only与defer(单表) select_related与prefetch_related(跨表) choices参数 MTV与MVC模型 Ajax简介 前后端传输数据编码格式 ...
- 【JS】324- JS中的内存管理(中高级前端必备)
前言 像C语言这样的底层语言一般都有底层的内存管理接口,比如 malloc()和free()用于分配内存和释放内存.而对于JavaScript来说,会在创建变量(对象,字符串等)时分配内存,并且在不再 ...
- PHP7 break和continue的区别
break:结束当前 for,foreach,while,do-while 或者 switch 结构的执行. continue:在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次 ...
- leetcode-164、最大间距
export default (arr) => { // 如果数组长度小于2返回0 if (arr.length < 2) { return 0 } // 排序 arr.sort() // ...
- liunx新装tomcat之后,tomcat不能识别新发布的项目
遇到的问题 在liunx新装tomcat之后,发布之前的项目,发现在tomcat不能识别新发布的项目,打成war包,还是直接把项目拷贝过去都不行. 环境:虚拟机:VMware 主机系统:win10 虚 ...
- .NET 中数据访问用的 DBHelper(Sql Server) 类
public class DBHelper { private static string DBConnectString = "Data Source=.;Initial Catalog= ...
- poj 1077 Eight (八数码问题——A*+cantor展开+奇偶剪枝)
题目来源: http://poj.org/problem?id=1077 题目大意: 给你一个由1到8和x组成的3*3矩阵,x每次可以上下左右四个方向交换.求一条路径,得到12345678x这样的矩阵 ...
- [ASP.NET Core 3框架揭秘] 跨平台开发体验: Windows [中篇]
我们在<上篇>利用dotnet new命令创建了一个简单的控制台程序,接下来我们将它改造成一个ASP.NET Core应用.一个ASP.NET Core应用构建在ASP.NET Core框 ...
- Winform中实现仿XP系统的任务栏菜单效果(附代码下载)
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个Fo ...