poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S

那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min

#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100005
#define LL long long
#define INF 0x3f3f3f3f using namespace std;
LL a[];
int n, t, ans = INF;
LL sum, s; int main()
{
scanf("%d", &t);
while (t--){
scanf("%d %I64d", &n, &s);
for (int i = ; i < n; i++) scanf("%I64d", a+i);
int st = , en = ;
ans = INF; sum = ;
while (){
while (en<n && sum<s) sum += a[en++];
if (sum < s) break;
ans = min(ans, en-st);
sum -= a[st++];
}
if (ans == INF) ans = ;
printf("%d\n", ans);
}
return ;
}

poj3320给一本书有P页,每页都有一个知识点,求最少的连续页数覆盖所有知识点

如果一个区间的子区间满足条件,那么右端点固定,左端点推进

反之左端点固定 ,右端点推进么。需要用map进行映射,set求总共有多少知识点

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#define MAX 1000010
#define LL long long
#define INF 0x3f3f3f3f using namespace std;
int a[MAX];
map <int, int> cnt;
set <int> t;
int p, ans = INF, st, en, sum; int main()
{
scanf("%d", &p);
for (int i = ; i < p; i++) scanf("%d", a+i), t.insert(a[i]);
int num = t.size();
while (){
while (en<p && sum<num)
if (cnt[a[en++]]++ == ) sum++;
if (sum < num) break;
ans = min(ans, en-st);
if (--cnt[a[st++]] == ) sum--;
}
printf("%d\n", ans);
return ;
}

poj2566 给定一个数组和值t,求一个子区间使得其和的绝对值与t的差值最小

有正有负,不能保证单调性,无法单点拓展边界,预处理出所有的前缀和,升序排列

然后尺取法求出两个端点,使区间和的绝对值最逼近t

#include <cstdio>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long
#define MAX 100010
using namespace std; typedef pair<LL, int> p;
LL a[MAX], t, ans, tmp, b;
int n, k, l, u, st, en;
p sum[MAX]; LL myabs(LL x)
{
return x>=? x:-x;
} int main()
{
while (scanf("%d %d", &n, &k), n+k){
sum[] = p(, );
for (int i = ; i <= n; i++){
scanf("%I64d", a+i);
sum[i] = p(sum[i-].first+a[i], i);
}
sort(sum, sum++n);
while (k--){
scanf("%I64d", &t);
tmp = INF; st = , en = ;
while(en <= n){
b = sum[en].first-sum[st].first;
if(myabs(t-b) < tmp){
tmp = myabs(t-b);
ans = b;
l = sum[st].second; u = sum[en].second;
}
if(b > t) st++;
else if(b < t) en++;
else break;
if(st == en) en++;
}
if (u < l) swap(u, l);
printf("%I64d %d %d\n", ans, l+, u);
}
}
return ;
}

poj3061 poj3320 poj2566尺取法基础(一)的更多相关文章

  1. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  2. Bound Found [POJ2566] [尺取法]

    题意 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. Input The input file contains several test cases. Each t ...

  3. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  4. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  5. poj2566 尺取法

    题意: 输入 n m  之后输入n个数  之后m个询问  对于每个询问 输入一个t    输出  三个数 ans l r  表示从l 到 r的所有数的和的绝对值最接近t 且输出这个和ans   思路: ...

  6. poj2739 poj2100 尺取法基础(二)

    都是很简单的题目 poj2739素数打表+单点推移 #include<iostream> #include<cstring> #include<cstdio> us ...

  7. poj3061 Subsequence【尺取法】

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  8. 尺取法 poj3061 poj3320

    尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...

  9. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

随机推荐

  1. Curl中的参数知多少

    我们常用的curl命令,后面有好多参数,都是什么含义呢?遂记录此文以备用. Curl命令参数解释: -a/--append 上传文件时,附加到目标文件 -A/--user-agent <stri ...

  2. Tomcat的配置文件详解

    Tomcat的配置文件详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Tomcat的配置文件 Tomcat的配置文件默认存放在$CATALINA_HOME/conf目录中, ...

  3. 多播委托和匿名方法再加上Lambda表达式

    多播委托就是好几个方法全都委托给一个委托变量 代码: namespace 委托 { class Program { static void math1() { Console.WriteLine(&q ...

  4. 设计模式---单一职责模式之装饰模式(Decorator)

    前提:"单一职责"模式 在软件组件的设计中,如果责任划分的不清晰,使用继承,得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任 典型模式(表现 ...

  5. python---RabbitMQ(1)简单队列使用,消息依次分发(一对一),消息持久化处理

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...

  6. 学习windows编程 day3 之 自定义画笔的两种方法

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

  7. JAVA记录-SpringMVC国际化配置

    1.搭建SpringMVC框架,不过多阐述 2.spring-mvc.xml加入以下配置: <!-- 国际化资源配置,资源文件绑定器--> <bean id="messag ...

  8. 虚拟机下Linux(终端)配置网络的方法

    这几天在虚拟机vmware上部署centos系统,想通过内部联网用yum命令安装必需的软件,但是一直不能静态地址联网,今天终于找到一个方法centos内部设置IP,对外联网.设置过程如下: .首先是网 ...

  9. HITS算法--从原理到实现

    本文介绍HITS算法的相关内容. 1.算法来源 2.算法原理 3.算法证明 4.算法实现 4.1 基于迭代法的简单实现 4.2 MapReduce实现 5.HITS算法的缺点 6.写在最后 参考资料 ...

  10. spring注解第06课 @Value

    1.注入<bean>中的属性 支持3种类型的赋值 <bean id="person" class="com.model.Person"> ...