TLE IN BUS

https://www.codechef.com/viewsolution/49973831

Guys why this code is giving a TLE verdict

probably because you are not taking the entire input first. What you’re doing is taking each query and checking it at the same time, so check correcting that.

Your are getting TLE → You are not taking complete input. , remove break statements to get rid of TLE.

Your approach is still wrong :-

else if (ch == '-' && s.find(x) != s.end()) {
			ok = false; 
		}

Here this condition is wrong , this will give wrong answer to valid input also . We can only remove a passanger if he is in the bus and for this situation you are making ok = false.

Here is right condition :-

   if (ch == '-') {
		    if(s.find(x)==s.end()){
		        ok = false;
		    }
			else{
			    s.erase(x);
			}
	}
	else {
		    s.insert(x);
	}
	
    if (s.size() > m) {
		ok = false;
	}

Here is AC code with fixing your wrong condition :- Solution: 49974947 | CodeChef

You might think why we are not checking size in the beginning ,it is because for some case after performing the last query our ( set.size( ) > m ) then we will not be able to check this condition and get WA so we should check our ( set.size( ) > m ) after performing each query operation.

1 Like

Hello!, @pkpawan123 Can You Please help me to find bug in the below code, I’m getting WA after submissions, though all given testCases passes succesfully.

int main()
{
	int test;
	std::cin >> test;

	while (test--)
	{
		int n, s, q, count = 0;
		std::cin >> n >> s >> q;

		string res;
		int key = 1;

		for (int i = 0; i < q; i++)
		{
			string val1, val2;
			std::cin >> val1 >> val2;

			if (val1[0] == '+')
			{
				count++;
				res += val2;
			}
			else if (val1[0] == '-')
			{
				int index;
				index = res.find(val2);

				if (index == string::npos)
				{
					key = 0;
				}

				count--;
			}

			if (count > s)
			{
				key = 0;
			}
		}

			std::cout << (key ? "Consistent" : "Inconsistent") << '\n';
}