poj 3320(尺取法)
参考资料:
[1]:挑战程序设计竞赛
题意:
一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数。
题解:
相关说明:
设以a[start]开始的最初包含所有知识点的最少连续子序列为a[start,....,end];
mymap[ a[i] ] : 知识点 a[i] 在当前最少连续子序列中出现的次数。
(1):求出所需复习的知识点总个数。
(2):求出最先包含所有知识点的最少页数a[start,........,end]。
(3):end++,mymap[ a[end] ]++,并判断mymap[ a[start] ]是否大于1,如果大于,start++,直到不大于为止,并更新 res。
(4):重复(3)过程,直到end > P。
AC代码:
#include<iostream>
#include<cstdio>
#include<set>
#include<map>
using namespace std;
const int maxn=1e6+; int P;
int idea[maxn];
set<int>myset;
map<int ,int >mymap; int Solve()
{
int sumIdea=myset.size();//步骤(1)
int start=,end=;
while(sumIdea != )//步骤(2)
{
sumIdea -= (mymap[idea[end]] == ? :);
mymap[idea[end++]]++;
while(mymap[idea[start]] > )
mymap[idea[start++]]--;
}
int res=end-start;
while(end <= P)//重复执行步骤(3)(4)
{
mymap[idea[end++]]++;
while(mymap[idea[start]] > )
mymap[idea[start++]]--;
res=min(res,end-start);
}
return res;
} int main()
{
scanf("%d",&P);
for(int i=;i <= P;++i)
{
scanf("%d",idea+i);
myset.insert(idea[i]);//set去重
mymap[idea[i]]=;
}
printf("%d\n",Solve());
}
#include<iostream>
#include<cstdio>
#include<set>
#include<map>
using namespace std;
const int maxn=1e6+; int P;
int idea[maxn];
set<int>myset;
map<int ,int >mymap; int Solve()
{
int sumIdea=myset.size();
int start=,end=;
int total=;
int res=;
while()
{
while(end <= P && total < sumIdea)
total += ((mymap[idea[end++]]++) == ? :); if(total < sumIdea)//如果 total < sumIdea,说明跳出上一个while()的条件为 end > P
break;
res=(res == || res > end-start ? end-start:res);
if(--mymap[idea[start++]] == )
total--;
}
return res;
} int main()
{
scanf("%d",&P);
for(int i=;i <= P;++i)
{
scanf("%d",idea+i);
myset.insert(idea[i]);//set去重
mymap[idea[i]]=;
}
printf("%d\n",Solve());
}
挑战程序设计竞赛
poj 3320(尺取法)的更多相关文章
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
- POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...
- A - Jessica's Reading Problem POJ - 3320 尺取
A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...
- poj 2100(尺取法)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 6107 Accepted: 1444 ...
- POJ 2566 尺取法(进阶题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4297 Accepted: 1351 Spe ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- 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 ...
随机推荐
- 【ML】Two-Stream Convolutional Networks for Action Recognition in Videos
Two-Stream Convolutional Networks for Action Recognition in Videos & Towards Good Practices for ...
- 使用highcharts绘制美观的燃尽图
使用highcharts绘制美观的燃尽图 助教在博客中介绍了两种绘制燃尽图的方法,但是我们组在使用时发现有些任务不适合写进issue,而且网站生成的燃尽图不是很美观,因此我们打算使用其他方法自己绘制燃 ...
- 《Linux内核设计》第17章学习笔记
- 软件项目第一次sprint评论
软件项目第一次sprint评论 组名:天线宝宝四人组 姓名:高长志 1. 组名:9-652组 项目:炸弹人游戏 对于炸弹人游戏,首先 ...
- Leetcode 279. 完全平方数
题目描述: https://leetcode-cn.com/problems/perfect-squares/ 解题思路: 同样是dp,一开始的想法是,对于每个数i做拆分为j和(i-j),利用动态转移 ...
- 软件工程APP进度更新
对原有的界面进行了美化,同时加进了背景音乐,并且优化了算法部分的代码 正在一步一步跟进中 顺带附上上一次组员帮我发的进度地址:http://www.cnblogs.com/case1/p/498192 ...
- 【转】Linux tail 命令详解
Linux tail 命令详解 http://www.2cto.com/os/201111/110143.html
- Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作
Java: String typeStr = "1,2"; String[] typeArray = typeStr.split(","); typeStr = ...
- [转载]Memory Limits for Windows and Windows Server Releases
Memory Limits for Windows and Windows Server Releases This topic describes the memory limits for sup ...
- matplotlib绘图2
fig=plt.figure() fig.add_subplot(3,3,1)#3行3列 第一个图 n=128 X=np.random.normal(0,1,n) Y=np.random.normal ...