谁能告诉我这道题怎么优化
查看原帖
谁能告诉我这道题怎么优化
846041
__xxy_free_ioi__楼主2024/1/13 14:42

64分 3、4、5号点TLE

前缀和用了,单调队列也用了,我真不知道怎么优化了。。。

#include <iostream>
#include <queue>
using namespace std;
int n, m, ans;
int a[500001];
deque<int> q;
void push(int x) {
	while(!q.empty() && x >= q.back()) q.pop_back();
	q.push_back(x);
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] += a[i - 1];
	}
	for(int i = 1; i <= m; i++) {
		for(int j = i; j <= n; j++) {
			push(a[j] - a[j - i]);
		}
	}
	cout << q.front();
	return 0;
}
2024/1/13 14:42
加载中...