POJ 3061 (二分找值)
来源:互联网 发布:动态桌面主题下载软件 编辑:程序博客网 时间:2024/05/20 18:18
一个由N个正整数组成的序列 // 10 < N < 10^5
其中每个数小于或等于10000给定一个正整数 S // S < 10^9
找到一个长度最小的连续子序列,使各个元素之和不小于S
输出此子序列长度
#include <stdio.h>#include <algorithm>#include <iostream>using namespace std;int integer[100010];int num_integer;int sum;bool check(int length){ int temp_sum = 0; for (int i = 0; i < length; ++i) temp_sum += integer[i]; if(temp_sum>=sum) return true; for (int tail = length,head = 0; tail < num_integer; ++tail, ++head) { temp_sum -= integer[head]; temp_sum += integer[tail]; if(temp_sum>=sum) return true; } return false;}int solve(){ int l = 0; int r = num_integer; while(r - l > 1) // find in (l, num_integer] { int mid = r - (r-l)/2; if(check(mid)) r = mid; else l = mid; } return r;}int main(){ int num_case; cin>>num_case; while(num_case--) { scanf("%d %d", &num_integer, &sum); int temp_sum = 0; for (int i = 0; i < num_integer; ++i) { scanf("%d", &integer[i]); temp_sum += integer[i]; } if(temp_sum<sum) printf("0\n"); else printf("%d\n", solve()); }}
0 0
- POJ 3061 (二分找值)
- POJ 2456 (二分找值)
- POJ 2010 二分找中位数
- 【POJ】3061---Subsequence(二分)
- 二分查找找下标或者值
- POJ 1068(找规律)
- 二分|hash(找正方形)poj2002
- The Meaningless Game (二分 找答案)
- POJ 3061 Subsequence(二分/尺取法)
- POJ 3061 简单二分
- poj 3061 Subsequence 二分
- Subsequence (二分)【POJ】-3061
- POJ 2010(二分最大化中间值)
- poj 3273 (二分)
- poj 3122(二分)
- poj 1905(二分)
- POJ 3111(二分)
- POJ 1064(二分)
- Mac 安装配置启动Tomcat
- Android第六讲——数据存储(五种)SharedPreferences、内部存储(缓存cache)、外部存储(Sdcrad)、SQLite数据库、网络存储
- 从零开始
- 谈电子政务云
- Wpf 实现登陆窗口
- POJ 3061 (二分找值)
- linux常用命令(22):find 命令的参数详解
- Rtaudio在VS下的使用
- php 相对路径中 ./ 与../的区别
- 腾讯面试
- C# 画图控件CHART 自定义坐标轴刻度标签
- 2014年7月华为校招机试真题2
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- UC/OS II事件管理(1)