Computer Science, asked by sakshiverma6360, 7 months ago

Sum of areas
0
You own many plots of an area. However you want to merge the plots to make one big plot for which you go to
an Agent
The agent tells you that in one step, any two plots can be merged and in return, you receive one plot of area
Az - Ay where Az and Ay are the areas of two plots merged in one operation) and the resulting plotis
added to your inventory of plots again but the cost of one operation is equal to the merged area, that is.
Cost = Ar + Ay
This operation can be performed any number of times
Your task is to merge the plots such that in the end you are left with one plot whose area is the sum of all your
current plots but the cost is minimum. Also, you must determine if it is profitable to merge the plots
You are given the cost per unit area of each piot
Note
000
The total cost of the plots is equal to Area cost per unit area.
It is profitable to merge all the plots the cost to merge the plots is less than the total cost of the plots

Answers

Answered by sksharmanewai
32

Answer:

Explanation:

 static void helper(int a[],int b[])

 {

  int n=a.length;

   

  PriorityQueue<Integer> pq= new PriorityQueue<Integer>();

   

  pq.addAll(Arrays.stream(a).boxed().collect(Collectors.toList()));

   

  int mergeCost=0;

   

  while(pq.size()>1)

  {

   int a1= pq.poll();

   int a2= pq.poll();

   

   mergeCost += a1+a2;

   

   pq.add(a1+a2);

  }

   

  int finalSize = pq.peek();

  System.out.println(mergeCost);

   

  int totalCost=0;

  for(int i=0;i<a.length;i++)

  {

   totalCost+= a[i]*b[i];

  }

   

  if(totalCost>mergeCost)

   System.out.println("YES");

  else

   System.out.println("NO");

   

 }

 

public static void main(String[] args) {

 

 

 Scanner sc= new Scanner(System.in);

 

 int N=sc.nextInt();

 int a[]= new int [N];

 int b[]= new int [N];

 for(int i=0;i<N;i++)

 {

  a[i]=sc.nextInt();

 }

 for(int i=0;i<N;i++)

 {

  b[i]=sc.nextInt();

 }

 

 helper(a,b);

}

Answered by aryansuts01
0

Answer:

Concept:

Computer programming is the process of creating and executing an executable computer program to perform a certain computation (or, more broadly, to get a specified computing result). Programming includes analysis, algorithm generation, evaluating algorithm accuracy and resource consumption, and algorithm execution (usually in a chosen programming language, commonly referred to as coding). A program's source code is written in one or more languages that programmers can understand, rather than machine code, which is immediately executed by the central processing unit. Programming's purpose is to create a set of instructions that will automate the execution of a task (which can be as complex as an operating system) on a computer, usually to solve a specific problem.

Given:

The total area You have a large number of plots in a region. You want to combine the plots into one large plot, so you go to an agent. The agent informs you that any two plots can be merged in one step, and in exchange, you will receive one plot of area Az - Ay (where Az and Ay are the areas of two plots merged in one operation) and the resulting plot will be added to your plot inventory once more, but the cost of one operation is equal to the merged area. Cost = Ar + Ay This operation can be performed any number of times Your task is to merge the plots such that in the end you are left with one plot whose area is the sum of all your current plots but the cost is minimum. You must also decide whether merging the plots is profitable. Each piot's cost per unit area is supplied to you. Note The plots' total cost is equal to the Area cost per unit area. It is profitable to merge all the plots the cost to merge the plots is less than the total cost of the plots.

Find:

write the program for the given question

Answer:

static void helper(int a[ ], int b[])

{

 int n = a.length;

 PriorityQueue &lt; Integer &gt; pq = new PriorityQueue &lt; Integer &gt; ();

 pq.addAll(Arrays.stream(a).boxed().collect(Collectors.toList()));

 int mergeCost = 0;

 while(pq.size() &gt; 1)

 {

  int a1 = pq.poll();

  int a2 = pq.poll();

  mergeCost += a1+a2;

  pq.add(a1+a2);

 }

 int finalSize = pq.peek();

 System.out.println(mergeCost);

 int totalCost = 0;

 for(int i=0; i &lt; a.length; i++)

 {

  totalCost += a[i]*b[i];

 }

 if(totalCost &gt; mergeCost)

  System.out.println("YES");

 else

  System.out.println("NO");

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int N = sc.nextInt();

int a[] = new int [N];

int b[] = new int [N];

for(int i=0; i &lt; N; i++)

{

 a[i] = sc.nextInt();

}

for(int i=0; i &lt; N; i++)

{

 b[i] = sc.nextInt();

}

helper(a,b);

}

Key steps

It must be straightforward, object-oriented, and intuitive.

It must be durable and safe.

It must be portable and architecture-independent.

It needs to perform at a high level.

It must be threaded, understood, and dynamic.

#SPJ3

Similar questions