AlGoreRhythm's blog

By AlGoreRhythm, history, 8 months ago, In English

1848A - Vika and Her Friends

Hello CF! Could you explain to me why these two codes output different results?

Problem 1848A: Vika and Her Friends

My Code:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

void solve() {
    ll n, m, k; //Size of mall and # of Vika's friends
    ll x, y; //Vika's coordinate
    cin >> n >> m >> k >> x >> y;

    //For each of vika's friends, check whether they are in the same square as her. If so, print NO
    for(int i = 0; i < k; i++) {
        int xx, yy;
        cin >> xx;
        cin >> yy;
//!!! PROBLEM SEEMS TO BE HERE vvv!!!
        if((x + y) % 2 == (xx + yy) % 2) { 
            cout << "NO" << endl;
            return;
        }
    }
    cout << "YES" << endl;
//!!! PROBLEM SEEMS TO BE HERE ^^^!!!
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
}

Editorial's code:

#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
int32_t main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m, k;
        cin >> n >> m >> k;
        int x, y;
        cin >> x >> y;
        string ans = "YES\n";
        for (int i = 0; i < k; ++i) {
            int xx, yy;
            cin >> xx >> yy;
            if ((x + y) % 2 == (xx + yy) % 2) {
                ans = "NO\n";
            }
        }
        cout << ans;
    }
    return 0;
}

The problem seems to be inside the for-loop. I used a different method to output a "NO" when it's found that one of Vika's friend is on the same square colour as Vika. This method seems to yield an incorrect result when there's a previous test case ('t') that outputs a 'NO' already (although I could be wrong).

I asked ChatGPT several times with different prompts on why these two code output different results, but I'm still quite confused. Out of options, I have to resort to asking you guys. Would be really nice if you guys could help a little newbie like me. Thanks!

EDIT: MISTAKE FOUND by Hazzler. Thank you for your time!

By using the return function, I didn't finish reading all the input from testcases that yields a NO before moving on to the next testcase. The result is the rest of the input from this testcase will be read as an input for the next testcase, leading to an incorrect result.

In hindsight, this mistake is one of the 200 IQ moments of all time.

Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By AlGoreRhythm, history, 9 months ago, In English

March 31, 2024

Rating: 400

Problems Solved: 16

Intro:

I will be documenting my progress on CodeForces here. Planning an intense problem-solving training session with ambitious goals for April and see how much I improve.

Progress:

15 March: Registered for CodeForces

18 March: Coded my first C++ code (Hello World!)

26 March: Solved first 800-rated CF problem (4A Watermelon) (Old problem)

27 March: Solved first 800-rated CF problem (1948A Special Characters) (New problem)

28 March: First CF Competition (Round 937 Div. 4) (+401 rating)

400 ratings… low starting point and humble beginnings. Good for showing progress, as there’s a lot of room for quick improvements. Should see a lot of progress in the very near future!

Note: Older problems seems to be easier than newer ones. I will be solving and using newer problems as a judgement for my skill level from now on.

Method:

Solve on average 3 problems a day

1 Competition per week + Upsolve after contest

4 hours of coding per day

Previous 2 weeks has been easy-going and comfortable, mainly because I want to develop habits and enjoyment for coding first before ramping things up. It’s not good to overextend myself early and burn out the early flames. Recently, however, I find myself struggling to sleep because I just want to solve more problems late at night. I think I’m ready now. Let's ramp things up!

Goal:

Pupil by 1 May (that’s right!)

Game on!

Full text and comments »

  • Vote: I like it
  • -6
  • Vote: I do not like it