EDU Two Pointers Method step 3

Правка en1, от SeniorBOTx, 2022-05-16 20:30:59

In step 3 , question number 1 : I write two codes very similar to each , As i am a newbie can you help me out to find what is difference in two code. I got ac in one submission and wrong answer in other.

Also if you don not know what difference between two code, give this blog a upvote so many people can see it.

AC : [submission:157416384] WA : [submission:157415655]

problem : [problem:307094A]

AC : ~~~~~

include <bits/stdc++.h>

// username: seniorBOTx (Abhishek Gupta [AKGEC,CSIT,2nd year])

using namespace std;

define int int64_t

define endl '\n'

define rep(i,a,n) for(int i = (a); i < (n); i++)

define rrep(i,a,n) for(int i = (a); i >= (n); i--)

define each(a,x) for(auto &a:x)

define watch(x) cerr << #x << " " << x << endl;

define all(x) x.begin(), x.end()

define rall(x) x.rbegin(),x.end()

define sz(x) (int)(x).size()

define F first

define S second

define pb push_back

void solve() { int n, p; cin >> n >> p; int sum{}, ans = 0 , res{}, index{}, prefix{};

vector<int> v(2 * n);
rep(i, 0, n) {
    cin >> v[i];
    v[i + n] = v[i];
    sum += v[i];
}

if (sum < p) {
    res = p / sum * n;
    p %= sum;
}
if (p != 0) {
    ans = INT_MAX;
    for (int r = 0, l = 0; r < 2 * n ; r++ ) {
       prefix += v[r];
       while (l < n and prefix >= p) {
         if (ans > r - l + 1) {
          ans = r - l + 1;
          index = l ;
         }
         prefix -= v[l];
         l++;
       }
    }
}
cout << index + 1 << " " << ans  + res << endl;

}

int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

solve();

} ~~~~~ WA : ~~~~~

include <bits/stdc++.h>

// username: seniorBOTx (Abhishek Gupta [AKGEC,CSIT,2nd year])

using namespace std;

define int int64_t

define endl '\n'

define rep(i,a,n) for(int i = (a); i < (n); i++)

define rrep(i,a,n) for(int i = (a); i >= (n); i--)

define each(a,x) for(auto &a:x)

define watch(x) cerr << #x << " " << x << endl;

define all(x) x.begin(), x.end()

define rall(x) x.rbegin(),x.end()

define sz(x) (int)(x).size()

define F first

define S second

define pb push_back

void solve() { int n, p; cin >> n >> p; int sum{}, ans = LLONG_MAX , res{}, index{}, prefix{};

vector<int> v(2 * n);
rep(i, 0, n) {
    cin >> v[i];
    v[i + n] = v[i];
    sum += v[i];
}

if (sum < p) {
    res = p / sum * n;
    p %= sum;
}

for (int r = 0, l = 0; r < 2 * n ; r++ ) {
    prefix += v[r];
    while (l < n and prefix >= p) {
       if (ans > r - l + 1) {
         ans = r - l + 1;
         index = l + 1;
       }
       prefix -= v[l]; l++;
    }
}

cout << index << " " << ans  + res << endl;

}

int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

solve();

} ~~~~~

[meanwhile this is my first time here]

Теги two pointers, array, education

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en7 Английский SeniorBOTx 2022-05-16 20:37:42 8
en6 Английский SeniorBOTx 2022-05-16 20:37:15 54
en5 Английский SeniorBOTx 2022-05-16 20:35:27 14
en4 Английский SeniorBOTx 2022-05-16 20:34:23 12
en3 Английский SeniorBOTx 2022-05-16 20:33:00 8
en2 Английский SeniorBOTx 2022-05-16 20:32:17 1287
en1 Английский SeniorBOTx 2022-05-16 20:30:59 2901 Initial revision (published)