Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Need help in a problem of two pointers

Правка en3, от bovin, 2024-03-30 17:32:56

Problem

Can someone help me, I don't understand where the error is in the code and reasoning. I'm getting the wrong answer, I'm getting extra segments in the answer

My code:

~~~~~

include

include

using namespace std;

int f(int n, long long s, vector& a) { int ans = 0; int i = 0; long long sum = 0; for(int j = 0; j < n; j++) { // move the right pointer here sum += a[j]; // update the amount while(sum > s) { // as long as the sum on the segment [i, j] //is greater than or equal to s, move the left pointer
sum -= a[i]; // update the amount i++; // move the right pointer } // since sum < s -> i is not included in the segment => segment length = j — i ans += (j — i); } return ans; }

int main() { int n; long long s; cin >> n >> s; vector a(n); for(int i = 0; i < n; i++) cin >> a[i]; cout << f(n, s, a); return 0; }~~~~~

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский bovin 2024-03-30 17:35:28 1 Tiny change: 'hile(sum > s) { // a' -> 'hile(sum >= s) { // a'
en5 Английский bovin 2024-03-30 17:34:28 0 (published)
en4 Английский bovin 2024-03-30 17:33:27 4
en3 Английский bovin 2024-03-30 17:32:56 2 Tiny change: 'n\n~~~~~\n#include' -> 'n\n~~~~~\n\n#include' (saved to drafts)
en2 Английский bovin 2024-03-30 17:32:29 1077 (published)
en1 Английский bovin 2024-03-30 17:28:48 128 Initial revision (saved to drafts)