102 - Ecological Bin Packing

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

pop7983
New poster
Posts: 2
Joined: Tue Apr 23, 2013 7:40 pm

102 Ecological Bin Packing in JAVA

Post by pop7983 » Mon Aug 05, 2013 10:14 pm

Hello everyone, I've got a problem in 102. I still got a WA !! but I don't know what happened with my code :(
and i also try before many guys there input , and i got the anser of the AC-code is the same answer. but the problem is I still got the WA.
so , plz help me to solved the problem, thx :))
here is my code in java.

Code: Select all

removed

I got AC :)
Last edited by pop7983 on Sun Aug 18, 2013 1:44 pm, edited 2 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 102 Ecological Bin Packing in JAVA

Post by brianfry713 » Tue Aug 06, 2013 1:22 am

Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 102 whats wrong with this?

Post by brianfry713 » Tue Aug 06, 2013 1:24 am

If you want to use q[6], then q should be defined to have size 7.
Check input and AC output for thousands of problems on uDebug!

f.maru
New poster
Posts: 13
Joined: Wed Jul 31, 2013 2:27 pm

Re: 102 whats wrong with this?

Post by f.maru » Wed Aug 07, 2013 3:34 pm

i have tried that but i got WA answer

Code: Select all

#include<iostream>
using namespace std;

int main()
{
    long long bin[4][4],q[7];
    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])
    {
    long long min=10000000,e=0;
    //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;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 102 whats wrong with this?

Post by brianfry713 » Wed Aug 07, 2013 11:20 pm

The minimum might be bigger than 10000000, try 2147483647 instead.
Check input and AC output for thousands of problems on uDebug!

f.maru
New poster
Posts: 13
Joined: Wed Jul 31, 2013 2:27 pm

Re: 102 whats wrong with this?

Post by f.maru » Thu Aug 08, 2013 12:03 pm

TNX i got AC

shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

WA 102 - Ecological Bin Packing

Post by shikhorroy » Thu Aug 22, 2013 8:24 pm

Where is the problem ??? I got WA.....

Code: Select all

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<list>
#include<map>
using namespace std;
#define ll long long int
#define sf scanf
#define pf printf
#define MAX 50
ll array[3][3];
char str[4] = {'B','G','C','\0'};
///B = 0, G = 1, C = 2
ll x, sum, MIN,
    B, G, C;
void planning(ll b, ll g, ll c){
    x = sum - (array[0][b] + array[1][g] + array[2][c]);
    if(x < MIN){
        MIN = x;
        B = b, G  = g, C = c;
    }
}
int main()
{
    while(true)
    {
        bool flag = false;
        sum = 0,  MIN = 999999999999999999;
        for(ll i = 0; i < 3; i++)
        {
            for(ll j = 0; j < 3; j++){
                if(sf("%lld",&array[i][j]) == EOF){
                    flag = true;
                    i = 10;
                    break;
                }
                sum += array[i][j];
            }
        }
        if(flag) break;
        planning(0, 2, 1);
        planning(0, 1, 2);
        planning(2, 1, 0);
        planning(2, 0, 1);
        planning(1, 0, 2);
        planning(1, 2, 0);

        printf("%c%c%c %lld\n",str[B],str[G],str[C],MIN);
    }
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: WA 102 - Ecological Bin Packing

Post by brianfry713 » Thu Aug 22, 2013 11:49 pm

Swap lines 50 and 51 from:
planning(2, 1, 0);
planning(2, 0, 1);
to:
planning(2, 0, 1);
planning(2, 1, 0);
Check input and AC output for thousands of problems on uDebug!

shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

Re: WA 102 - Ecological Bin Packing

Post by shikhorroy » Fri Aug 23, 2013 8:42 am

AC... :)
Thanks dear....

EricM
New poster
Posts: 1
Joined: Sun Dec 08, 2013 7:45 am

102 Can anyone fix my bug, please~~

Post by EricM » Sun Dec 08, 2013 7:48 am

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int minv=2147483647;
int brown[3], green[3], clear[3];
int movev=0;
char binseq[3];

void trybin(int bin1, int bin2, int bin3){
movev=0;
if (bin1==0) {
movev=movev+brown[1]+brown[2];
}
else if (bin1==1){
movev=movev+green[1]+green[2];
}
else{
movev=movev+clear[1]+clear[2];
}

if (bin2==0) {
movev=movev+brown[0]+brown[2];
}
else if (bin2==1){
movev=movev+green[0]+green[2];
}
else{
movev=movev+clear[0]+clear[2];
}
if (bin3==0) {
movev=movev+brown[0]+brown[1];
}
else if (bin3==1){
movev=movev+green[0]+green[1];
}
else{
movev=movev+clear[0]+clear[1];
}

if (movev<=minv) {
minv=movev;
if (bin1==0) {
binseq[0]='B';
}
else if (bin1==1){
binseq[0]='G';
}
else{
binseq[0]='C';
}

if (bin2==0) {
binseq[1]='B';
}
else if (bin2==1){
binseq[1]='G';
}
else{
binseq[1]='C';
}

if (bin3==0) {
binseq[2]='B';
}
else if (bin3==1){
binseq[2]='G';
}
else{
binseq[2]='C';
}
}

}


int main()
{


while (scanf("%d%d%d%d%d%d%d%d%d", &brown[0], &green[0], &clear[0], &brown[1], &green[1], &clear[1], &brown[2], &green[2], &clear[2])==9) {
minv=2147483647;
trybin(0, 1, 2);
trybin(0, 2, 1);
trybin(1, 0, 2);
trybin(1, 2, 0);
trybin(2, 0, 1);
trybin(2, 1, 0);
printf("%c%c%c %d\n",binseq[0], binseq[1], binseq[2], minv);
}

return 0;
} thank you~

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 102 Can anyone fix my bug, please~~

Post by brianfry713 » Mon Dec 09, 2013 9:34 pm

Doesn't match the sample I/O
Check input and AC output for thousands of problems on uDebug!

DevJustinCC
New poster
Posts: 2
Joined: Wed Jan 15, 2014 6:48 am

Problem 102 Bins - 8 submissions and all of them wrong

Post by DevJustinCC » Wed Jan 15, 2014 6:52 am

Hi there,

I cannot seem to figure out where I am going wrong. I have used numerous data sets and all seem to come out correctly. Perhaps someone can help. I took a more complicated than necessary approach, but it still ought to work, as follows:

Code: Select all

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <ctime>

using namespace std;
int main(char* args)
{	string input,token;
	bool set=false;
	int permuts[18] = {1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1};
	
	while(!cin.eof())
	{	int periterator=0;
		string winner;
		getline(cin,input);
		stringstream ss(input);
		unsigned long bins[3] = {1,2,3};
		unsigned long binno=1,maxbrown=0,maxgreen=0,maxclear=0,minmoves=0, counter=0,brownbin=0,greenbin=0,clearbin=0,i=0,first=1,second=2,third=3,j=0,winnerl=0;
		set=false;
		while(j < 6)
		{	binno=1,maxbrown=0,maxgreen=0,maxclear=0,minmoves=0, counter=0,brownbin=0,greenbin=0,clearbin=0,i=1,first=1,second=2,third=3;
			bins[0]=1;
			bins[1]=2;
			bins[2]=3;

			while(i<=3) //for each bin on a line
			{	counter=0;
				binno=1;
				if(j>0 || i>1)
				{	ss.clear();
					ss.seekg(0);	
				}
				while(getline(ss,token,' ')) // for each corresponding bottle per bin
				{	counter++;
					unsigned long bottles=0;
					istringstream(token) >> bottles;
					if(counter==3 && i==permuts[periterator]) //clear bottles
					{	if(bottles>=maxclear && bins[binno-1]!=-1) //if new max and bin not taken
						{	minmoves+=maxclear;
							if(clearbin!=0)bins[clearbin-1]=clearbin;
							bins[binno-1]=-1;
							clearbin=binno;
							maxclear=bottles;
						}
						else 
							if(bottles>0)minmoves+=bottles;
					}
					else if(counter==2 && i==permuts[periterator+1]) //green bottles
					{	if(bottles>=maxgreen && bins[binno-1]!=-1)
						{	minmoves+=maxgreen;
							if(greenbin!=0)bins[greenbin-1]=greenbin;
							bins[binno-1]=-1;
							greenbin=binno;
							maxgreen=bottles;
						}	
						else
							if(bottles>0)minmoves+=bottles;
					}
					else if(counter==1 && i==permuts[periterator+2])//brown bottles
					{	if(bottles>=maxbrown && bins[binno-1]!=-1)
						{	minmoves+=maxbrown;
							if(brownbin!=0)bins[brownbin-1]=brownbin;
							bins[binno-1]=-1;
							brownbin=binno;
							maxbrown=bottles;
						}
						else 
							if(bottles>0)minmoves+=bottles;
					}
					if(counter%3==0)
					{	++binno;
						counter=0;
					}
				}
				
				++i;
			}
			periterator+=3;
			++j;
			
			string binout;
			for(int k=1;k<4;++k)
			{	if(k==brownbin)binout+="B";
				if(k==greenbin)binout+="G";
				if(k==clearbin)binout+="C";
			}
			if(!set || minmoves<winnerl || ( minmoves==winnerl &&  binout < winner))
			{	set=true;
				winnerl=minmoves;
				winner=binout;
			}
		}
		cout << winner << " " << winnerl;
		if(!cin.eof()) cout << endl;
	}
	return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Problem 102 Bins - 8 submissions and all of them wrong

Post by brianfry713 » Fri Jan 17, 2014 1:07 am

Try changing your input parsing, maybe
while(getline(cin, input))
instead of
while(!cin.eof())
Check input and AC output for thousands of problems on uDebug!

DevJustinCC
New poster
Posts: 2
Joined: Wed Jan 15, 2014 6:48 am

Re: Problem 102 Bins - 8 submissions and all of them wrong

Post by DevJustinCC » Sat Jan 18, 2014 12:19 am

Thank you :)

That seemed to be it. I would have expected the eof bit to be set after the last successful getline. In fact, that is exactly what happens with my compiler(VS2010 C++ compiler) This seems odd. I appreciate your time.

EDIT: it would appear I need to read passed eof for the bit to be set. This didn't appear to be happening, although using the peek function could have returned ios_base::eofbit and an equality checked perhaps could have gotten out of loop as well. Funny, the VS2010 compiler seems to not require getting passed EOF as judging from the logic it would have been getline that fails and then non-compliant output. That doesnt happen when I run it with the original code. Time to run it on the uva compiler lol.

Thanks again.

Justin.

me33
New poster
Posts: 16
Joined: Wed Feb 19, 2014 10:08 pm

WA: 102 Why???

Post by me33 » Fri Feb 28, 2014 1:46 am

:D :) :lol:
My Code.

[cpp]Got AC
[/code]

Post Reply

Return to “Volume 1 (100-199)”