UVA之1121 - Subsequence
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/SunnyYoona/article/details/25840365
【题目】
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S <
100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.
Input
Many test cases will be given. For each test case the program has to read the numbers N and S, separated by an interval, from the first line.
The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.
Output
For each the case the program has to print the result on separate line of the output file. If there isn't such a subsequence, print 0 on a line by itself.
Sample Input
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3
【分析】
本题最直接的思路是二重循环,枚举子序列的起点和终点。
代码例如以下(输入数据已存入数组A[1]~A[n])。
int ans = n+1;
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++) {
int sum = 0;
for(int k = i; k <= j; k++) sum += A[k];
if(sum >= S) ans = min(ans, j-i+1);
}
printf("%d\n", ans == n+1 ? 0 : ans);
非常可惜,上述程序的时间复杂度是O(n3)的,因此,当n达到100 000的规模后,程序将无能为力。
有一个方法能够减少时间复杂度。即常见的前缀和技巧。
令Bi=A1+A2+…+Ai,规定B0=0,则能够在O(1)时间内求出子序列的值:Ai+Ai+1+…+Aj=Bj-Bi-1。
这样。时间复杂度降为O(n2),代码例如以下。
B[0] = 0;
for(int i = 1; i <= n; i++) B[i] = B[i-1] + A[i];
int ans = n+1;
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++)
if(B[j] - B[i-1] >= S) ans = min(ans, j-i+1);
printf("%d\n", ans == n+1 ? 0 : ans);
遗憾的是,本题的数据规模太大。O(n2)时间复杂度的算法也太慢。
不难发现,仅仅要同一时候枚举起点和终点,时间复杂度不可能比O(n2)更低,所以必须另谋他路。比方,能否够不枚举终点。仅仅枚举起点。或者不枚举起点,仅仅枚举终点呢?
我们首先试试仅仅枚举终点。对于终点j,我们的目标是要找到一个让Bj-Bi-1≥S。且i尽量大(i越大。序列长度j-i+1就越小)的i值。也就是找一个让Bi-1≤Bj-S最大的i。考虑图1-29所看到的的序列。
当j=5时。B5=12,因此目标是找一个Bi-1≤12-7=5的最大i。注意到B是递增的(别忘了,本题中全部Ai均为整数),所以能够用二分查找。
【代码】
/*********************************
* 日期:2014-5-14
* 作者:SJF0115
* 题号: 1121 - Subsequence
* 地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=246&page=show_problem&problem=3562
* 来源:UVA
* 结果:Accepted
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define N 100001
int A[N];
int B[N];
//二分查找最接近target但不大于target
int BinarySerach(int target,int R){
int L = 0;
int mid = 0;
while(L < R){
mid = L + (R - L) / 2;
if(B[mid] > target){
R = mid;
}
else{
L = mid + 1;
}
}
return L;
}
int main(){
int n,s,i,j;
//freopen("C:\\Users\\wt\\Desktop\\acm.txt","r",stdin);
while(scanf("%d %d",&n,&s) != EOF){
int minLen = n+1;
B[0] = 0;
for(i = 1;i <= n;i++){
scanf("%d",&A[i]);
//序列前缀和
B[i] = B[i-1] + A[i];
}
for(j = 1;j <= n;j++){
int target = B[j] - s;
//二分查找
int index = BinarySerach(target,j-1);
if(index > 0){
minLen = min(minLen,j-index+1);
}
}
//没有满足条件的序列
if(minLen == n+1){
minLen = 0;
}
cout<<minLen<<endl;
}//while
return 0;
}
【类似题目】
UVA之1121 - Subsequence的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- uva 1121 Subsequence
https://vjudge.net/problem/UVA-1121 题意: 给出一个正整数数列a,要求找出最短的连续的一个序列使得这个序列的所有数字之和大于等于S. 思路: 第一是由于序列都是正整 ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- 【UVa】Palindromic Subsequence(dp+字典序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=465&page=s ...
- UVa 10747 - Maximum Subsequence
题目大意:给出n个数,从中选取k个,使得乘积最大,并且尽量使和最大 分析:首先按照数的绝对值大小排序.然后就要分三大类情况讨论: (1)前k个中选到0:如果选到0的话,乘积一定是0,所以尽量选大的数, ...
- UVa 11404 Palindromic Subsequence (LCS)
题意:给定一个字符串,问删除一些字符,使得它成为一个最长回访串,如果有多个,输出字典序最小的那个. 析: 我们可以把原字符串反转,然后求两个串的LCS,就得到最长回文串,不过要注意一些细节. 代码如下 ...
- uva 11404 dp
UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...
- ACM 杂题,思路题 整理
UVa 11572 - Unique Snowflakes 问一个数组中,无重复数字的最长子串长度是多少. 用map维护某数字上次出现的位置.另外用变量last表示上次出现数字重复的位置. 如果出现重 ...
随机推荐
- uva1391 2-SAT 问题
题意在大白书上. 有3 种工作 abc 大于等于平均年龄的可以去做a c 工作, 小于平均年龄的可以去做 bc , 同样转化为2 -sat 去做, 因为对于每个人也只有2 种情况可以作为选择 #inc ...
- EF Code First 学习笔记:表映射(转)
多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...
- Vue学习笔记之Babel介绍
这个是解析我们es6的代码的,为什么要用它呢,因为对于一些ie浏览器,甚至FF浏览器,低版本的还不能识别我们的es6代码,那么vue里面好多还让我们去写es6的代码,这个时候我们就可以用babel这个 ...
- 20162314 Experiment 3 - Sorting and Searching
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...
- Cooperation.GTST团队第三周项目总结
项目进展 这周我们仍然在学习使用博客园的相关接口,页面的一个基本模块已经搭建出来了,但是页面整体效果还没有完全做出来.另外,我们在使用其他的APP时留意到许多APP都使用上拉加载和下拉刷新的效果,所以 ...
- pix2pix-tensorflow搭建及其使用
目录 pix2pix-tensorflow搭建过程 1. 环境搭建 2. 环境说明 3. 开始搭建 4. 训练结果说明 5. 数据集 5.1 图片格式说明 5.3 从先用图片创建图像对 5.4 如何进 ...
- 偶数求1/2+1/4+...+1/n奇数1/1+1/3+...+1/n
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n Scanner scanner = new Scanner(Sy ...
- bzoj1058: [ZJOI2007]报表统计 stl xjbg
小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个可能为负数的整数数列,并且 ...
- 2-10~2-11 配置iptables防火墙增强服务 selinux简单讲解
学习一个服务的过程: 1.此服务器的概述:名字,功能,特点,端口号 2.安装 3.配置文件的位置 4.服务启动关闭脚本,查看端口 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上, ...
- HDU 2553 状态压缩
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...