这两道题都是用的尺取法。尺取法是《挑战程序设计竞赛》里讲的一种常用技巧。

就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针(t)要么不动要么也往前走。满足这种特点的就可以考虑尺取法。

poj3061 比较简单,也可以用二分做,时间复杂度O(n*logn)。用尺取法可以O(n)解决。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
int T,N,S,a[];
int main()
{
//freopen("in7.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&S);
int ans=INF;
for(int i=;i<N;i++)
{
scanf("%d",&a[i]);
}
int s=,t=,sum=;
while()
{
while(t<N&&sum<S)
{
sum+=a[t];
t++;
}
if(sum<S) break;
ans=min(ans,t-s);
//注意这里的距离不是(t-s+1),因为我前一个while中最后t++了,所以
//现在是s到t的左闭右开区间
sum-=a[s];
s++;
}
if(ans<INF)
cout<<ans<<endl;
else
cout<<''<<endl;
}
//fclose(stdin);
//fclose(stdout);
return ;
}

poj3320 对数据用set和map来处理显得很方便,核心部分也是尺取法。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
int P,a[];
set<int>st;
map<int,int>mp;
int main()
{
// freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&P);
for(int i=;i<P;i++)
{
scanf("%d",&a[i]);
st.insert(a[i]);
}
int tol=st.size();
int s=,t=;
int ans=INF;
for(;;)
{
while(t<P&&mp.size()<tol)
{
if(mp.count(a[t])) mp[a[t++]]++;
/*在map里用count函数,有返回1,没有就返回0*/
else mp[a[t++]]=;
}
if(mp.size()<tol) break;
ans=min(ans,t-s);
mp[a[s]]--;
if(mp[a[s]]==) mp.erase(a[s]);
s++;
}
cout<<ans<<endl;
//fclose(stdin);
//fclose(stdout);
return ;
}

poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)的更多相关文章

  1. POJ3320 Jessica's Reading Problem(尺取+map+set)

    POJ3320 Jessica's Reading Problem set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用 #inc ...

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

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

  3. 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 ...

  4. POJ3320 Jessica's Reading Problem 2017-05-25 19:55 38人阅读 评论(0) 收藏

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

  5. poj3320 Jessica's Reading Problem(尺取思路+STL)

    https://vjudge.net/problem/POJ-3320 尺取法,要想好组织方式. 又被卡了cin.. #include<iostream> #include<cstd ...

  6. poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9134   Accep ...

  7. poj3320 Jessica's Reading Problem

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

  8. POJ3320 Jessica's Reading Problem

    Bryce1010模板 #include <stdio.h> #include <string.h> #include <stdlib.h> #include &l ...

  9. 【二分】Jessica's Reading Problem

    [POJ3320]Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1309 ...

随机推荐

  1. set去重,session,cookie c#与python 对比

    端口,发送请求进行监听,然后处理 session 是存储在服务器端的数据,靠sessionId来验证获取信息,没有大小和类型限制, cookie   是存储在客户端的数据,可以长期使用,有面临被获取的 ...

  2. Java转C#,非常不错(转)

    http://www.cnblogs.com/cnwebman/archive/2012/07/21/2602436.html 最近正在研究将一个纯java工程如何转换成C#工程,代码量还比较大,于是 ...

  3. MySQL数据库(7)_MySQL 数据备份与还原

    一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先 ...

  4. asp.net,缓存Cache

    缓存Cache: >直接使用Cache["content"],缓存与Session不同,所有用户都可以共享.永不过期,由服务器自己维护,当内存不够时,会将老的缓存释放掉. & ...

  5. Bürkert 流体控制系统 (8611 型通用调节器)

    Type Description High-Tech Made EasyThe new universal controller eCONTROL Type 8611 brings an essent ...

  6. R语言的输出函数cat,sink,writeLines,write.table

    根据输出的方向分为输出到屏幕和输出到文件. 1.cat函数即能输出到屏幕,也能输出到文件. 使用方式:cat(... , file = "", sep = " " ...

  7. ubuntu关闭631(cups)端口

    在ubuntu17.04环境下使用nmap扫描自己机器,发现631端口处于开启状态,将其输入到浏览器,可以看出是网络打印机的服务: 这个端口开着总是那么的刺眼,(5.12全球爆发的勒索病毒让人不寒而栗 ...

  8. Anaconda创建环境、删除环境、激活环境、退出环境

    Anaconda创建环境: //下面是创建python=3.6版本的环境,取名叫py36 conda create -n py36 python=3.6  删除环境(不要乱删啊啊啊) conda re ...

  9. [BZOJ4730][清华集训2016][UOJ266] Alice和Bob又在玩游戏

    题意:俩智障又在玩游戏.规则如下: 给定n个点,m条无向边(m<=n-1),保证无环,对于每一个联通块,编号最小的为它们的根(也就是形成了一片这样的森林),每次可以选择一个点,将其本身与其祖先全 ...

  10. 泛型学习第一天:List与IList的区别 (一)

    先看代码: using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace L ...