POJ3061 Subsequence(二进制前缀和法律+仿真足)
二分法+前缀和法律
满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和。前缀和可在O(n)的时间内统计
sum[i]的值。再用二分找出满足条件的最小的子序列长度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define ll __int64
#define INF 0x3fffffff
using namespace std; int sum[100005];
int a[100005];
int n,s; bool C(int x)
{
bool flag=false;
for(int i=0;i<n-x;i++)
{
if(sum[x+i]-sum[i]>=s)
{
flag=true;
break;
}
}
if(flag) return true;
else return false;
} int solve()
{
int l=0,r=n+1;
while(r-l>1)
{
int mid=(l+r)/2;
if(C(mid)) r=mid;
else l=mid;
}
return r;
} int main()
{
int T;
//freopen("d:\\Test.txt","r",stdin);
cin>>T;
while(T--)
{
cin>>n>>s;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum[0]=a[0];
for(int i=1;i<n;i++)
{
sum[i]=sum[i-1]+a[i];
}
if(solve()==n+1) cout<<"0"<<endl;
else cout<<solve()<<endl;
}
return 0;
}
尺取法
(1) 设置两个指针s和t。一開始都指向数列第一个元素。此外sum=0,res=0。
(2) 仅仅要sum<S。就将sum添加一个元素,t加1;
(3) 直到sum>=S,更新res=min(res,t-s);
(4) 将sum减去一个元素。s加1,运行(2)。
上述流程重复地推进区间的开头和末尾,来求取满足条件的最小区间。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int a[100005]; void solve()
{
int res=n+1;
int s=0,t=0,sum=0;
while(true)
{
while(t<n&&sum<m)
{
sum+=a[t++];
}
if(sum<m) break;
res=min(res,t-s);
sum-=a[s++];
}
if(res>n) res=0;
cout<<res<<endl;
} int main()
{
int T;
//freopen("d:\\Test.txt","r",stdin);
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
solve();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ3061 Subsequence(二进制前缀和法律+仿真足)的更多相关文章
- Leetcode 1029. 可被 5 整除的二进制前缀
1029. 可被 5 整除的二进制前缀 显示英文描述 我的提交返回竞赛 用户通过次数467 用户尝试次数662 通过次数477 提交次数1964 题目难度Easy 给定由若干 0 和 1 组成的 ...
- [POJ3061]Subsequence(二分,前缀和)
题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...
- [ACM] POJ 3061 Subsequence (仿真足)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8403 Accepted: 3264 Descr ...
- Poj 3061 Subsequence(二分+前缀和)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ3061 Subsequence
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16520 Accepted: 7008 Desc ...
- poj3061 Subsequence(尺取法)
https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...
- poj3061 Subsequence(尺取)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- POJ3050 Hopscotch 【DFS】
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2113 Accepted: 1514 Descrip ...
- •当对自己的技术足够自信的时候,拼的就是RP和面试技巧了(转)
前言 三月真是一个忙碌的季节,刚刚开学就需要准备各种面试和笔试(鄙视).幸运的是,在长达一个月的面试内推季之后,终于同时拿到了阿里和腾讯的offer,还是挺开心的.突而想起久未更新的博客,就冒昧学一学 ...
- mv目录前后要加斜杠,否则会当成文件
mv目录要加斜杠,否则会当成文件
- 【LaTeX排版】LaTeX论文排版<三>
A picture is worth a thousand words(一图胜千言).图在论文中的重要性不言而喻,本文主要解说图的制作与插入. 1.图像的插入 图像能够分为两大类:位图和向量图 ...
- android 自己定义开关(SwitchButton)
近期心血来潮,写了一个自己定义仿iPhone的开关. 有须要的同学能够来下载啦.支持点击自己主动滚动,速率能够自己依据须要改动.触摸滚动,大小自己定义,支持改动样式.就不录制动画,就上传了两张图给大家 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- tarjan算法(割点/割边/点连通分量/边连通分量/强连通分量)
tarjan算法是在dfs生成一颗dfs树的时候按照访问顺序的先后,为每个结点分配一个时间戳,然后再用low[u]表示结点能访问到的最小时间戳 以上的各种应用都是在此拓展而来的. 割点:如果一个图去掉 ...
- Windows Phone开发(21):做一个简单的绘图板
原文:Windows Phone开发(21):做一个简单的绘图板 其实我们今天要说的就是一个控件--InkPresenter,这个控件并不是十分强大,没办法和WPF中的InkCanvas相比,估计在实 ...
- Java EE (6) -- Java EE 5 Enterprise Architect Certified Master
Section 1: Application Design Concepts and Principles Explain the main advantages of an object-orien ...
- android升级软件版本号,您安装后的新版本号,成功安装画面没有出现,或直接回到桌面
Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //an ...