#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 1e5 + 7;
char input[maxn];
int sum[maxn],p[maxn], n, l;
int GetSlope(int x1, int x2, int x3, int x4) {
return (sum[x2] - sum[x1 - 1]) * (x4 - x3 + 1) - (sum[x4] - sum[x3 - 1]) * (x2 - x1 + 1);
}
int main()
{
int t;
cin >> t;
while (t--) {
cin >> n >> l;
cin.get();
cin >> input + 1;
sum[0] = 0;
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + (input[i] - '0');
}
int ansL = 1, ansR = l, nLeft = 0, nRight = 0,last = 0;
for (int i = l; i <= n; i++) {
//去掉上凸点
while (nRight - nLeft > 1 && GetSlope(p[nRight - 2], last,p[nRight - 1], last) >= 0) nRight--;
p[nRight++] = last = i - l + 1;
//处理下凹点
while (nRight - nLeft > 1 && GetSlope(p[nLeft], i, p[nLeft + 1], i ) <= 0) nLeft++;
int nSlope = GetSlope(p[nLeft],i,ansL,ansR);
if (nSlope > 0 || nSlope == 0 && i - p[nLeft] < ansR - ansL) {
ansL = p[nLeft];
ansR = i;
}
}
cout << ansL << " " << ansR << endl;
}
return 0;
}
例题8-9(uva-1451)
最新推荐文章于 2021-05-19 15:16:21 发布
本文介绍了一个用于处理特定字符序列的算法实现,通过计算斜率来寻找最优子序列,并使用多种C++标准库进行数据结构操作。该算法适用于需要在长序列中找出具有特定属性的连续子串的问题。
459

被折叠的 条评论
为什么被折叠?



