Posted: Sun Feb 06, 2005 4:48 pm
hey~ check it out first~
http://online-judge.uva.es/board/viewto ... 9b39ba02c9
http://online-judge.uva.es/board/viewto ... 9b39ba02c9
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
//http://online-judge.uva.es/p/v1/102.html
//Ecological Bin Packing
int answer_array[3];
//to show answer condition,for debug
char answer_order[4];
//to store BCG or BGC or...
int answer_count=0;
//store the minimum count
int min=999;
//compare with the count if minimum
//minimum= count
void output(const int a[][3])
{//output condition
for(int i=0;i<9;i++)
{
printf("%d,",a[0][i]);
if(i%3==2) printf("\n");
}
}
void copy_a(int a[][3])
{
int count=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]!=0)
{
answer_array[i]=a[i][j];
if(j==0)
answer_order[count]='B';
else if(j==1)
answer_order[count]='G';
else if(j==2)
answer_order[count]='C';
count++;
}
answer_order[count]='\0';//set NULL at end
}
void reproduce_data(const int data[][3],int a[][3])
{//because call by name,i will broken my original data
//so need a copy
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
a[i][j]=data[i][j];
}
}
void pack(const int data[][3],int order[][3],int i)
{
if(i==0)
{//if many inputs it will be used
min=299999;
//output(data);
}
int count=0;
int a[3][3];
reproduce_data(data,a);
//because call by name,i will broken my original data
//so need a copy
for(int j=0;j<3;j++)
{
if(order[i][0]!=j)
{//move bin with enery order
a[order[i][0]][0]+=a[j][0];
count+=a[j][0];
a[j][0]=0;
}
if(order[i][1]!=j)
{
a[order[i][1]][1]+=a[j][1];
count+=a[j][1];
a[j][1]=0;
}
if(order[i][2]!=j)
{
a[order[i][2]][2]+=a[j][2];
count+=a[j][2];
a[j][2]=0;
}
}
if(count<min)
{
copy_a(a);
min=count;
answer_count=count;
}
//output(a);
//cout<<"count="<<count<<endl;
}
void main()
{
int sample1[3][3]={{1 ,2, 3},{ 4, 5, 6},{ 7, 8, 9}};
int sample2[3][3]={{5 ,10, 5},{ 20, 10, 5},{ 10, 20, 10}};
//samples in 102
int a[3][3],i;
int order[6][3]={
{0,2,1},
{0,1,2},
{2,0,1},
{2,1,0},
{1,0,2},
{1,2,0}};
//the all order will be try and save the mininum
srand(time(NULL));
for(i=0;i<9;i++)
{//if use rand
int k=rand();
a[0][i]=(k%15)+1;
}
for(i=0;i<6;i++)
{//to solve sample2
pack(sample2,order,i);
//printf("\n");
}
//the output
for(i=0;i<3;i++)
cout<<answer_order[i];
cout<<" "<<answer_count<<endl;
/*
for(i=0;i<3;i++)
cout<<answer_array[i]<<",";
cout<<endl;
*/
}
Code: Select all
#include <iostream>
using namespace std;
int answer_array[3];
char answer_order[4];
int answer_count=0;
int min=999;
void copy_a(int a[][3])
{
int count=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]!=0)
{
answer_array[i]=a[i][j];
if(j==0)
answer_order[count]='B';
else if(j==1)
answer_order[count]='G';
else if(j==2)
answer_order[count]='C';
count++;
}
answer_order[count]='\0';
}
void reproduce_data(const int data[][3],int a[][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
a[i][j]=data[i][j];
}
}
void pack(const int data[][3],int order[][3],int i)
{
if(i==0)
{
min=299999;
}
int count=0;
int a[3][3];
reproduce_data(data,a);
for(int j=0;j<3;j++)
{
if(order[i][0]!=j)
{
a[order[i][0]][0]+=a[j][0];
count+=a[j][0];
a[j][0]=0;
}
if(order[i][1]!=j)
{
a[order[i][1]][1]+=a[j][1];
count+=a[j][1];
a[j][1]=0;
}
if(order[i][2]!=j)
{
a[order[i][2]][2]+=a[j][2];
count+=a[j][2];
a[j][2]=0;
}
}
if(count<min)
{
copy_a(a);
min=count;
answer_count=count;
}
}
main()
{
int data[3][3],i;
int order[6][3]={
{0,2,1},
{0,1,2},
{2,0,1},
{2,1,0},
{1,0,2},
{1,2,0}};
for(i=0;i<9;i++)
{
cin>>data[0][i];
}
for(i=0;i<6;i++)
{
pack(data,order,i);
}
for(i=0;i<3;i++)
cout<<answer_order[i];
cout<<" "<<answer_count<<endl;
return 0;
}
Your program can not handle multiple inputs.The input consists of a series of lines with each line containing 9 integers.
Code: Select all
while (true)
{
for(i=0;i<9;i++)
{
cin>>data[0][i];
if (cin.eof()) return 0;
}
.....
}
2^31 = 2147483647The total number of bottles will never exceed 2^31.
Code: Select all
0 0 0 0 0 0 1 1 0
0 1 0 0 2 0 0 3 0
Code: Select all
#include <iostream>
using namespace std;
char answer_order[4];
int answer_count=0;
void copy_a(long a[][3])
{
int count=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]!=-1)
{
//answer_array[i]=a[i][j];
if(j==0)
answer_order[count]='B';
else if(j==1)
answer_order[count]='G';
else if(j==2)
answer_order[count]='C';
count++;
}
answer_order[count]='\0';
}
void reproduce_data(const long data[][3],long a[][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
a[i][j]=data[i][j];
}
}
void pack(const long data[][3],int order[][3],int i,long &min)
{
if(i==0)
{
min=2147483647;
}
long count=0;
long a[3][3];
reproduce_data(data,a);
for(int j=0;j<3;j++)
{
if(order[i][0]!=j)
{
//a[order[i][0]][0]+=a[j][0];
count+=a[j][0];
a[j][0]=-1;
}
if(order[i][1]!=j)
{
//a[order[i][1]][1]+=a[j][1];
count+=a[j][1];
a[j][1]=-1;
}
if(order[i][2]!=j)
{
//a[order[i][2]][2]+=a[j][2];
count+=a[j][2];
a[j][2]=-1;
}
}
if(count<min)
{
copy_a(a);
min=count;
answer_count=count;
}
}
main()
{
long min;
long data[3][3],i;
int order[6][3]={
{0,2,1},
{0,1,2},
{2,0,1},
{2,1,0},
{1,0,2},
{1,2,0}};
do{
for(i=0;i<9;i++)
{
cin>>data[0][i];
}
for(i=0;i<6;i++)
{
pack(data,order,i,min);
}
for(i=0;i<3;i++)
cout<<answer_order[i];
cout<<" "<<answer_count<<endl;
}while(true);
return 0;
}
Code: Select all
do{
Code: Select all
@BEGIN_OF_SOURCE_CODE
/* @JUDGE_ID: 58050zz 102 C++ */
#include <iostream>
using namespace std;
char answer_order[4];
int answer_count=0;
void copy_a(int i)
{
if(i==0) strcpy(answer_order,"BCG");
if(i==1) strcpy(answer_order,"BGC");
if(i==2) strcpy(answer_order,"GCB");
if(i==3) strcpy(answer_order,"CGB");
if(i==4) strcpy(answer_order,"GBC");
if(i==5) strcpy(answer_order,"CBG");
}
void reproduce_data(const unsigned long data[][3],unsigned long a[][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
a[i][j]=data[i][j];
}
}
void pack(const unsigned long data[][3],int order[][3],int i,unsigned long
&min)
{
if(i==0)
{
min=2147483647;
}
unsigned long count=0;
unsigned long a[3][3];
reproduce_data(data,a);
for(int j=0;j<3;j++)
{
if(order[i][0]!=j)
{
//a[order[i][0]][0]+=a[j][0];
count+=a[j][0];
//a[j][0]=-1;
}
if(order[i][1]!=j)
{
//a[order[i][1]][1]+=a[j][1];
count+=a[j][1];
//a[j][1]=-1;
}
if(order[i][2]!=j)
{
//a[order[i][2]][2]+=a[j][2];
count+=a[j][2];
//a[j][2]=-1;
}
}
if(count<min)
{
copy_a(i);
min=count;
answer_count=count;
}
}
main()
{
unsigned long min;
unsigned long data[3][3],i;
int order[6][3]={
{0,2,1},
{0,1,2},
{2,0,1},
{2,1,0},
{1,0,2},
{1,2,0}};
do{
for(i=0;i<9;i++)
{
if(cin.eof())
return 0;
cin>>data[0][i];
}
for(i=0;i<6;i++)
{
pack(data,order,i,min);
}
for(i=0;i<3;i++)
cout<<answer_order[i];
cout<<" "<<answer_count<<endl;
}while(true);
return 0;
}
@END_OF_SOURCE_CODE
Code: Select all
10 8 9 8 6 0 9 7 5
3 13 7 8 7 1 5 7 1
0 11 8 5 1 2 12 12 7
11 12 10 7 10 8 12 5 3
8 10 6 1 0 3 8 1 10
Code: Select all
CBG 38
CBG 30
CBG 33
CGB 46
GBC 26
If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.
Code: Select all
10 8 9 8 6 0 9 7 5
3 13 7 8 7 1 5 7 1
0 11 8 5 1 2 12 12 7
11 12 10 7 10 8 12 5 3
8 10 6 1 0 3 8 1 10
Code: Select all
10 8 9 8 6 0 9 7 5
3 13 7 8 7 1 5 7 1
0 11 8 5 1 2 12 12 7
11 12 10 7 10 8 12 5 3
8 10 6 1 0 3 8 1 10
CBG 38
CBG 30
CBG 33
CGB 46
GBC 26
Code: Select all
10 8 9 8 6 0 9 7 5
CBG 38
3 13 7 8 7 1 5 7 1
CBG 30
0 11 8 5 1 2 12 12 7
CBG 33
11 12 10 7 10 8 12 5 3
CGB 46
8 10 6 1 0 3 8 1 10
GBC 26
Code: Select all
#include <iostream>
using namespace std;
void main()
{
char *str[6]={"BCG","BGC","CBG","CGB","GBC","GCB"};
unsigned int a[9]; //store input num
unsigned int b[6];
while(1)
{
if(cin.eof())
break;
int i=0;
while(i<9)
{
cin>>a[i];
i++;
}
//six case
b[0]=a[0]+a[5]+a[7];
b[1]=a[0]+a[4]+a[8];
b[2]=a[2]+a[3]+a[7];
b[3]=a[2]+a[4]+a[6];
b[4]=a[1]+a[3]+a[8];
b[5]=a[1]+a[5]+a[6];
//choose the biggest b[i],while the i is the smallest
i=1;
unsigned int t=b[0];
unsigned int s=0;
while(i<6)
{
if(b[i]>t)
{
t=b[i];
s=i;
}
i++;
}
cout<<str[s]<<' ';
s=0;
i=0;
while(i<9)
{
s+=a[i];
i++;
}
cout<<s-t<<endl;
}
}