Блог пользователя alirezaopmc

Автор alirezaopmc, история, 5 лет назад, По-английски

Hello force coders, I was just solving a problem (1324A - Yet Another Tetris Problem) and my code was this:

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

#define ll long long

#define li(n) for(int i=0; i<n; i++)

void test();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t = 1;
	cin >> t;
	while(t--) {
		test();
		cout << endl;
	}
}

void test() {
	int n;
	cin >> n;
	int pre, cur;
	cin >> pre;
	li(n-1) {
		cin >> cur;
		pre%=2;
		cur%=2;
		if(pre==cur) continue;
		else {
			cout << "NO";
			return;
		}
	}
	cout << "YES";
	return;
}

and the tried to check my solution by given test cases, but the operation of taking input was amazingly wonderful. Input:

4
3
1 1 3
4
1 1 2 1
2
11 11
1
100

My Code's Output: (also containing inputs)

4
3
1 1 3
YES
4
1 1 2 1
NO
2 // how?
YES
11 11
1
100
NO

I was wondered why would something like this happen in C++. Likewise I had same problem on some interactive problems before. That Was (1305D - Kuroni and the Celebration) = (72397838)

Any idea?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Your program doesn't always take the entire input of each test case. If you output NO before reading all N-1 numbers, then the next time you call test() your program will start inside the middle of the previous test case.

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
	li(n-1) {
		cin >> cur;
		pre%=2;
		cur%=2;
		if(pre==cur) continue;
		else {
			cout << "NO";
			return;
		}
	}

Here you should not return when you are not finishing taking all the variables from the test case