Ejemplo n.º 1
0
int main() {
	int i = 0;
	for (;i < n; i++) {
		accumulated_sum(i);
	}
	return accumulated_sum(0);
}
 int shortestSubarray(vector<int>& A, int K) {
     vector<int> accumulated_sum(A.size() + 1, 0);
     partial_sum(A.cbegin(), A.cend(), next(accumulated_sum.begin()), plus<int>());
     
     int result = numeric_limits<int>::max();
     deque<int> mono_increasing_q;
     for (int i = 0; i < accumulated_sum.size(); ++i) {
         while (!mono_increasing_q.empty() &&
                accumulated_sum[i] <= accumulated_sum[mono_increasing_q.back()]) {
             mono_increasing_q.pop_back();
         }
         while (!mono_increasing_q.empty() &&
                accumulated_sum[i] - accumulated_sum[mono_increasing_q.front()] >= K) {
             result = min(result, i - mono_increasing_q.front());
             mono_increasing_q.pop_front();
         }
         mono_increasing_q.emplace_back(i);
     }
     return result != numeric_limits<int>::max() ? result : -1;
 }