poj2566 (尺取法)

链接:https://cn.vjudge.net/problem/POJ-2566

#include
#include
#include
#include
#include
using namespace std;
typedef sz 100005;

pair p[sz];
int n,k,q;

void solve(int t){
	int temp = 0,sum = 1e9+1,i = 0,j = 1,l,r;
	while(j <= n&&sum){
		temp = p[j].first-p[i].first;
		if(abs(temp-t) < ans ){
			ans = abs(temp-t)
		}
	}
}

int main(){
	int x,sum;
	while(cin>>n>>k){
		if(n == 0&&k == 0)
		break;
		p[0] = make_pair(0,0);
		sum = 0;
		for(int i = 1;i<=n;++i){
			cin>>x;
			sum += x;
			p[i] = make_pair(sum,i);
		}
		sort(p,p+1+n);
		while(k--){
			cin>>x;
			solve(x);
		}
	}
}

 

你可能感兴趣的:(ACM,小技巧)