Skip to content

Instantly share code, notes, and snippets.

@viniru
Created July 23, 2018 07:48
Show Gist options
  • Save viniru/2cd29ea3d18f093612beef66c23c94ca to your computer and use it in GitHub Desktop.
Save viniru/2cd29ea3d18f093612beef66c23c94ca to your computer and use it in GitHub Desktop.
cpu scheduling alogorithm based on weight/length ratio of the jobs
import java.util.*;
public class CPU_SCHEDULING{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int temp = n;
ArrayList<Node> ar = new ArrayList<>();
while(n-->0)
{
Node nd = new Node(sc.nextInt(),sc.nextInt());
ar.add(nd);
}
Collections.sort(ar,new Sort());
long sum = 0;
long length = 0;
for(Node x : ar)
{
length = length + x.l;
sum = sum + x.w * ( length);
}
System.out.println(sum);
}
}
class Node{
int w,l;
float dif;
Node(int w,int l)
{
this.w = w;
this.l = l;
dif = (float)w / (float)l;
}
public String toString()
{
return this.w+" "+this.l+" "+this.dif;
}
}
class Sort implements Comparator<Node>
{
public int compare(Node a , Node b)
{
if(a.dif - b.dif != 0)
return (int)Math.signum(b.dif - a.dif);
else return (int)Math.signum(b.w - a.w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment