1 / 4
Jun 2023
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n1; cin >> n1;
    while(n1!=0) {
        vector<int> v1(n1);
        for(int i=0; i<n1; i++) cin >> v1[i];
        int n2; cin >> n2;
        vector<int> v2(n2);
        for(int i=0; i<n2; i++) cin >> v2[i];
        
        vector<int> intersectionPointsV1, intersectionPointsV2;
        for(int i=0; i<n1; i++) {
            bool present = binary_search(v2.begin(), v2.end(), v1[i]);
            if(present) intersectionPointsV1.push_back(i);
        }
        
        for(int i=0; i<n2; i++) {
            bool present = binary_search(v1.begin(), v1.end(), v2[i]);
            if(present) intersectionPointsV2.push_back(i);
        }
        
        intersectionPointsV1.push_back(-1);
        intersectionPointsV2.push_back(-1);
        intersectionPointsV1.push_back(n1);
        intersectionPointsV2.push_back(n2);
        sort(intersectionPointsV1.begin(), intersectionPointsV1.end());
        sort(intersectionPointsV2.begin(), intersectionPointsV2.end());
        
        long long answer = 0;
        for(int i=0; i<intersectionPointsV1.size()-1; i++) {
            int start1 = intersectionPointsV1[i]+1, end1 = intersectionPointsV1[i+1];
            int start2 = intersectionPointsV2[i]+1, end2 = intersectionPointsV2[i+1];
                
            long long sum1 = 0, sum2 = 0;
            for(int j=start1; j<end1; j++) sum1 += v1[j];
            for(int j=start2; j<end2; j++) sum2 += v2[j];
                
            answer += max(sum1, sum2);
            if(end1<n1) answer += v1[end1];
        }
        cout << answer << endl;
        cin >> n1; 
    }
}

This is the code I have used. I wrote another code that does not store the intersection points and that got accepted. Logically they do the exact same thing. Where am I going wrong with this code?The double helix14

  • created

    Jun '23
  • last reply

    Jun '23
  • 3

    replies

  • 479

    views

  • 3

    users

  • 1

    link

I can’t find a test case where this code gives a different answer to my AC code.

According to one of the comments “some of the test cases are not strictly increasing” (which suggest inconsistency between a problem statement and test cases). While the solution above does rely on this assumption for correctness, for example:

3 -2 -1 -1
3 -2 -1 5
0

Oh, I see. Thanks a lot for clarifying. This has been bugging me for many days now.