整体思路:

分解为子问题:在[l,r)区间里查找长度为l的子序列,要求序列和sum≥s,输出sum最小值以及该序列起始位置,代码如下:

int sum=0, flag=0, pos=0, temp=inf;
for(int i=1;i<=mid;++i) sum+=a[i];
for(int i=1;i<=n-mid;++i){if(i>1) sum=sum-a[i-1]+a[i-1+mid];//前缀和实现, 复杂度Onif(sum>=s&&sum<temp) {flag=1;temp=sum;pos=i;}
}
if(flag) cout<<temp<<' '<<pos<<endl;
else cout<<-1<<endl;

长度l属于[l, r),利用二分,每次进行子问题操作,记录每一个l对应的sum,pos,整体复杂度nlgn

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;int s, a[2005], k;
struct data
{int sum;int pos;int len;
}d[2005];void getAnswer(int a[], int n, int s)
{int l=1, r=n;while(l<r){int mid=(l+r)>>1;int sum=0, flag=0, pos=0, temp=inf;for(int i=1;i<=mid;++i) sum+=a[i];
for(int i=1;i<=n-mid;++i){if(i>1) sum=sum-a[i-1]+a[i-1+mid];if(sum>=s&&sum<temp) {flag=1;temp=sum;pos=i;}}if(flag) {r=mid;d[k].len=mid, d[k].pos=pos, d[k].sum=temp;k++;}else l=mid+1;}
}//3关键字排序,重要程度按序列长度、序列和、起始位置依次递减
bool cmp(data x, data y)
{if(x.len==y.len&&x.sum==y.sum) return x.pos<y.pos;if(x.len==y.len) return x.sum<y.sum;return x.len<y.len;
}int main()
{cin>>s;int n=1;for(int i=1;cin>>a[i]&&a[i]!=-1;++i) ++n;getAnswer(a, n, s);if(k==0) cout<<0<<endl;//没有符合条件的子序列else {sort(d,d+k,cmp);cout<<d[0].len<<endl;for(int i=d[0].pos;i<d[0].pos+d[0].len;++i)printf("%d%c",a[i]," \n"[i==n-1]);}return 0;
}
/*
90
1 2 3 4 5 9 10 50 40 23 90 -1
*/

此题运用二分思想,特此记录。

转载于:https://www.cnblogs.com/ChenyangXu/p/10724849.html

Minimum Size Subarray Sum (二分)相关推荐

  1. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. Minimum Size Subarray Sum 最短子数组之和

    题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...

  3. [JAVA]寻找满足和的最短子序列(Minimum Size Subarray Sum)

    题目来源:leetcode 题目描述: Given an array of n positive integers and a positive integer s, find the minimal ...

  4. 209. Minimum Size Subarray Sum 长度最小的子数组

    Title 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度.如果不存在符合条件的连续子数组,返回 0. **示例: ** 输入: ...

  5. leetcode 209. Minimum Size Subarray Sum | 209. 长度最小的子数组(Java)

    题目 https://leetcode.com/problems/minimum-size-subarray-sum/ 题解 双指针解法,左指针和右指针在合适的时候向右走,并维护一个sum 版本1 思 ...

  6. Leetcode209-Minimum Size Subarray Sum

    新的个人博客:Hu Haoyu's Blog,欢迎参观! Given an array of n positive integers and a positive integer s, find th ...

  7. LeetCode 325. Maximum Size Subarray Sum Equals k

    这一题开始以为是sliding window,后来发现,因为有负数,sum不具有单调性,没有办法用slidng window找出optimal solution. 如果考虑brute force的做法 ...

  8. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  9. LeetCode 523. Continuous Subarray Sum

    题目: Given a list of non-negative numbers and a target integer k, write a function to check if the ar ...

  10. LeetCode Subarray Sum Equals K

    原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...

最新文章

  1. 小程序之实现滚动加载
  2. 频谱分析:基于python画出时域频域波形
  3. linux下使用tc工具模拟网络延迟和丢包
  4. 吴恩达机器学习作业5.偏差和方差
  5. 四川地震,物联网地震预警系统立功了
  6. 神器--通过Workspaces来编辑本地文件
  7. Algorithm:数学建模大赛(CUMCM/NPMCM)之全国大学生数模竞赛简介 相关书籍、文章推荐等详细攻略
  8. 华为云 云耀云服务器初体验
  9. ISP pipeline之lens shading correction
  10. python读取文件r_python读入文件时加r的作用?
  11. 二叉树(树,森林,二叉树互转)
  12. android 地铁地图api,入门指南-地铁图 JS API | 高德地图API
  13. NUIST 9th 校赛 P4
  14. Java 多态实际操练--使用多态的特征创建不同宠物的玩耍方法
  15. C++万能头文件(bits/stdc++.h)
  16. Edgecam2016基础编程到车铣复合加工视频教程
  17. JAVA RedisTemplate实现(加锁/解锁) 解决高并发问题
  18. 【Unity-学习-014】EasyAR4.0稀疏空间地图 扫描场景功能
  19. 线上微课堂:信号与系统
  20. Error Pe007: unrecognize token

热门文章

  1. 使用前端node.js 提供的服务器live-server
  2. 《一切皆是映射:代码的本质》哈希算法 (Hash)
  3. RabbitMQ 消息队列
  4. 【Linux】CentOS下vim的配置
  5. 破解电信光猫RG2010-CA超级管理员账号
  6. TCP/IP协议栈的封装
  7. Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)
  8. redis的批量操作命令pipeline(PHP实现)
  9. WPF Invoke与BeginInvoke的区别
  10. Codeforces 474 D. Flowers