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

就是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. LVS Nginx Haproxy对比

    一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术. 具体的应用需求还得具体分析,如果是中小型的Web应用,比如日PV小于1000万,用Nginx就完全可以了: 如果机器不少,可以 ...

  2. Video标签的使用

    现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8(如果这玩意儿没出事的话)的(Opera.Mozilla.Chrome),支持H.264的(Safari.IE ...

  3. java对象生命周期概述复习

    最近看了下java对象的生命周期做个笔记复习复习,很多不同的原因会使一个java类被初始化,可能造成类初始化的操作: 1)  创建一个java类的实例对象. 2)  调用一个java类中的静态方法. ...

  4. iOS 几种常用的 crash log 崩溃信息调试方法

    前言:crash log 对 定位崩溃问题 ,并且不容易复现,尤其是及时对appstore 上正在运营的 app 的迭代改进来说 非常重要. 1 crash两种情况 1.1 测试环境下 追踪bug 1 ...

  5. 安装好MySQL后就开始学习如何后台创建自己的数据库吧!

    MySQL创建数据库的方式不像SQL Server那样有图形界面,而是使用DOS窗口创建的,接下来就是创建MySQL数据库的具体步骤. ↓↓↓↓↓↓↓↓      ↓↓↓↓↓↓↓↓      ↓↓↓↓ ...

  6. ssh登陆virtualbox虚拟机

  7. hadoop02---高可用网站架构

    tomcat每个请求都会占用内存cpu,tomcat没有代理功能.nginx是俄国人写的,nginx是静态资源服务器,既可以自己返回请求,也可以做代理进行转发,和负载均衡.Tomcat是动态资源jav ...

  8. centos中安装php7

    centos7下安装php7 php7 centos7 安装PHP7 首先安装一些必须的依赖,这里就不阐述了,后面文章再细说 yum install -y \ gcc-c++ autoconf \ l ...

  9. QtGstreamer 编译

    一  安装依赖项 1 安装cmake hdhuang@hdh-UBT:~/gstreamer/qt-gstreamer-0.10.2/build$ sudo apt-get install cmake ...

  10. gstreamer——文档/资源/使用

    http://gstreamer.freedesktop.org/src/ http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gs ...