Question

Codeforces problem 158B-http://codeforces.com/problemset/problem/158/B

I am getting an unexpected output for test case 5.I think I should get 1 as output but I am it as 2.Please guide me.Judge's log:

Test: #5, time: 30 ms., memory: 380 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER Input 2 2 1 Output 2 Answer 1 Checker Log wrong answer expected 1, found 2

My solution:

#include<iostream>
using namespace std;
int n,a[100000],i,b,c,d,e,f,g,h;
int main()
{
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    b=0;
    c=0;
    d=0;
    e=0;
    for(i=0;i<n;i++)
    {
        if(a[i]==1) //To check for number of 1,2,3 and 4 membered groups.
        b=b+1;
        if(a[i]==2)
        c=c+1;
        if(a[i]==3)
        d=d+1;
        if(a[i]==4)
        e=e+1;  
    }
    f=e;
    if(d>b) //More 3 member groups than 1.
    {
        f=f+d; //f=f+b+(g-b) 3 and 1 member groups combine.Remaining 3 i.e. (g-b) member groups will can't combine with 2 member groups.Hence,they take separate taxies.
        g=-1;
    }
    if(b>=d) //More 1 member groups than 3.
    {
        f=f+d;
        g=b-d; //g=remaining 1 member groups.
    }
    h=(2*c)%4; //Empty seats in last taxi.Possible values can be 0,1,2,3.
    if(h==0)
    f=f+(c/2);
    else
    f=f+((c+1)/2);

    if(g!=-1)
    {
        g=g-h; //Remaining 1 member groups after combining with remaining seats in last 2 member taxi.
        if((g%4)==0)
        f=f+(g/4);
        else
        f=f+(g/4)+1;
    }
    cout<<f;
}
Was it helpful?

Solution

If your input is 2 2 1, then b and c will both be 1, making f 0 and g 1 in the first set of conditionals. h will be (2 * 1) % 4 or 2, making an update to f (0 + 1 = 1). Since g is 1, g-h is -1, which will lead to you executing f=f+(g/4)+1 which is f=1 + (-1/4)+1 which is 1 + 0 + 1 = 2 in integer math.

I think you wanted to check if g-h>0 instead of g!=-1, but there are a ton of places you could simplify your code. Note that using a debugger and stepping through this would have shown you where your problems are much faster, and be much more helpful to increasing your skills, than asking SO.

Just for anyone else looking at this question, this is a fairly simple answer to the problem.

OTHER TIPS

  1. Do it by hand and see if you get the same answer. If you get the same answer by hand as the computation, your algorithm is wrong. If you don't, your code is wrong.
  2. Print variables from intermediate computations to see if they are what you think they should be.
  3. Make sure, if it might matter, that you reinitialize your variables (including arrays) before each use.

Other tips:

  • Use a switch statement instead of multiple if-equal statements.
  • Name your variables. It helps you keep track when looking at your code.
  • When you have multiple variables with similar use, consider using an array instead. b, c, d, and e all seem similar.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top