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++)以后,尾指针 ...
随机推荐
- Python介绍以及安装
Python介绍以及安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 借用我的导师的一句话:当你看到这篇文章的时候,那么恭喜你,你已经是踏入了开发的大门!欢迎加入:高级运维工程师 ...
- 设计模式---对象创建模式之工厂方法模式(Factory Method)
前提:“对象创建”模式 通过“对象创建”模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式(表现最为突出) 工 ...
- JAVA记录-消息队列介绍
1.JMS概述 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消 ...
- win7屏蔽ctrl+alt+up/down快捷键/ (eclipse冲突)
win7屏蔽ctrl+alt+up/down快捷键/ Eclipse有个非常好用的快捷键(当然Eclipse好用的快捷键有N个)Ctrl+Alt+UP/DOWN,用于复制当前行的内容,用法很简单, ...
- scala基本语法和单词统计
scala 基本语法 1.声明变量 (1)val i = 1 使用val声明的变量值是不可变的,相当于java里final修饰的变量,推荐使用. (2)var i = "hello" ...
- EasyUI动态修改easyui-textbox验证信息
<tr> <td>编码:</td> <td><input type="text" id="code" na ...
- 二、主目录 Makefile 分析(2)
2.7 编译选项---config.mk 代码 163 164 行 # load other configuration include $(TOPDIR)/config.mk 此段就是包含顶层目录下 ...
- Synchronized和lock的区别和用法
一.synchronized和lock的用法区别 (1)synchronized(隐式锁):在需要同步的对象中加入此控制,synchronized可以加在方法上,也可以加在特定代码块中,括号中表示需要 ...
- html5移动端页面分辨率设置及相应字体大小设置的靠谱使用方式
对于html5移动端网页编写CSS网上有很多介绍的文章,但在实际使用过程中还是会纠结. 网上的资料太多,且大多都是技术介绍型,特别是针对android上,网上写的各种麻烦,各种复杂,各种不接地气儿.. ...
- Django学习手册 - 初识自定义分页
核心: <a href='http://127.0.0.1:8000/index-%s'>%s<a> 自定义分页 1.前端处理字符 后端的字符 return render(r ...