Question

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

My solution is giving unexpected output in several cases.I am currently stuck at test case 4.I am not able to find mistakes in my solution.What am I missing?My code:

#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.
        f=f+g;
    }
    cout<<f;
}

Judge's log:

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

Was it helpful?

Solution

In the if(g!=-1) statement you have g remaining groups of 1 person. They have to take (g+3)/4 taxis, and not g.

In other words change this line f=f+g; to f=f+(g+3)/4;.

Btw, you can use the operator += for such operations. For example, e=e+1 will become e += 1 or even better e++; (when you increase the number with exactly one).

OTHER TIPS

g=g-h; //Remaining 1 member groups after combining with remaining seats in last 2 member taxi.
f=f+g;

This is incorrect IMO. should be something like f = f + (g+3)/4

Also, please give meaningful names to your variables to enable quick debugging.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top