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. sed命令使用介绍(转载)

    sed命令介绍 (转自:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html) 简介 sed 是一种在线编辑器,它一次处理一 ...

  2. Scala进阶之路-Spark底层通信小案例

    Scala进阶之路-Spark底层通信小案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spark Master和worker通信过程简介 1>.Worker会向ma ...

  3. 5、Python-字典

    定义 info = {'name': '班长', 'id': 88, 'sex': 'man', 'address': '地球亚洲中国北京'} print(info['name']) print(in ...

  4. secure CRT the remote system refused the connection 解决办法

    1.安装ssh服务器和客户端 apt-get install openssh-server apt-get install openssh-client 2.重启ssh /etc/init.d/ssh ...

  5. postgresql 常用速查

    中文资料 中文资料 /**gp中的基本sql语法**/ --删除表 drop table testtb; --创建表 CREATE TABLE testtb ( id integer, "n ...

  6. Ajax提交请求模板

    function methodName() { var params = { }; var url = ''; jQuery.ajax({ type: 'POST', contentType: 'ap ...

  7. web4.0基本配置

    const path = require('path');//引入路径包 const HWP = require('html-webpack-plugin');//引入自动产出html包 const ...

  8. Jacob用法收集

    介绍 Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调 ...

  9. IEEE 802.1X标准

    1.介绍 802.1X是一个IEEE标准,通过对用户进行基于端口的安全认证和对密钥的动态管理,从而实现保护用户用户的位置隐私和身份隐私以及有效保护通信过程中信息安全的目的. 在802.1X协议中,只有 ...

  10. tessaract ocr简介

    Tesseract的历史Tesseract是一个开源的OCR引擎,惠普公司的布里斯托尔实验室在1984-1994年开发完成.起初作为惠普的平板扫描仪的文字识别引擎.Tesseract在1995年UNL ...