Gwazy's blog

By Gwazy, history, 4 years ago, In English

29A - Spit Problem

#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;
 
int main() {
    IOS
 
    int n;
    cin >> n;
    int x[n], d[n];
    x[0] = 0;
    d[0] = 0;
 
    for(int i = 0; i < n; i++){ 
        scanf("%d", &x[i]);
        scanf("%d", &d[i]);
    }
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if ((x[i] + d[i]) == x[j]) {
                if(x[j] + d[j] == x[i]) {
                    cout << "YES";
                    return 0;
                }
            }
        }
    }
    cout << "NO";
    return 0; 
}

Hey Code Forcers, I'm running this code on my terminal, and I'm getting a correct output for all tests.

However, when I submit the code to CodeForces, I fail test case two.

Initially, I was getting an uninitialized value usage before I initialised arrays x and d, could it be related?

I would appreciate any help,

Thanks

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

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by Gwazy (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it +9 Vote: I do not like it

Don't mix cin and scanf, if you use sync_with_stdio(false). Either stick to one of these, or don't turn off sync_with_stdio.

»
4 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

You cannot mix scanf and cinonce you use ios_base:: sync_with_studio(false) in your code. That is the thing which is giving you the wrong answer. I submitted the same code removing the IOS, and you can have a look at it 86608563. For more information you can also look at this blog.
Updated: Thanks for the clarification dmitry.dolgopolov

  • »
    »
    4 years ago, # ^ |
    Rev. 4   Vote: I like it +9 Vote: I do not like it

    It seems you misunderstand what sync_with_stdio(false) does. One can use scanf/printf with that but should not mix them with cin/cout. If you do not believe me, just replace cin >> n; with scanf("%d", &n); and check.

    This might clarify things related to mixed C-style and C++-style IO.

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Oh, I see!! Thanks guys!