Help for Shooting (Easy) problem

I directly took the first element of the score difference array. I am taking the number of shots of each player next to them and subtracting the number of shot everytime I move by one square from the first element score. Also, everytime I pass a shot I change the counter by two because the bullseye will now move away from the shot instead of towards but the code is failing some hidden test case. Can someone please help me?
include <bits/stdc++.h>
using namespace std;

int main() {
int T,N,M,scorea,scorek,counta,countk;
cin >> T;
while (T) {
scorea=0;
scorek=0;
countk=0;
counta=0;
cin >> N >> M;
vector A(M,0);
for (int i=0;i<M;i++) {
cin >> A[i];
}
for (int i=0;i<M;i++) {
if(A[i]==1||A[i]==3) {
counta-=1;
scorea+=i;
}
if(A[i]==2||A[i]==3) {
countk-=1;
scorek+=i;
}
}
vector B(M,0);
for (int i=0;i<M;i++) {
B[i]=abs(scorea-scorek);
if(A[i]==1||A[i]==3) {
counta+=2;
}
if (A[i]==2||A[i]==3) {
countk+=2;
}

		scorea+=counta;
		scorek+=countk;
        
    }
    for (int i=0;i<M;i++) {
        cout << B[i] << " ";
    }
	T--;
    cout << endl;
}

}

Issues Identified

  1. Score Calculation Logic: The score should be based on the positions of shots, but your handling of score adjustment during iteration might not be correct.

  2. Updating Counts: The way you’re updating counta and countk could lead to incorrect calculations, particularly how you’re incrementing or decrementing them based on shots.

  3. Score Difference Logic: You calculate the score difference for every shot, but you’re not correctly reflecting how the “bullseye” concept works.

Suggested Approach

  1. Initialize Scores Properly: Start from zero for both players and calculate the total score as you iterate through each shot.

  2. Iterate and Update Correctly: For each shot, check its type and update the respective score accordingly. Also, adjust the counts as you pass each shot.

  3. Maintain Score Differences: Store the difference in scores correctly as you progress.

Here’s a corrected version of your code with explanations inline:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int N, M;
        cin >> N >> M;
        vector<int> A(M);
        for (int i = 0; i < M; i++) {
            cin >> A[i];
        }

        long long scoreA = 0, scoreK = 0;
        vector<long long> B(M);

        for (int i = 0; i < M; i++) {
            // Calculate the scores based on the shot type
            if (A[i] == 1 || A[i] == 3) {
                scoreA += i;
            }
            if (A[i] == 2 || A[i] == 3) {
                scoreK += i;
            }

            // Store the current score difference
            B[i] = abs(scoreA - scoreK);
        }

        for (int i = 0; i < M; i++) {
            cout << B[i] << " ";
        }
        cout << endl;
    }
    return 0;
}

Key Changes

  1. Removed Count Variables: You don’t need separate counters (counta, countk) for the logic you’re implementing.

  2. Directly Updated Scores: Directly update scoreA and scoreK without intermediary count adjustments, as they should reflect the shot scores directly.

  3. Used Long Long for Scores: It’s safer to use long long for scores in case of large inputs, although it might not be strictly necessary here.

Explanation of the Logic

  • For each shot type (1, 2, or 3), you increment the respective player’s score by the index of the shot.
  • The absolute difference in scores is calculated and stored for each shot.
  • At the end of processing all shots, the score differences are printed.

This should address the hidden test cases you were encountering. Make sure to test it with various scenarios to ensure correctness!

1 Like