Codeforces问题158B - http://codeforces.com/problemset/problem/158/B
我的解决方案在几种情况下给出了意想不到的输出。我目前陷入测试案例4.我无法在我的解决方案中找到错误。我错过了什么?我的代码:
#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;
}
法官的日志:
测试:#4,时间:0 ms。,内存:380 KB,退出代码:0,检查器退出代码:1,判决结果:WRONG_ANSWER 输入 12 1 1 1 1 1 1 1 1 1 1 1 1 产量 12 回答 3 检查日志 错误答案预计3,发现12
答案 0 :(得分:0)
g=g-h; //Remaining 1 member groups after combining with remaining seats in last 2 member taxi.
f=f+g;
这是不正确的IMO。应该是f = f + (g+3)/4
另外,请为变量提供有意义的名称,以便快速调试。
答案 1 :(得分:0)
在if(g!=-1)
声明中,您有g
个剩余的1人组。他们必须乘坐(g+3)/4
的出租车,而不是g
。
换句话说,将此行f=f+g;
更改为f=f+(g+3)/4;
。
顺便说一句,您可以使用运算符+=
进行此类操作。例如,e=e+1
将变为e += 1
甚至更好e++;
(当您使用完全一个增加数字时)。