## 102 - Ecological Bin Packing

Moderator: Board moderators

abhinavswaroop
New poster
Posts: 3
Joined: Tue May 27, 2008 3:10 pm

### Re: Wrong Answer for 102

plz plz phelp.i ma getting WA although i have tested the very large no. of inputs given by "almost human" above and i have got his output only.......
here is my code plz check.
#include<iostream>
using namespace std;

int main()
{
long a[3][3],c[3][3];
string str;

do
{
long temp=0,b[6]={0,0,0,0,0,0};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>c[j];
}
}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[j]=c[j];
}
}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{

if(i!=j)
b[1]+=a[j];
if((i+j)!=0 && (i*j)!=2)
b[0]+=a[j];
if((i+j)!=1 && (i+j)!=4)
b[4]+=a[j];
if((i-j)!=1 && (j-i)!=2)
b[5]+=a[j];
if((j-i)!=1 && (i-j)!=2)
b[2]+=a[j];
if((i+j)!=2)
b[3]+=a[j];
}
}

int k=6;
temp=b[0];
for(int i=1;i<6;i++)
{
if(b<temp)
temp=b[i];
}
for(int i=0;i<6;i++)
{
if(temp==b[i] && k==6)
k=i;
}

if (k==0)
str="BCG";
if (k==1)
str="BGC";
if (k==2)
str="CBG";
if (k==3)
str="CGB";
if (k==4)
str="GBC";
if (k==5)
str="GCB";

cout<<str<<" "<<b[k]<<endl;
}while(cin);

system("pause");
}

Ron
Learning poster
Posts: 55
Joined: Mon Jul 23, 2007 5:01 pm
Location: INDIA

### Re: Wrong Answer for 102

To Mr. abhinavswaroop,
your algo is correct but you are getting problem in taking inputs from file.
your code prints two time output of last line...

NOTE:- when you are pasting code..use BBCode .. !!

To Mr. amrsaqr
in your code also , i got same problem as above.
and one more mistake...that is your last condition

Code: Select all

``````movements=0;
movements=(bottles[0]+bottles[2]+bottles[4]+bottles[5]+bottles[7]+bottles[8]);
if (movements<minimum)
{
minimum=movements;
order[0]='G'; order[1]='C'; order[2]='B';
}``````
but it should be -

Code: Select all

``````movements=0;
movements=(bottles[0]+bottles[2]+bottles[3]+bottles[4]+bottles[7]+bottles[8]);
if (movements<minimum)
{
minimum=movements;
order[0]='G'; order[1]='C'; order[2]='B';
}``````
remaining code is OK..!!

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: Wrong Answer for 102

Thanx Mr.Ron, I Got Accepted
C++ Is The Best.

New poster
Posts: 12
Joined: Fri May 23, 2008 10:24 pm

### Re: 102 Runtime Error (SIGSEGV)

Verdict "Runtime Error":
Removed after accepted
Last edited by The_Madman on Fri Jun 27, 2008 11:31 am, edited 1 time in total.

shiplu_1320
New poster
Posts: 32
Joined: Sat Dec 29, 2007 9:08 pm
Location: CSEDU , Dhaka
Contact:

### Re: 102 Runtime Error (SIGSEGV)

You have declare

Code: Select all

``int sum[5]``
but you are trying to access index sum[5] , which doesn't exist.

Good luck.
A learner......

New poster
Posts: 12
Joined: Fri May 23, 2008 10:24 pm

### Re: 102 Runtime Error (SIGSEGV)

shiplu_1320 wrote:You have declare

Code: Select all

``int sum[5]``
but you are trying to access index sum[5] , which doesn't exist.

Good luck.
Thanks that worked!

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

### 102 getting wa

i am getting wa in 102
my code is

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

int main()
{
long bin1[3],bin2[3],bin3[3],a,b,c;
long min,index;

//freopen("102.txt","r",stdin);
while (scanf("%ld %ld %ld",&a,&b,&c)==3){
if(a&&b==0)break;
bin1[0] = a;
bin1[1] = b;
bin1[2] = c;
for (int i=0; i<3; i++)
cin >> bin2;
for (int j=0; j<3; j++)
cin >> bin3[j];

long bcg = bin2[0] + bin3[0] + bin1[2] + bin3[2] + bin1[1] + bin2[1];
long bgc = bin1[1] + bin1[2] + bin2[0] + bin2[2] + bin3[0] + bin3[1];

long cbg = bin2[2] + bin3[2] + bin1[0] + bin3[0] + bin1[1] + bin2[1];
long cgb = bin2[2] + bin3[2] + bin1[1] + bin3[1] + bin1[0] + bin2[0];

long gbc = bin2[1] + bin3[1] + bin1[0] + bin3[0] + bin1[2] + bin2[2];
long gcb = bin2[1] + bin3[1] + bin1[2] + bin3[2] + bin1[0] + bin2[0];

string list[6] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"};
long value[6] = { bcg, bgc, cbg, cgb, gbc, gcb };

min=value[0];
index=0;

for(int s=0;s<6;s++){
if(value[s]<min){
min=value[s];
index=s;
}
}

cout<<list[index]<<" "<<value[index]<<endl;
}
return 0;
}
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### Re: 102 getting wa

Search the board first.
Don't create a new thread for a problem if one already exists!

Codemonkey2000
New poster
Posts: 9
Joined: Sun Dec 09, 2007 6:46 am

### Re: Wrong Answer for 102

Why do I keep getting WA?

Code: Select all

``````#include <iostream>
#include <string>
using namespace std;

int main()
{
int B[3],G[3],C[3];
long BGC,BCG,CGB,CBG,GBC,GCB;
while(cin>>B[0]>>G[0]>>C[0]>>B[1]>>G[1]>>C[1]>>B[2]>>G[2]>>C[2])
{
BCG=G[0]+C[0]+ B[1]+G[1]+ B[2]+C[2];
BGC=G[0]+C[0]+ B[1]+C[1]+ B[2]+G[2];
CBG=G[0]+B[0]+ G[1]+C[1]+ C[2]+B[2];
CGB=G[0]+B[0]+ B[1]+C[1]+ B[2]+G[2];
GBC=B[0]+C[0]+ G[1]+C[1]+ B[2]+G[2];
GCB=B[0]+C[0]+ B[1]+G[1]+ C[2]+G[2];
long min=BCG;
string out="BCG";
if (min>BGC)
{
min=BGC;
out="BGC";
}
if (min>CBG)
{
min=CBG;
out="CBG";
}
if (min>GBC)
{
min=GBC;
out="GBC";
}
if (min>GCB)
{
min=GCB;
out="GCB";
}
cout<<out<<" "<<min<<endl;
}
}``````

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

### Re: help me to understand 102

any pls help me to understand how this pattern came

UINT uIdxSum[NUM_PATTERN][6] = {{3, 6, 2, 8, 1, 4, }, // "BCG"
{3, 6, 1, 7, 2, 5, }, // "BGC"
{5, 8, 0, 6, 1, 4, }, // "CBG"
{5, 8, 1, 7, 0, 3, }, // "CGB"
{4, 7, 0, 6, 2, 5, }, // "GBC"
{4, 7, 2, 8, 0, 3, }, // "GCB"
};

your help would be highly appreciated
thanx for help
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

jingtra
New poster
Posts: 1
Joined: Sun Nov 16, 2008 1:35 am

### 102: Wrong answer. Why? Anything i have misunderstand?

Thanks for the help

Code: Select all

``````#include<iostream>
#include<vector>

using namespace std;

void add(vector<vector<int> >& bin,vector<vector<int> >& result){

for(int i=0; i<3;++i){
for(int n=0; n<3; ++n){
if(n==i) continue;
result[i][0]+=bin[n][0];
result[i][1]+=bin[n][1];
result[i][2]+=bin[n][2];
}
}

}

string minfunction(vector<vector<int> >& result, int& min){
int a,b,c,sum;
string bgc;

sum=result[0][1]+result[1][2]+result[2][0];
if(min>=sum){ min=sum;bgc="GCB";}

sum=result[0][1]+result[1][0]+result[2][2];
if(min>=sum){ min=sum;bgc="GBC";}

sum=result[0][2]+result[1][1]+result[2][0];
if(min>=sum){ min=sum;bgc="CGB";}

sum=result[0][2]+result[1][0]+result[2][1];
if(min>=sum){ min=sum;bgc="CBG";}

sum=result[0][0]+result[1][1]+result[2][2];
if(min>=sum){ min=sum;bgc="BGC";}

sum=result[0][0]+result[1][2]+result[2][1];
if(min>=sum){ min=sum;bgc="BCG";}

return bgc;}

int main(){

vector< vector<int> > bin(6,vector<int>(3,0));
while(cin>>bin[0][0]>>bin[0][1]>>bin[0][2]>>bin[1][0]>>bin[1][1]>>bin[1][2]>>bin[2][0]>>bin[2][1]>>bin[2][2]){
vector<vector<int> > result(3,vector<int>(3,0));
int min=10000000;
string res=minfunction(result,min);
cout <<res<<' '<<min << endl;
}
return 0;
}
``````

meee...
New poster
Posts: 4
Joined: Tue Dec 09, 2008 4:04 pm

### 102 - Why WA ?? ...

Thank you very much!!

Code: Select all

``````#include <cstdio>
#include <iostream>
using namespace std;

inline long min(long a, long b, long c = -1){
if(c == -1)
return (a > b) ? b : a;
else
return (min(min(a,b),c));
}
int main(){
long i;
long num[10];
char order[4];
long x,y,z;
long op[4];
while(cin>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>num[6]>>num[7]>>num[8]>>num[9]){

long var1 = num[2]+num[8];
long var2 = num[3]+num[6];
long var3 = num[5]+num[2];
long var4 = num[3]+num[9];
long var5 = num[5]+num[8];
long var6 = num[6]+num[9];

op[1] = num[4] + num[7] + min(var1+var2,var3+var4);
op[2] = num[1] + num[7] + min(var5+var2,var3+var6);
op[3] = num[1] + num[4] + min(var1+var6,var5+var4);

long m = min(op[1],op[2],op[3]);
if(op[1] == m){
order[1] = 'B';
if(var1+var2 < var3+var4){order[2] = 'G'; order[3] = 'C';}
else  {order[2] ='C';order[3] = 'G';}
}
else if(op[2] == m){
order[2] = 'B';
if(var5+var2 < var3+var6){order[1] = 'G';order[3] = 'C';}
else {order[3] = 'G'; order[1] = 'C';}
}
else{
order[3] = 'B';
if(var1+var6 < var5+var4){order[2] = 'G'; order[1] = 'C';}
else {order[1] = 'G'; order[2] = 'C';}
}
printf("%c%c%c %ld\n",order[1],order[2],order[3],m);
}
return 0;
}``````

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### Re: 102 - Why WA ?? ...

There are plenty of other threads related to this topic!

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: Wrong Answer for 102

got WA.

ANYONE PLZ PLZ HELP.

Code: Select all

``````  #include<stdio.h>

int   main()

{
long int br1,cl1,gr1;
long int br2,cl2,gr2;
long int br3,cl3,gr3;
long int count[6];
int min;
int i;

freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);

while(scanf("%ld %ld %ld %ld %ld %ld %ld %ld %ld",&br1,&gr1,&cl1,&br2,&gr2,&cl2,&br3,&gr3,&cl3)==9)
{

count[0]=br2+br3+cl1+cl3+gr1+gr2;
//printf("BCG %d\n",count[0]);
count[1]=br2+br3+cl1+cl2+gr1+gr3;
//printf("BGC %d\n",count[1]);
count[2]=br1+br3+cl2+cl3+gr1+gr2;
// printf("CBG %d\n",count[2]);
count[3]=br1+br2+cl3+cl2+gr1+gr3;
// printf("CGB %d\n",count[3]);

count[4]=br1+br2+cl1+cl3+gr2+gr3;
// printf("GCB %d\n",count[4]);
// count[3]=br1+br2+cl3+cl2+gr1+gr3;
//printf("cgb %d\n",count[3]);
count[5]=br1+br3+cl1+cl2+gr2+gr3;
//printf("GBC %d\n",count[5]);
// count[5]=br1+br3+cl2+cl3+gr1+gr2;
// printf("cbg %d\n",count[5]);

min=count[0];
for(i=1;i<6;i++){

if(min>count[i])
min=count[i];

}  // printf("min %d\n",min);

if (min==count[0])
printf("BCG %d\n",min);

else  if(min==count[1])
printf("BGC %d\n",min);

else    if(min==count[2])
printf("CBG %d\n",min);

else   if(min==count[3])
printf("CGB %d\n",min);

else   if(min==count[4])
printf("GBC %d\n",min);

else  if (min==count[5])
printf("GCB %d\n",min);

}

return 0;

}
``````
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

### Re: 102: Wrong answer. Why? Anything i have misunderstand?

jingtra wrote:

Code: Select all

``````    add(bin,result);
int min=10000000;
.......
``````
You can take any of the values of sum as min initially and then continue to find which one is actual min. At least it will solve the problem if your min is not large enough..
Solving for fun..