二分 或者 指针



Problem Description

One day, ZHJ came across a warrior novel named Kunlun in library. Soon, ZHJ fall into it.
He tells everybody how deeply he loves Kunlun no matter whom he meets.
He found it attractive so he decided to read the novel in the next N days. However, ZHJ had
his own schedule. So every day he had only few minutes to spend on the book. Besides, he was
accustomed to reading a novel continuously, that is, once he started to read the novel, he would
not stop until he had read the entire novel.
ZHJ wondered when he should begin to read the novel so that he could spend the least days
on the novel.

Input

The input contains several cases.
The first line of each case contains two space-seperated integers N(2 <= N <= 10^5) and S(1 <= S <= 10^8) where N means ZHJ decides to finish the novel in the next N days, S means the novel would cost ZHJ S minutes.Then N lines follows.The ith line contains an integer ai(1 <= ai <= 100) which means ZHJ has ai minutes' free time in the ith day.It is guaranteed that the sum of the free time of the N days is enough for ZHJ to finish the novel.

Output

For each test case, you should output a single line with a integer indicating the day ZHJ should start to read the novel.If there are several answers, output the smallest one.

Sample Input

2 100
99
100
20 100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Sample Output

2
15



二分
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int dp[100005];
int main()
{
	int n,s;
	int i,j;
	while(cin>>n>>s)
	{
		dp[0]=0;
		for(i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			dp[i]=dp[i-1]+x;
		}
		int min=1000001;
		int k;
		for(i=1;i<=n;i++)
		{
			if(dp[n]-dp[i-1]<s)
				break;
			else
			{
				int l=i,r=n;
				int mid=(l+r)/2;
				while(r-l>4)
				{
					if(dp[mid]-dp[i-1]==s)
						break;
					if(dp[mid]-dp[i-1]<s)
					{
						l=mid;
						mid=(l+r)/2;
					}
					else
					{
						r=mid;
						mid=(l+r)/2;
					}
						
				}
				for(j=l;j<=r;j++)
					if(dp[j]-dp[i-1]>=s)
					{
						mid=j;
						break;
					}
				if(mid-i<min)
				{
					min=mid-i;
					k=i;
				}
			}
		}
		cout<<k<<endl;
	}
	return 0;
}
双边指针移动
#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
	int num[100006];
	int i,j;
	int n,s;
	while(cin>>n>>s)
	{
		for(i=1;i<=n;i++)
			scanf("%d",&num[i]);
		int a=1,b=0;
		int sum=0;
		int min=100001;
		int k;
		while(b<=n&&a<=n)
		{
			while(sum<s&&b<n)
			{
				b++;
				sum+=num[b];
			}
			if(b-a+1<min&&sum>=s)
			{
				min=b-a+1;
				k=a;
			}
			sum-=num[a];
			a++;
		}
		cout<<k<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值