poj3061 poj3320 poj2566尺取法基础(一)
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尺取法基础(一)的更多相关文章
- poj3061 Subsequence(尺取法)
https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...
- Bound Found [POJ2566] [尺取法]
题意 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. Input The input file contains several test cases. Each t ...
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
- poj3061 Subsequence ,尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- poj2566 尺取法
题意: 输入 n m 之后输入n个数 之后m个询问 对于每个询问 输入一个t 输出 三个数 ans l r 表示从l 到 r的所有数的和的绝对值最接近t 且输出这个和ans 思路: ...
- poj2739 poj2100 尺取法基础(二)
都是很简单的题目 poj2739素数打表+单点推移 #include<iostream> #include<cstring> #include<cstdio> us ...
- poj3061 Subsequence【尺取法】
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- 尺取法 poj3061 poj3320
尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- POJ - 1019 Number Sequence (思维)
https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 1234 ...
- Winform程序双向绑定
程序比较简单,一看就明白,主要需要实现INotifyPropertyChanged using System; using System.Collections.Generic; using Syst ...
- Http请求中 content-type 和 dataType 区别
contentType: 告诉服务器,我要发什么类型的数据 dataType:告诉服务器,我要想什么类型的数据,如果没有指定,那么会自动推断是返回 XML,还是JSON,还是script,还是Stri ...
- jaxp实现对xml文档的增,删,改,查操作(附源码)浅析
jaxp,属于javase中的一部分.是对xml进行解析的一个工具类: 既然说到这里,还是讲全一点,讲讲上面说到的xml的解析技术. xml的一个标记型文档. 在html的层级结构中,它会在内存中分配 ...
- edge box
先介绍一下matlab与c混合编程 主要步骤: 使用c语言编写函数 利用mexFunction()函数创建C与matlab接口 从Matlab中编译函数 # include <mex.h> ...
- Elastic Job入门(3) - 集成Springboot
引入pom文件 <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job ...
- 青云VPC网络配置
1 创建VPC网络 2 申请公网IP 3 回到VPC图形界面绑定公网ip 4 创建私有网络,并绑定私有网络 5 创建3台主机 6 新建防火墙,并绑定到VPC 7 配置VPC端口转发规则 8 添加防火墙 ...
- 【通信】JDK中的URLConnection参数详解
JDK中的URLConnection参数详解 来自:http://www.blogjava.net/supercrsky/articles/247449.html 针对JDK中的URLConnecti ...
- 北洋UAM-05LX(网口系列适用)ROS节点
参考创客智造ROS与激光雷达入门教程 说明: 介绍ROS如何接入Hokuyo网口的雷达及基本使用 测试雷达:UAM-05LX采用太网接口,如果型号是USB口的参考教程 ros wiki: http:/ ...
- 开发使用tomcat生产使用weblogic造成jar包冲突如何解决?
问题描述开发时新增了jar包,当将增量包部署到测试服务器后启动应用出现如下错误提示:java.lang.LinkageError: loader constraint violation: loade ...