1044火星での買い物(デュアルポインター方式)

#include<cstdio>
#include<vector>
using namespace std;
const int inf = 1000000000;
struct shape{
	int left,right;
};
int chain[100010];
int main()
{
	int n,m,i,j;
	vector<shape> v;
	scanf("%d%d",&n,&m);
	for(i=0; i<n; i++){
		scanf("%d",&chain[i]);
	}
	i=0,j=0;
	int now=chain[i],min=inf;
	while(i<n&&j<n){
		bool flag = true;
		while(now<m){
			if(j<n-1) j++;
			else{
				flag = false;
				goto OUT;
			}
			now+=chain[j];
		}
		OUT:
			if(flag ==false ) break;
		if(now<min){
			v.clear();
			min = now;
			shape tmp;
			tmp.left = i+1;
			tmp.right = j+1;
			v.push_back(tmp);
		}
		else if(now==min){
			shape tmp;
			tmp.left = i+1;
			tmp.right = j+1;
			v.push_back(tmp);
		}
		now-=chain[i];
		i++;
	//	printf("i=%d\n",i);
	}
	for(int i=0; i<v.size(); i++){
		printf("%d-%d\n",v[i].left,v[i].right);
	}
	return 0;
}







 

おすすめ

転載: blog.csdn.net/yiwaite/article/details/112808496