102 - Ecological Bin Packing
Moderator: Board moderators
why this shows runtime error, this is uva 102
import java.util.Scanner;
class ebp
{
public static int main(String[] args)
{
Scanner s = new Scanner(System.in);
int nb[]=new int[9];
int m=2147483647;
int t=0;
String str="abc";
while(s.hasNextInt())
{
nb[0]=s.nextInt();
nb[1]=s.nextInt();
nb[2]=s.nextInt();
nb[3]=s.nextInt();
nb[4]=s.nextInt();
nb[5]=s.nextInt();
nb[6]=s.nextInt();
nb[7]=s.nextInt();
nb[8]=s.nextInt();
m=2147483647;
t=0;
t=nb[1]+nb[2]+nb[3]+nb[4]+nb[6]+nb[8];
if(t<m)
{
m=t;
str="BCG";
}
t=nb[1]+nb[2]+nb[3]+nb[5]+nb[6]+nb[7];
if(t<m)
{
m=t;
str="BGC";
}
t=nb[1]+nb[0]+nb[5]+nb[4]+nb[6]+nb[8];
if(t<m)
{
m=t;
str="CBG";
}
t=nb[1]+nb[0]+nb[3]+nb[5]+nb[7]+nb[8];
if(t<m)
{
m=t;
str="CGB";
}
t=nb[0]+nb[2]+nb[5]+nb[4]+nb[6]+nb[7];
if(t<m)
{
m=t;
str="GBC";
}
t=nb[0]+nb[2]+nb[3]+nb[4]+nb[7]+nb[8];
if(t<m)
{
m=t;
str="GCB";
}
if(str!=null)System.out.println(str+" "+m);
}
return 0;
}
}
class ebp
{
public static int main(String[] args)
{
Scanner s = new Scanner(System.in);
int nb[]=new int[9];
int m=2147483647;
int t=0;
String str="abc";
while(s.hasNextInt())
{
nb[0]=s.nextInt();
nb[1]=s.nextInt();
nb[2]=s.nextInt();
nb[3]=s.nextInt();
nb[4]=s.nextInt();
nb[5]=s.nextInt();
nb[6]=s.nextInt();
nb[7]=s.nextInt();
nb[8]=s.nextInt();
m=2147483647;
t=0;
t=nb[1]+nb[2]+nb[3]+nb[4]+nb[6]+nb[8];
if(t<m)
{
m=t;
str="BCG";
}
t=nb[1]+nb[2]+nb[3]+nb[5]+nb[6]+nb[7];
if(t<m)
{
m=t;
str="BGC";
}
t=nb[1]+nb[0]+nb[5]+nb[4]+nb[6]+nb[8];
if(t<m)
{
m=t;
str="CBG";
}
t=nb[1]+nb[0]+nb[3]+nb[5]+nb[7]+nb[8];
if(t<m)
{
m=t;
str="CGB";
}
t=nb[0]+nb[2]+nb[5]+nb[4]+nb[6]+nb[7];
if(t<m)
{
m=t;
str="GBC";
}
t=nb[0]+nb[2]+nb[3]+nb[4]+nb[7]+nb[8];
if(t<m)
{
m=t;
str="GCB";
}
if(str!=null)System.out.println(str+" "+m);
}
return 0;
}
}
-
- New poster
- Posts: 19
- Joined: Thu May 20, 2010 8:58 am
Re: 102, WA
hi there,panthenia wrote:who can tell me if all input is zero what string should be output?
the output from my ACC code is
BCG 0
Hope this will help...
![:)](./images/smilies/icon_smile.gif)
one day...
102 Problem Time Exceed, Why??
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
struct Case
{
string bins ;
long long motions;
Case(string s , long long mo)
{
bins = s;
motions = mo;
}
};
Case calcMotions(long long arr [3][3] )
{
vector <Case> Cases;
long long B1 = arr[0][1] + arr[0][2];
long long G1 = arr[1][0] + arr[1][2];
long long C1 = arr[2][0] + arr[2][1];
long long case1 = B1 + G1+C1;
Cases.push_back(Case("BGC",case1));
long long B2 = arr[0][1] + arr[0][2];
long long C2 = arr[2][0] + arr[2][2];
long long G2 = arr[1][0] + arr[1][1];
long long case2 = B2+C2+G2;
Cases.push_back(Case("BCG",case2));
long long C3 = arr[2][1] + arr[2][2];
long long B3 = arr[0][0] + arr[0][2];
long long G3 = arr[1][0] + arr[1][1];
long long case3 = C3 + B3 + G3;
Cases.push_back(Case("CBG",case3));
long long G4 = arr[1][1] + arr[1][2];
long long B4 = arr[0][0] + arr[0][2];
long long C4 = arr[2][0] + arr[2][1];
long long case4 = G4 + B4 + C4;
Cases.push_back(Case("GBC",case4));
long long G5 = arr[1][1] + arr[1][2];
long long C5 = arr[2][0] + arr[2][2];
long long B5 = arr[0][0] + arr[0][1];
long long case5 = G5 + C5 + B5;
Cases.push_back(Case("GCB",case5));
long long C6 = arr[2][1] + arr[2][2];
long long G6 = arr[1][0] + arr[1][2];
long long B6 = arr[0][0] + arr[0][1];
long long case6 = C6 + G6+ B6;
Cases.push_back(Case("CGB",case6));
long long min = Cases[0].motions;
Case tmp = Cases[0];
for(long long i = 1 ;i<Cases.size();i++)
{
if(Cases.motions < min){
min = Cases.motions;
tmp = Cases;
}
else if(Cases.motions == min)
{
if(tmp.bins > Cases.bins)
tmp = Cases;
}
}
return tmp;
}
int main()
{
long long bottels [3][3];
long long num = 0;
long long bigNum = 2147483648;
string str = "";
Case tmp("",0);
while (true)
{
//get 9 bottels
for(long long j = 0 ;j<3;j++)
{
for(long long i = 0 ;i<3;i++)
{
cin>>bottels[j] ;
num += bottels[j];
if(!(num < bigNum))
goto l1;
}
}
tmp = calcMotions(bottels);
cout<<tmp.bins<<" "<<tmp.motions<<endl;
//prlong longf("%s %f\n",tmp.bins , tmp.motions);
l1:
num = 0;
}
return 0;
}
=============
what is the problem in my code, UVA is telling me "Time Limit Exceeded"
, why??
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
struct Case
{
string bins ;
long long motions;
Case(string s , long long mo)
{
bins = s;
motions = mo;
}
};
Case calcMotions(long long arr [3][3] )
{
vector <Case> Cases;
long long B1 = arr[0][1] + arr[0][2];
long long G1 = arr[1][0] + arr[1][2];
long long C1 = arr[2][0] + arr[2][1];
long long case1 = B1 + G1+C1;
Cases.push_back(Case("BGC",case1));
long long B2 = arr[0][1] + arr[0][2];
long long C2 = arr[2][0] + arr[2][2];
long long G2 = arr[1][0] + arr[1][1];
long long case2 = B2+C2+G2;
Cases.push_back(Case("BCG",case2));
long long C3 = arr[2][1] + arr[2][2];
long long B3 = arr[0][0] + arr[0][2];
long long G3 = arr[1][0] + arr[1][1];
long long case3 = C3 + B3 + G3;
Cases.push_back(Case("CBG",case3));
long long G4 = arr[1][1] + arr[1][2];
long long B4 = arr[0][0] + arr[0][2];
long long C4 = arr[2][0] + arr[2][1];
long long case4 = G4 + B4 + C4;
Cases.push_back(Case("GBC",case4));
long long G5 = arr[1][1] + arr[1][2];
long long C5 = arr[2][0] + arr[2][2];
long long B5 = arr[0][0] + arr[0][1];
long long case5 = G5 + C5 + B5;
Cases.push_back(Case("GCB",case5));
long long C6 = arr[2][1] + arr[2][2];
long long G6 = arr[1][0] + arr[1][2];
long long B6 = arr[0][0] + arr[0][1];
long long case6 = C6 + G6+ B6;
Cases.push_back(Case("CGB",case6));
long long min = Cases[0].motions;
Case tmp = Cases[0];
for(long long i = 1 ;i<Cases.size();i++)
{
if(Cases.motions < min){
min = Cases.motions;
tmp = Cases;
}
else if(Cases.motions == min)
{
if(tmp.bins > Cases.bins)
tmp = Cases;
}
}
return tmp;
}
int main()
{
long long bottels [3][3];
long long num = 0;
long long bigNum = 2147483648;
string str = "";
Case tmp("",0);
while (true)
{
//get 9 bottels
for(long long j = 0 ;j<3;j++)
{
for(long long i = 0 ;i<3;i++)
{
cin>>bottels[j] ;
num += bottels[j];
if(!(num < bigNum))
goto l1;
}
}
tmp = calcMotions(bottels);
cout<<tmp.bins<<" "<<tmp.motions<<endl;
//prlong longf("%s %f\n",tmp.bins , tmp.motions);
l1:
num = 0;
}
return 0;
}
=============
what is the problem in my code, UVA is telling me "Time Limit Exceeded"
![:x](./images/smilies/icon_mad.gif)
![:x](./images/smilies/icon_mad.gif)
Problem 102 WA !
my code is as follwing:
I keep getting WA
please any help ?
Code: Select all
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define BGC 1
#define BCG 0
#define GBC 5
#define GCB 4
#define CGB 3
#define CBG 2
int main() {
int B1 ,G1, C1 ,B2 ,G2, C2 ,B3 ,G3, C3 ;
int *min ;
int array[6] ={0};
int index ;
while (cin >> B1 >> G1 >> C1 >> B2 >>G2 >> C2 >> B3 >> G3 >> C3){
//if (cin.eof() == 1) break;
array[BGC] = B2+B3+G1+G3+C1+C2;
array[BCG] = B2+B3+C1+C3+G1+G2;
array[GBC] = G2+G3+B1+B3+C1+C2;
array[GCB] = G2+G3+C1+C3+B1+B2;
array[CGB] = C2+C3+G1+G3+B1+B2;
array[CBG] = C2+C3+B1+B3+G1+G2;
min = min_element(array,array+6);
index = min - array;
switch (index){
case BGC :
cout << "BGC";
break;
case BCG :
cout << "BCG";
break;
case CBG :
cout << "CBG";
break;
case CGB :
cout << "CGB";
break;
case GBC :
cout << "GBC";
break;
case GCB :
cout << "GCB";
break;
default: break;
}
cout << ' ' << array[index]<<endl;
}
return 0;
}
please any help ?
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Problem 102 WA !
Try input 0 1 0 0 0 0 0 0 0
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: Problem 102 WA !
Code: Select all
#include<iostream>
#include<string>
#include <vector>
#include <sstream>
#include <algorithm>
/*Problem ID:102, MindFreak, 19/12/2012*/
using namespace std;
int swap(string &str_array,int i,int j)
{
char str=str_array[j];
str_array[j]=str_array[i];
str_array[i]=str;
return 0;
}
int main()
{
string str_array="BCG";
vector<string> color_combin;
for(int i=0; i<3; i++)
{
for(int i=0; i<2; i++)
{
swap(str_array,i,i+1);
color_combin.push_back(str_array);
}
}
sort(color_combin.begin(),color_combin.end());
vector<int> input_vector;
string str;
getline (cin,str);
istringstream iss(str,istringstream::in);
int temp_int;
while(iss>>temp_int)
{
input_vector.push_back(temp_int);
}
int bottles_nos[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
bottles_nos[i][j]=0;
}
}
int row=0,col=0;
for(int i=0; i<input_vector.size(); i+=3)
{
col=0;
for(int j=i; j<i+3; j++)
{
bottles_nos[row][col++]=input_vector[j];
}
row++;
}
vector<int> weights;
for(int i=0; i<color_combin.size(); i++)
{
string comb_str=color_combin[i];
int sum1=0,sum2=0,sum3=0;
int row=i;
for(int j=0; j<comb_str.length(); j++)
{
if(comb_str[j]=='B')
{
if(j==0)
{
sum1=bottles_nos[1][0]+bottles_nos[2][0];
}
else if(j==1)
{
sum1=bottles_nos[0][0]+bottles_nos[2][0];
}
else
{
sum1=bottles_nos[0][0]+bottles_nos[1][0];
}
}
else if(comb_str[j]=='G')
{
if(j==0)
{
sum2=bottles_nos[1][1]+bottles_nos[2][1];
}
else if(j==1)
{
sum2=bottles_nos[0][1]+bottles_nos[2][1];
}
else
{
sum2=bottles_nos[0][1]+bottles_nos[1][1];
}
}
else if(comb_str[j]=='C')
{
if(j==0)
{
sum3=bottles_nos[1][2]+bottles_nos[2][2];
}
else if(j==1)
{
sum3=bottles_nos[0][2]+bottles_nos[2][2];
}
else
{
sum3=bottles_nos[0][2]+bottles_nos[1][2];
}
}
}
weights.push_back(sum1+sum2+sum3);
}
int min=2147483647;
for(int i=0; i<weights.size(); i++)
{
if(weights[i]<min)
{
min=weights[i];
}
}
int min_index=-1;
for(int i=0; i<weights.size(); i++)
{
if(min==weights[i])
{
min_index=i;
break;
}
}
cout<<color_combin[min_index]<<" "<<min<<endl;
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Problem 102 WA !
The sample output has two lines.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: 102 Runtime Error (SIGSEGV)
What could be the possible reason to get RE?
Code: Select all
#include<iostream>
#include<string>
#include <vector>
#include <sstream>
#include <cctype>
#include <cstdlib>
/*eco_bin_packing,MinDFrEak,20122012*/
using namespace std;
int eco_bin_pack(string str)
{
string array[6]={"BCG","BGC","CBG","CGB","GBC","GCB"};
vector<string> color_combin(array, array + sizeof(array)/sizeof(array[0]));
int bottles_nos[3][3]={0};
int row=0,col=0;
char c;
for(int i=0; i<str.length(); i++)
{
if (!isspace(str.at(i)))
{
c=str[i]-'0';
int temp=c;
bottles_nos[row][col++]=temp;
if(col==3)
{
col=0;
row++;
}
}
}
vector<int> weights;
int sum1=0,sum2=0,sum3=0;
for(int i=0; i<color_combin.size(); i++)
{
string comb_str=color_combin[i];
sum1=0;
sum2=0;
sum3=0;
int row=i;
for(int j=0; j<comb_str.length(); j++)
{
if(comb_str[j]=='B')
{
if(j==0)
{
sum1=bottles_nos[1][0]+bottles_nos[2][0];
}
else if(j==1)
{
sum1=bottles_nos[0][0]+bottles_nos[2][0];
}
else
{
sum1=bottles_nos[0][0]+bottles_nos[1][0];
}
}
else if(comb_str[j]=='G')
{
if(j==0)
{
sum2=bottles_nos[1][1]+bottles_nos[2][1];
}
else if(j==1)
{
sum2=bottles_nos[0][1]+bottles_nos[2][1];
}
else
{
sum2=bottles_nos[0][1]+bottles_nos[1][1];
}
}
else if(comb_str[j]=='C')
{
if(j==0)
{
sum3=bottles_nos[1][2]+bottles_nos[2][2];
}
else if(j==1)
{
sum3=bottles_nos[0][2]+bottles_nos[2][2];
}
else
{
sum3=bottles_nos[0][2]+bottles_nos[1][2];
}
}
}
weights.push_back(sum1+sum2+sum3);
}
int min=2147483647;
for(int i=0; i<weights.size(); i++)
{
if(weights[i]<min)
{
min=weights[i];
}
}
int min_index=-2147483647;
for(int i=0; i<weights.size(); i++)
{
if(min==weights[i])
{
min_index=i;
break;
}
}
cout<<color_combin[min_index]<<" "<<min<<endl;
return 0;
}
int main()
{
string str;
while(1)
{
getline(cin,str);
eco_bin_pack(str);
}
return 0;
}
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Getting TL for UVa 102
I am not sure why am I getting TL for the following code?
Need help!!!!!
Code: Select all
#include<iostream>
#include<string>
/*eco_bin_packing,MinDFrEak,20122012*/
using namespace std;
int eco_bin_pack()
{
string color_combin[6]={"BCG","BGC","CBG","CGB","GBC","GCB"};
int bottles_nos[3][3]={0};
int row=0,col=0;
cin>>bottles_nos[0][0]>>bottles_nos[0][1]>>bottles_nos[0][2]>>bottles_nos[1][0]>>bottles_nos[1][1]>>bottles_nos[1][2]>>bottles_nos[2][0]>>bottles_nos[2][1]>>bottles_nos[2][2];
int min=2147483647,min_index=-2147483647;
int total[6];
int sum1=0,sum2=0,sum3=0;
/*BCG*/
sum1=bottles_nos[1][0]+bottles_nos[2][0];
sum2=bottles_nos[0][2]+bottles_nos[2][2];
sum3=bottles_nos[0][1]+bottles_nos[1][1];
total[0]=sum1+sum2+sum3;
/*BGC*/
sum1=bottles_nos[1][0]+bottles_nos[2][0];
sum2=bottles_nos[0][1]+bottles_nos[2][1];
sum3=bottles_nos[0][2]+bottles_nos[1][2];
total[1]=sum1+sum2+sum3;
/*CBG*/
sum1=bottles_nos[1][2]+bottles_nos[2][2];
sum2=bottles_nos[0][0]+bottles_nos[2][0];
sum3=bottles_nos[0][1]+bottles_nos[1][1];
total[2]=sum1+sum2+sum3;
/*CGB*/
sum1=bottles_nos[1][2]+bottles_nos[2][2];
sum2=bottles_nos[0][1]+bottles_nos[2][1];
sum3=bottles_nos[0][0]+bottles_nos[1][0];
total[3]=sum1+sum2+sum3;
/*GBC*/
sum1=bottles_nos[1][1]+bottles_nos[2][1];
sum2=bottles_nos[0][0]+bottles_nos[2][0];
sum3=bottles_nos[0][2]+bottles_nos[1][2];
total[4]=sum1+sum2+sum3;
/*GCB*/
sum1=bottles_nos[1][1]+bottles_nos[2][1];
sum2=bottles_nos[0][2]+bottles_nos[2][2];
sum3=bottles_nos[0][0]+bottles_nos[1][0];
total[5]=sum1+sum2+sum3;
for(int i=0; i<6; i++)
{
if(total[i]<min)
{
min=total[i];
min_index=i;
}
}
cout<<color_combin[min_index]<<" "<<min<<endl;
return 0;
}
int main()
{
while(1)
{
eco_bin_pack();
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: Getting TL for UVa 102
Your code gets stuck in an infinite loop. You should terminate when there is no more input.
Check input and AC output for thousands of problems on uDebug!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 102 Runtime Error (SIGSEGV)
Don't double post.
Check input and AC output for thousands of problems on uDebug!
102 whats wrong with this?
Code: Select all
#include<iostream>
using namespace std;
int main()
{
unsigned long long int bin1_b, bin1_g, bin1_c, bin2_b, bin2_g, bin2_c, bin3_b, bin3_g, bin3_c, a[5];
unsigned long long int min;
int i, num;
while(cin >> bin1_b)
{
cin >> bin1_g >> bin1_c >> bin2_b >> bin2_g >> bin2_c >> bin3_b >> bin3_g >> bin3_c ;
//simplemente para entender esto es sumas las botellas que no se van a quedar en el orden de los contenedores
//por ejemplo si quiero BGC respectivamente B solo en Bin1 debo sumar los movimientos de G y C por que en esa solo se deben quedar
// los B y igual en el contenedor 2 solo dejar el G y sumar sus otras botellas para terminar con C sumar las que se vam
//BCG
a[0] = (bin1_g + bin1_c) + (bin2_b + bin2_g) + (bin3_b + bin3_c);
//BGC
a[1] = (bin1_g + bin1_c) + (bin2_b + bin2_c) + (bin3_b + bin3_g);
//CBG
a[2] = (bin1_b + bin1_g) + (bin2_g + bin2_c) + (bin3_b + bin3_c);
//CGB
a[3] = (bin1_b + bin1_g) + (bin2_b + bin2_c) + (bin3_g + bin3_c);
//GBC
a[4] = (bin1_b + bin1_c) + (bin2_g + bin2_c) + (bin3_b + bin3_g);
//GCB
a[5] = (bin1_b + bin1_c) + (bin2_b + bin2_g) + (bin3_g + bin3_c);
min = a[0];
num = 0;
for(i=0; i<6; i++)
{
if( min > a[i] )
{
min = a[i];
num = i;
}
}
if(num==0)
cout <<"BCG "<<min<<"\n";
if(num==1)
cout <<"BGC "<<min<<"\n";
if(num==2)
cout <<"CBG "<<min<<"\n";
if(num==3)
cout <<"CGB "<<min<<"\n";
if(num==4)
cout <<"GBC "<<min<<"\n";
if(num==5)
cout <<"GCB "<<min<<"\n";
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 102 whats wrong with this?
On line 5, change a[5] to a[6].
Check input and AC output for thousands of problems on uDebug!
Re: 102 whats wrong with this?
WA can sb help?
Code: Select all
#include<iostream>
using namespace std;
long long bin[4][4],q[6];
int main()
{
while(cin>>bin[1][1]>>bin[1][2]>>bin[1][3]>>bin[2][1]
>>bin[2][2]>>bin[2][3]>>bin[3][1]>>bin[3][2]>>bin[3][3])
{
int min=100000,e;
//bcg
q[1]=bin[2][1]+bin[3][1]+bin[1][3]+bin[3][3]+bin[1][2]+bin[2][2];
//bgc
q[2]=bin[2][1]+bin[3][1]+bin[1][2]+bin[3][2]+bin[1][3]+bin[2][3];
//cbg
q[3]=bin[2][3]+bin[3][3]+bin[1][1]+bin[3][1]+bin[1][2]+bin[2][2];
//cgb
q[4]=bin[2][3]+bin[3][3]+bin[1][2]+bin[3][2]+bin[1][1]+bin[2][1];
//gbc
q[5]=bin[2][2]+bin[3][2]+bin[1][1]+bin[3][1]+bin[1][3]+bin[2][3];
//gcb
q[6]=bin[2][2]+bin[3][2]+bin[1][3]+bin[3][3]+bin[1][1]+bin[2][1];
for (int i=1;i<=6;i++)
{
if (q[i]<min)
{
e=i;
min=q[i];
}
}
if (e==1) cout<<"BCG "<<q[1]<<endl;
if (e==2) cout<<"BGC "<<q[2]<<endl;
if (e==3) cout<<"CBG "<<q[3]<<endl;
if (e==4) cout<<"CGB "<<q[4]<<endl;
if (e==5) cout<<"GBC "<<q[5]<<endl;
if (e==6) cout<<"GCB "<<q[6]<<endl;
}
return 0;
}