Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Your company is building cell-phone towers along a straight railway line at points p[0], p[1], …, p[n-1]. The company must construct temporary houses for workers, and wants to ensure two conditions: (i) from every point p[i], the distance to the nearest house should be at most k; and (ii) the number of temporary houses should be as small as possible.
Input: The input will consist of several lines, each containing one number as follows:
The first line will have the number n (0 < n < 100000)
The second line will have the number k (0 < k < n)
The next n lines will contain the values
p[0]
p[1]
:
p[n1] (one value per line)
Note: The numbers p[0], p[1], …, p[n-1] are NOT sorted, and for each value of i, 0 <p[i]>< 10n.
Output: Write a program that prints the (integer) locations at which to construct houses, one integer per line, in INCREASING order. You must also ensure that your program satisfies conditions (i) and (ii) above. Your program should print NOTHING ELSE.
Some sample inputs and outputs are given below. Note that the outputs of your program need not match these outputs exactly – what matters is that conditions (i) and (ii) are satisfied and that the numbers are printed in INCREASING order.
Sample input 1:
5
3
15
8
9
1
12
Sample output 1:
1
10
15
Posted
Updated 1-Mar-15 4:19am
v2
Comments
Mehdi Gholam 1-Mar-15 5:53am    
Looks like homework.
Member 11488706 2-Mar-15 12:14pm    
import java.util.Scanner;
import java.util.Arrays;

/**
*
* @author lenovo g460
*/
public class Hw3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System Resources and Information. This website is for sale!);

System.out.print("Enter the number of towers");
int towers=sc.nextInt();
int a[] = new int[towers];
System.out.print("eneter the point where towers to be placed");
for(int i=0;i<a.length;i++)
{="" a[i]="sc.nextInt();
"
="" }
="" arrays.sort(a);
="" int="" last="a[towers-1];
" b[]="new" int[last+1];
="" for(int="" i="1;i<b.length;i++)
" for(="" j="0;j<a.length;j++)
" {int="" count="0;
" if(a[j]-i<="3" &&="" a[j]-i="">=-3)

count++;

}
b[i]=count;}
for(int i=0;i

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

So think about how you would do it by hand for a simple system, and work from there.
 
Share this answer
 
Comments
Member 11488706 1-Mar-15 5:57am    
i just need to know which algorithm of dynamic program is involved.is it knapsack?
Member 11488706 2-Mar-15 11:25am    
import java.util.Scanner;
import java.util.Arrays;

/**
*
* @author lenovo g460
*/
public class Hw3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System Resources and Information. This website is for sale!);

System.out.print("Enter the number of towers");
int towers=sc.nextInt();
int a[] = new int[towers];
System.out.print("eneter the point where towers to be placed");
for(int i=0;i<a.length;i++)
{="" a[i]="sc.nextInt();
"
="" }
="" arrays.sort(a);
="" int="" last="a[towers-1];
" b[]="new" int[last+1];
="" for(int="" i="1;i<b.length;i++)
" for(="" j="0;j<a.length;j++)
" {int="" count="0;
" if(a[j]-i<="3" &&="" a[j]-i="">=-3)

count++;

}
b[i]=count;}
for(int i=0;i

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900