-1

Suppose I have array of positive integers, each standing for a length of a rod, e.g.:

[26, 103, 59]

I want to find some positive integer size into which I will cut each of these rods. I will be penalized by the total number of cuts I make and by the sum of the remainders.


Example

For example, if I cut the rods whose lengths are as above, into pieces of length 6, I will get:

  • rod one (length 26): 4 pieces + remainder 2

  • rod two (length 103): 17 pieces + remainder 1

  • rod three (length 59): 9 pieces + remainder 5

The penalties are

  • 4 + 17 + 9 = 30 cuts

  • 2 + 1 + 5 = 8 remainder


I'd like an algorithm taking as inputs:

  • an array of rod lengths

  • a cut-penalty cost

  • a remainder-penalty cost

and outputting the optimal cut size.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • So what's your code? And where did you get stuck? – Beat May 17 '15 at 13:46
  • https://www.hackerrank.com/contests/juniper-hackathon/challenges/metals This is the problem. And I got stuck in finding optimum length for rod – Sandesh Damkondwar May 17 '15 at 13:47
  • and do you have any initial idea for your algorithm? – Han May 17 '15 at 13:58
  • First case I tried with taking minimum value out of array, and use that as a optimum length. This algo will fail if there is very small number and others are very large numbers. Total cut cost will be much higher in that case. And in other case If I consider taking average of sum of the array, that will be again pretty bad solution. – Sandesh Damkondwar May 17 '15 at 14:11
  • All numbers in the constraints are small enough, so that you could just brute force the solution… – Beat May 17 '15 at 14:12
  • maximum of a rod length is 10000 and maximum of number of rods is 50 so brute force with O(n2) is acceptable. Try to cut from length of 1 to the maximum length and evaluate profit for each case, choose the greatest one. – Han May 17 '15 at 15:19
  • As it stands, the question is completely unclear without reading the full problem statement. You should at least specify what value you want to maximize. – Gassa May 18 '15 at 09:29
  • Two things specifies what I want: 1: "So I can throw little bit of remaining final cut pieces." (we don't want to waste the rods) 2: "I have cost of cut to consider. I want to find optimum length of rod to cut." – Sandesh Damkondwar May 18 '15 at 12:15

1 Answers1

0

It is a brute force solution just try it.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s=new Scanner(System.in);
        long c=s.nextLong();
        long pr=s.nextLong();
        long l=s.nextLong();
        long[] array=new long[(int)l];
        long max=0;
        for(int x=0;x<l;x++){
             array[x]=s.nextLong();
             if(max<array[x])
                  max=array[x];
        }
        long ans=0;
        for(int p=1;p<=max;p++){
            long sum=0;
            long cut=0;
            for(int q=0;q<l;q++){
                sum+=(array[q]/p);
                if(array[q]%p==0){
                cut+=(array[q]/p)-1;
                }else{
                    cut+=(array[q]/p);
                }

            }
            long cost=sum*p*pr-cut*c;
            if(cost>ans)
                 ans=cost;

        }
        System.out.println(ans);   

    }
}
nikhil jain
  • 105
  • 9