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

mostruash
New poster
Posts: 8
Joined: Thu May 22, 2014 7:06 pm

Re: 119 Why WA??

Post by mostruash »

Whatever compiler the default is in XCode 5.1.
Blief.S
New poster
Posts: 16
Joined: Wed Jun 18, 2014 9:03 pm

Re: 119 Why WA??

Post by Blief.S »

gtcoder wrote:
Input

Code: Select all

2
Fat Dude
Fat 1000 0
Dude 100 1 Fat
Output

Code: Select all

Fat 100
Dude -100
3. Print a blank line after each case except the last one.
when money given to 0 it becomes 0?
what happens 0 given to anyone?

Code: Select all

laura 0 2 amr vick
Last edited by Blief.S on Fri Jun 27, 2014 12:34 pm, edited 1 time in total.
lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 119 Why WA??

Post by lbv »

Blief.S wrote:when money given to 0 it becomes 0?
I'm afraid I don't understand this question, but see if the comments below help.
Blief.S wrote:what happens 0 given to anyone?
If 0 money is given (as with laura in the sample), that 0 is divided by the number of people to give gifts to, and that result is added to the "net worth" of those people. Just as with any other amount. If you carefully analyze the first sample from the problem statement, you can clearly see what is the result of laura giving away 0 to two friends.

I think the problem statement is failry clear, so when in doubt, take the time to patiently read it as many times as necessary until you understand all the details.
Blief.S
New poster
Posts: 16
Joined: Wed Jun 18, 2014 9:03 pm

Re: 119 Why WA??

Post by Blief.S »

Last edited by Blief.S on Sun Jun 29, 2014 5:04 pm, edited 2 times in total.
Blief.S
New poster
Posts: 16
Joined: Wed Jun 18, 2014 9:03 pm

Re: 119 Why WA??

Post by Blief.S »

Thanks lbv... :)
tidusleonart
New poster
Posts: 10
Joined: Fri Nov 21, 2014 9:18 am

119 - Keep getting WA even though i test many many times

Post by tidusleonart »

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
typedef vector<string> vs;

int indexP(vs& a, string s)
{
	for(int i =0; i < a.size(); i++)
	{
		if(a[i] == s)
			return i;
	}
	return -1;
}

int main()
{
	//ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
	freopen("input.txt", "r", stdin);
	int n;
	//cout << "enter amount person" << endl;
	while(cin >> n)
	{
		cin.ignore();
		vs name;
		string temp, owner, gifter;
		int money[n], cash, amt;
		//cout << "enter: " << endl;
		for(int i = 0; i < n; i++)
		{
			cin >> temp;
			name.push_back(temp);
			money[i] = 0;
		}
		for(int i = 0; i < n; i++)
		{
			cin >> owner;
			int index = indexP(name, owner);
			cin >> cash;
			cin >> amt;
			cin.ignore();
			int received, leftover;
			if(amt != 0){
				leftover = cash%amt;
				//cout << money[index] << endl;
				money[index] = (money[index] - cash + leftover);
				received = cash/amt;
				while(amt--)
				{
					cin >> gifter;
					index = indexP(name, gifter);
					money[index] += received;
				}
			}
		}
		for(int i = 0; i < n; i++)
		{
			cout << name[i] << " " << money[i] << endl;
		}
		cout << "\n";
	}
	return 0;
}
Please help me wats wrong with my code. Thanks
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 119 - Keep getting WA even though i test many many times

Post by lighted »

Post in existing thread. Use search by problem number (119). Don't read from file or post here code that you actually submit. Problem description says
The output for each group should be separated from other groups by a blank line
Don't print blank line after last case
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
tidusleonart
New poster
Posts: 10
Joined: Fri Nov 21, 2014 9:18 am

Re: 119 - Keep getting WA even though i test many many times

Post by tidusleonart »

I'm sorry for posting into a new thread. But you solved my problem. Thanks a alot.
mchowdhu
New poster
Posts: 1
Joined: Thu May 07, 2015 12:49 pm

119-Greedy Gift Givers

Post by mchowdhu »

Hi I am getting wrong answer from the following code and not able to trace error.Could you help me ?

Code: Select all

import java.util.Scanner;
import java.util.HashMap;

class Main{
public static void main(String[] args) 
	{
        Scanner sc = new Scanner(System.in);       
        while (true) {
        	int n = 0;
            try {
             n = sc.nextInt();
            } catch (Exception e) {
                break;
            }
           String[] friends =new String[n];
           HashMap<String,Integer> map=new HashMap<String,Integer>();
           for(int i=0;i<n;i++)
           {
        	friends[i]=sc.next();
        	map.put(friends[i], i);            
           }
           int []money=new int[n];
            for(int i=0;i<n;i++)
            {
            	int idx = map.get(sc.next());
                int gift = sc.nextInt();
                int M = sc.nextInt();
                if (M!= 0) {               
                int rem =gift%M;
                int dev=gift/M;
                money[idx]+=rem-gift;
                for(int j=0;j<M;j++)
                {
                  money[map.get(sc.next())]+=dev;
                }
                }
            }
       for(int j=0;j<n;j++)
        {
        	System.out.println(friends[j] + " " + money[j]);
        }
	}	
	}
}
predicate
New poster
Posts: 18
Joined: Tue Jun 17, 2014 9:32 pm
Location: Hyderabad, India

Re: 119-Greedy Gift Givers

Post by predicate »

Read the problem statement carefully.
The output for each group should be separated from other groups by a blank line.
atulshanbhag
New poster
Posts: 1
Joined: Mon Sep 14, 2015 10:03 pm

Re: 119 - Greedy Gift Givers

Post by atulshanbhag »

Please tell me why this code is getting WA?

Code: Select all

#include <stdio.h>
#include <string.h>
 
int main ()
{
    long np, i, money, person, divide, c, flag = 0;
    char giver [20], receiver [20];
 
    while (scanf ("%ld", &np) != EOF) {
 
        struct friends {
            int spend;
            int receive;
            char name [20];
        } A [12] = {0, 0, 0};
 
        for (i = 0; i < np; i++)
        scanf ("%s", A [i].name);
 
        for (c = 0; c < np; c++ ) {
 
            scanf ("%s", giver);
            scanf ("%ld", &money);
 
            i = 0;
            while (strcmp (A [i].name, giver) != 0)
                i++;
            A [i].spend += money;
 
            scanf ("%ld", &person);
            if (person == 0) {
                A [i].receive += money;
                continue;
            }
 
            divide = person;
            A [i].receive += money - ((money / person) * person);
            while (person--) {
                scanf ("%s", receiver);
                i = 0;
                while (strcmp (A [i].name, receiver) != 0)
                    i++;
                A [i].receive += (money / divide);
            }
        }
 
        if (flag == 1)
            printf("\n");
        flag = 1;
 
        for (i = 0; i < np; i++)
            printf("%s %ld\n", A [i].name, A [i].receive - A [i].spend);
    }
    return 0;
}
EDIT: Problem solved. Thank You
Post Reply

Return to “Volume 1 (100-199)”