119 - Greedy Gift Givers

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

Post Reply
Zuberul
New poster
Posts: 28
Joined: Sun Oct 24, 2004 9:46 pm
Location: dhaka
Contact:

Post by Zuberul » Tue Dec 21, 2004 10:20 pm

I got ac.

ZenseaX
New poster
Posts: 2
Joined: Tue Apr 19, 2005 6:47 am

119 WA...Help

Post by ZenseaX » Sun Jun 05, 2005 11:02 am

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int find_per(char *bas[], char *nam, int ran){
	for(int i = 1; i <= ran; i++){
		if(strcmp(bas[i], nam) == 0)
			return i;
	}
	return 0;
}
int main(){
	int num_per, rec[11], giv[11], 
		mon_hav, num_rec, mon_giv,
		nth_per,
		i, j;
	char *nam[11], *per = new(char);
	while(scanf("%d", &num_per) == 1){
		memset(rec, 0, sizeof(rec));
		memset(giv, 0, sizeof(giv));
		for(i = 1; i <= num_per; i++){
			nam[i] = new(char);
			scanf("%s", nam[i]);
		}
		for(i = 1; i <= num_per; i++){
			mon_giv = 0;
			scanf("%s %d %d", per, &mon_hav, &num_rec);
			nth_per = find_per(nam, per, num_per);
			if(num_rec != 0){
				mon_giv = mon_hav / num_rec;
				giv[nth_per] = mon_giv * num_rec;
			}
			for(j = 1; j <= num_rec; j++){
				scanf("%s", per);
				rec[find_per(nam, per, num_per)] += mon_giv;
			}
		}
		for(i = 1; i <= num_per; i++)
			printf("%s %d\n", nam[i], rec[i] - giv[i]);
		printf("\n");
	}
	return 0;
}
i tried many times, but still get WA, who can help me find the problem in
my code???

jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post by jakabjr » Mon Jun 06, 2005 12:34 pm

i think ur problem is that when (mon_hav/num_rec) yields a remainder, u loose that money, which should be added to rec[nth_per].
in the first example, dave gives 200 to 3 people. that means 198 is given and 2 remains to him. after getting 300 from others, he has in the end 302.
Understanding a problem in a natural way will lead to a natural solution

ZenseaX
New poster
Posts: 2
Joined: Tue Apr 19, 2005 6:47 am

Post by ZenseaX » Tue Jun 07, 2005 4:45 am

Dave just get 300??? Owen gives 500 to him!
I have add the remainders to 'rec', but still WA
Any other problems???

Fali
New poster
Posts: 8
Joined: Fri Jul 15, 2005 4:00 am

119 - Run Time Error

Post by Fali » Sun Sep 18, 2005 11:15 pm

Code: Select all

#include<iostream>
using namespace std;
short n;
struct gift
{char cad[13];
 int spend;
 int rec;
 gift *next;
};
char c[13];
int i,d,g,t,p,k;
gift *cab,*grupo,*aux;
int main()
{while(cin>>n)
 {cab=new gift;
  cin>>cab->cad;
  grupo=cab;
  grupo->rec=0;
  for(i=1;i<n;i++)
  {aux=new gift;
   cin>>aux->cad;
   aux->rec=0;
   grupo->next=aux;
   grupo=grupo->next;
  }
  grupo=cab;
  for(i=0;i<n;i++)
  {cin>>c>>d>>g;
   grupo=cab;
   for(;;)
   {k=0;
    while(c[k]!='\0'&&c[k]==grupo->cad[k])
     k++;
    if(c[k]==grupo->cad[k])
     break;
    grupo=grupo->next;
   }
   grupo->spend=d;
   t=(d/g);
   for(p=0;p<g;p++)
   {cin>>c;
    grupo=cab;
    for(;;)
    {k=0;
     while(c[k]!='\0'&&c[k]==grupo->cad[k])
      k++;
     if(c[k]==grupo->cad[k])
      break;
     grupo=grupo->next;
    }
    grupo->rec+=t;
   }
  }
  grupo=cab;
  p=0;
  do
  {
  i=0;
  while(grupo->cad[i]!='\0')
  {cout<<grupo->cad[i];
   i++;
  }
  cout<<" "<<grupo->spend-grupo->rec<<endl;
  grupo=grupo->next;
  p++;
  }while(p<n);
 }
return 0;
}
This is my code, but i don

zaza123
New poster
Posts: 4
Joined: Sun Oct 16, 2005 1:09 pm

WA119 help

Post by zaza123 » Sun Oct 16, 2005 1:14 pm

i have tested my solution with every test case i could find on this forum but i still got WA. help!

Code: Select all

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

int main(void)
{
    while( !cin.eof() )
        {
            int n;
            vector<string> names;
            map< string, int > give, got;
            cin >> n;
            names.resize(n);
            for( int i = 0; i < n; ++i )
            {
                cin >> names[i];
                give[names[i]] = got[names[i]] = 0;
            }
            for ( int i = 0; i < n; ++i )
            {
                string ime, tokom;
                int st, to;
                cin >> ime >> st >> to;
               
                for( int j = to; j; --j )
                {
                    cin >> tokom;
                    got[tokom] += st / to;
                    give[ime] += st / to;
                }
            }            
            for( int i = 0; i < n; ++i )
                cout << names[i] << " " << got[names[i]] - give[names[i]] << endl;
         if( !cin.eof() )
            cout << endl;
        }
return 0;
}

LisaM
New poster
Posts: 1
Joined: Sun Oct 23, 2005 12:23 am

119 Compile error, using Java

Post by LisaM » Sun Oct 23, 2005 12:33 am

Hello,

I would be frustrated if I got a WA, but a compile error is more maddening somehow, as of course it compiles and runs just fine "on my computer" :-? I even think I have the right answer :)

I have not submitted in a while and am still pretty new to all this. I do have an inner class, but thought that was allowed. I am using Java. The other thing is that the input is terminated by "end of file" and I'm using "while((input = readLine(255)) !=null)" shown in the java example.

Anyway, here's my code, any help would be greatly appreciated, thanks!
Lisa

********pasting code below**********

Code: Select all

/* begin_of_source_code */
/*  @JUDGE_ID:  38154WT  119  Java  */
import java.io.*;
import java.util.*;
class Main
{
    public static void main(String[] args) throws IOException
    {
        class Person
        {
            private String per_strName;
            private int per_iMoney;
        
            public Person()
            {
                per_strName = "yo";
                per_iMoney = 0;
            }
            public Person(String strName, int iMoney)
            {
                per_strName = strName;
                per_iMoney = iMoney;
            }
            public String getName()
            {
                return per_strName;
            }
            public void setName(String strName)
            {
                per_strName = strName;
            }
            public int getMoney()
            {
                return per_iMoney;
            }
            public void setMoney(int iMoney)
            {
                per_iMoney = iMoney;
            }
            public void addMoney(int iMoney)
            {
                per_iMoney = per_iMoney + iMoney;
            }
            public void subtractMoney(int iMoney)
            {
                per_iMoney = per_iMoney - iMoney;
            }
        }//end class person  
        
        String input;
        String strName;
        String giver;
        String luckyone;
        int money = 0;
        int sharedPerPerson = 0;
        int retained = 0;
        int countpresents = 0;
        int iHowMany = 0;
        int step = 1;
        int count = 0;
        StringTokenizer getter;
        //boolean bFirstRun = true;
        boolean bDone = false;
        Person[] allPeople = new Person[50];;
        while((input = readLine(255)) !=null)
        {
            getter = new StringTokenizer(input);
            
            if (step == 1)
            {
                iHowMany = Integer.parseInt(getter.nextToken());
            }
            
            if (step == 2)
            {
                //System.out.println("getting all the names");
                for (int i = 0; i < iHowMany; i++)
                {
                    strName = getter.nextToken();
                    Person x = new Person(strName, 0);
                    allPeople[i] = x;
                }
            }
            
            if (step == 3)
            {
                //for each person, read new line and parse
                for (int i = 0; i <= iHowMany; i++)
                {
                    getter = new StringTokenizer(input);
                    count = getter.countTokens();
                    giver = getter.nextToken();
                    money = Integer.parseInt(getter.nextToken());
;                   countpresents = Integer.parseInt(getter.nextToken());
                    if ((money != 0) && (countpresents != 0))
                    {
                        sharedPerPerson = money/countpresents;
                        retained = money%countpresents;
                    }
                    else
                    {
                        sharedPerPerson = 0;
                        retained = 0;
                    }
                    
                    //for each name who is getting something
                    for (int j = 0; j < countpresents; j++)
                    {
                        luckyone = getter.nextToken();
                        String output = luckyone + " gets " + sharedPerPerson;
                        output += " by " + giver;
                        
                        //iterate the array and check if have right person
                        for (int index = 0; index < iHowMany; index++)
                        {
                            if (allPeople[index].getName().equals(giver))
                            {
                                allPeople[index].subtractMoney(sharedPerPerson);
                                //System.out.println(allPeople[index].getName() + " now has $" + allPeople[index].getMoney());
                            }
                            
                            if (allPeople[index].getName().equals(luckyone))
                            {
                                allPeople[index].addMoney(sharedPerPerson);
                                //System.out.println(allPeople[index].getName() + " now has $" + allPeople[index].getMoney());
                            }
                        }//iterating array
                    }//end for each name getting something loop
                    
                    if (i < (iHowMany - 1))//read new line EXCEPT last time through
                    {
                        input = readLine(255);
                    }
                }//end for each person total loop
                
                //Output
                for (int a = 0; a < iHowMany; a++)
                {
                    String fun = allPeople[a].getName() + "  ";
                    fun += allPeople[a].getMoney();
                    System.out.println(fun);
                }
                System.out.println();
                
                //reset everything
                strName = "";
                giver = "";;
                luckyone = "";;
                money = 0;
                sharedPerPerson = 0;
                retained = 0;
                countpresents = 0;
                iHowMany = 0;
                count = 0;
                allPeople = new Person[50];
            }//end if step 3 statemet
            
            if (step == 3)
            {
                step = 1;
            }
            else
            {
                step++;
            }
        }
    }//end main()
      
    static String readLine(int maxLg) throws IOException {
           byte[] lin = new byte[maxLg];
           int lg = 0, car = -1;
           while(lg < maxLg) {
                   car = System.in.read();
                   if(car<0 || car=='\n') break;
                   lin[lg++] += car;
           }
      if(car<0 && lg==0) return null;
      return new String(lin, 0, lg);
   }//end read line
   
}//end class
/* end_of_source_code */

zaza123
New poster
Posts: 4
Joined: Sun Oct 16, 2005 1:09 pm

Post by zaza123 » Mon Oct 24, 2005 10:34 pm

help please.!!!!!!!!!!!!

User avatar
tmdrbs6584
Learning poster
Posts: 98
Joined: Sat Jan 21, 2006 12:45 pm
Location: Busan,Corea(Republic of)

119 Why WA??

Post by tmdrbs6584 » Sat Feb 04, 2006 8:53 am

#include<iostream.h>
#include<string.h>
int n,i,j;
int h[1000]={0,},q[1000]={0,};
int z,x,c,v,b;
char a[1000][1000],s[1000],d[1000];
int main(){
while(cin >> n){
for(i=0;i<1000;i++){
for(j=0;j<1000;j++)
a[j]=0;
s=0;d=0;q=0;h=0;
}
v=0;
b=0;
for(i=0;i<n;i++)
cin >> a;
for(i=0;i<n;i++){
cin >> s;
cin >> z >> x;
for(j=0;j<n;j++)
if(strcmp(a[j],s) == 0){
h[j] = z;
if (x != 0) q[j] += z%x;
}
for(j=0;j<x;j++){
cin >> d;
for(c=0;c<n;c++)
if(strcmp(a[c],d)==0)
q[c] += z/x;
}
}
for(i=0;i<n;i++)
cout << a << " " << q-h << endl;
cout << endl;
for(i=0;i<1000;i++){
for(j=0;j<1000;j++)
a[j]=0;
h[i]=s[i]=d[i]=q[i]=0;
}
}
return 0;
}

Joel J. Hernandez
New poster
Posts: 8
Joined: Tue May 16, 2006 9:31 am
Location: Las Vegas, NV. USA.
Contact:

119 special case

Post by Joel J. Hernandez » Thu May 18, 2006 10:13 pm

How about a special case:
There is a post on this forum discussing the following input/output:
Input:
2
a 10 0
b 10 0

I'm about to submit my code, and according to it, it will print a -10 and b-9.
Why?
Simple:
a spends 10 and b spends 9. They both received 0, so they lost 10 and 9 recpectively. It makes perfect sense to me according to the problem statement, but apparently it does not make sense for some people. What is even wose, somebody said that if they output 0 for both a and b they got ACC.

Any comments on this....??
I'll submit my code and I will tell you what I get.

c ya.
The harder you work to find the solution, the more you learn in the process.

Joel J. Hernandez
New poster
Posts: 8
Joined: Tue May 16, 2006 9:31 am
Location: Las Vegas, NV. USA.
Contact:

???

Post by Joel J. Hernandez » Thu May 18, 2006 10:16 pm

Ok I got WA...
What the h.....???

Any body??
The harder you work to find the solution, the more you learn in the process.

Joel J. Hernandez
New poster
Posts: 8
Joined: Tue May 16, 2006 9:31 am
Location: Las Vegas, NV. USA.
Contact:

PE

Post by Joel J. Hernandez » Thu May 18, 2006 10:42 pm

Ok I got it but I get PE...
Anything wrong on my output??
Here is output code:

Code: Select all

for(i=0; i<n; i++)
{
    sum=0;
    for(j=0; j<n; j++)
    {
        sum+=matrix[j][i];
        if(matrix[j][i]!=0)
        {
            zerocounter++;
        }
    }
    cout<<p[i].name<<" ";
    if(zerocounter==0)
    {
        cout<<0;
    }
    else
    {
        cout<<sum-capital[i];
    }
    cout<<endl;
}
The harder you work to find the solution, the more you learn in the process.

evterpa
New poster
Posts: 1
Joined: Sun Jun 18, 2006 12:53 pm

Post by evterpa » Sun Jun 18, 2006 1:36 pm

isn't that just what it is supposed to give??? :o

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi » Fri Aug 18, 2006 5:18 pm

i got PE....i didn't print any blank line after the last case.
any one can say why????

thanx in advance.
Sanjana

shuma
New poster
Posts: 1
Joined: Wed Sep 20, 2006 4:48 pm

119

Post by shuma » Wed Sep 20, 2006 4:56 pm

can any 1 give me some special cases on this problem? :cry:

Post Reply

Return to “Volume 1 (100-199)”