POJ3061 Subsequence 尺取法

链接:https://ac.nowcoder.com/acm/problem/107658
来源:牛客网

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.
输入描述:
The first line is the number of test cases. 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.
输出描述:
For each the case the program has to print the result on separate line of the output file.if no answer, print 0.
示例1
输入
复制

2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

输出
复制

2
3

尺取法的板子题
用两个指针(lef, rig)维护一段连续区间
sum维护当前区间的和,当sum<m时,我们就应该向右扩展区间
相反,当sum>=m时,我们就应该收缩左区间

#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <stdio.h>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
#endif

template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

int n, m, Q, K, a[MAXN];

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%d ", &Q);
	while(Q--) {
		scanf("%d %d ", &n, &m);
		for(int i=1; i<=n; i++) scanf("%d ", a+i);
		int lef = 1, rig = 0, ans = INF, sum = 0;
		while(lef <= n) {
			//不断扩展右端点
			while(rig+1<=n && sum<m) sum += a[++rig];
			if(sum >= m) 
				ans = min(ans, rig-lef+1);
			sum -= a[lef]; //收缩左端点
			lef ++;
		}
		//注意没有答案的时候输出0即可
		printf("%d\n", ans==INF ? 0 : ans);
	}

#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}


全部评论

相关推荐

整体时间线:2月末力扣从零开始。3月初刷题成瘾,中旬陆续开面开杀,被机试折磨,下旬纠结日常offer选择。4月入职淘天,从硬landing到上手业务快乐融入5月平静美好,顺利到我觉得直接转正是最佳选择,月底转暑期流程被hr直接挂,主管诱骗能转正,万幸蚂蚁暑期流程没拒掉,压哨发意向,手里也还有个腾讯offer兜底,毁约腾讯暑期到此结束。==============================一些感悟:永远保留后手,先拿了阿里国际日常,拿到网易伏羲offer之后才拒绝意向,中间难免要催hr尽量开在同一时间,后续等淘天oc的时候立马拒了网易意向。不会让手里超过2个offer,但是也不会在未确定的时候就拒掉到手的。在淘天的时候师兄主管都保证能转正别担心,甚至主管拉我进内部群一起团建,但是始终把腾讯offer抓在手里,也给了我撕破脸之后和主管谈判的底气。蚂蚁一面二面间隔一个半月,时不时反向保温一下面试官又没拒掉流程,真是我最明智的选择。==============================实习体验:研一在鹅厂AI&nbsp;Lab实习打杂纯快乐的,自己包装一下也是有产出的。遇到的所有人都很温和有礼貌,整体不卷年纪偏大,公司关怀好,不考虑城市的话应该会是第一选择。淘天业务组非常业务,技术不容易提升但是容易有产出,整体强度能承受分到的活也不多还挺核心的,师兄还是很nice的,往年转正待遇也挺好,小组整体年龄结构有中有小没老人,晋升空间不错。拒掉的offer里面,同花顺是做大模型部署加速的,给钱少太卷拒了;阿里国际是研究型实习生随便面的感觉面试官技术没有太懂;网易伏羲是llm+智能npc其实很有搞头,还是贪图大厂title拒了;腾讯这个最可惜,agent+游戏ai,而且在大部门实习过可以丝滑landing,腾讯招聘经常能看到校招社招广告,应该是团队扩张期,考虑到城市因素忍痛拒绝,释放一个hc给大家。==============================彩蛋:想看看牛u会做什么选择,感觉人生到了这个时间点,每个决策都会影响很大,已知和女友都是浙江人,她稳定杭州工作,计划后续杭州定居结婚。 #暑期实习# #腾讯# #阿里# #蚂蚁# #大模型# #淘天#
投递蚂蚁集团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务