Uva 1451 - Average(数形结合)

题目链接 https://vjudge.net/problem/UVA-1451

紫书243页例题,采用数形结合的方法,用deque来维护一个单调结点的队列,即不包含上凸点的序列,每次都更新结果,记录最优解。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100050;

int n, l;
char st[maxn];
int s[maxn];
deque<int> que;

//a,b对应直线斜率大于c,d对应直线,返回值>0,小于则返回值<0,等于返回0 
int check(int a, int b, int c, int d) {
	return (c - d)*(s[a] - s[b]) - (a - b)*(s[c] - s[d]);
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &l);
		scanf("%s", 1 + st);
		que.clear();
		for (int i = 1; i <= n; ++i) {
			s[i] = s[i - 1] + st[i] - '0';
		}
		
		int ansl = 0, ansr = l;//ansl记录左端点的前一位
		 
		for (int i = l; i <= n; ++i) {
			//加入新结点j前,先把形成的上凸点删掉
			int j = i - l;
			while (que.size() > 1) {
				int t1 = que[que.size() - 1];//倒数第一个点
				int t2 = que[que.size() - 2];//倒数第二个点
				if (check(t2, t1, t2, j) > 0) que.pop_back();
				else break;
			}
			//加入j
			que.push_back(j);
			//把不可能是解的开头若干项去掉
			while (que.size() > 1) {
				if (check(que[0], i, que[1], i) <= 0) que.pop_front();
				else break;
			} 
			
			//更新结果 
			int res = check(que[0], i, ansl, ansr);
			if (res > 0 || 0 == res && i - que[0] < ansr - ansl) {
				ansl = que[0];
				ansr = i;
			}
		}
		
		printf("%d %d\n", 1 + ansl, ansr);
	}
	return 0;
}


posted @ 2018-01-07 22:54  不想吃WA的咸鱼  阅读(125)  评论(0编辑  收藏  举报