HackerRank: Cutting boards
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int m = sc.nextInt();
int n = sc.nextInt();
Integer[] yi = new Integer[m-1];
Integer[] xi = new Integer[n-1];
for(int j=0;j<m-1;j++){
yi[j]= sc.nextInt();
}
for(int j=0;j<n-1;j++){
xi[j]= sc.nextInt();
}
Arrays.sort(yi,Collections.reverseOrder());
Arrays.sort(xi,Collections.reverseOrder());
int ny=1,nx=1;
long c=0;
while(ny<m || nx<n) {
if(ny<m && (nx>=n || yi[ny-1]>xi[nx-1])) {
c= (c + ((long)nx*(long)yi[ny-1])%1000000007)%1000000007;
ny++;
} else if(nx<n && (ny>=m || xi[nx-1]>=yi[ny-1])) {
c= (c + ((long)ny*(long)xi[nx-1])%1000000007)%1000000007;
nx++;
}
}
System.out.println(c);
}
}
Alice gives a wooden board composed of M X N wooden square pieces to Bob and asks him to find the minimal cost of breaking the board into square wooden pieces. Bob can cut the board along horizontal and vertical lines, and each cut divides the board in smaller parts. Each cut has a cost depending on whether the cut is made along a horizontal or a vertical line.
Let us denote the costs of cutting it along consecutive vertical lines with x1, x2, ..., xn-1, and the cost of cutting it along horizontal lines with y1, y2, ..., ym-1. If a cut (of cost c) is made and it passes through n segments, then total cost of this cut will be n*c.
The cost of cutting the whole board into single squares is the sum of the cost of successive cuts used to cut the whole board into square wooden pieces of size 1x1. Bob should compute the minimal cost of breaking the whole wooden board into squares of size 1x1.
The board should be broken in square wooden pieces, so that means we have to cut the board along every horizontal and verticle line.The order in which we make the cuts is important here.For minimum cost, sort the lines according to cost in decreasing order.
Let there be a 5x5 board and the cost along horizontal lines be y1,y2,y3,y4,y5 and along verticle lines be x1,x2,x3,x4,x5
Sort the lines according to cost in decreasing order.Let the lines be y4,x3,y5,x2,x1,y1,x4,y2,y3,x5 in decreasing order of cost.
V = number of vertical cuts
H = number of horizontal cuts
Initially both V and H be zero minimum_cost=0 Now traverse the lines in sorted order if the line is horizontal minimum_cost+=(cost of cut along that particular line)*(V+1) H++ if the line is verticle minimum_cost+=(cost of cut along that particular line)*(H+1) V++
struct arr{
long long int val;
int horv;
};
int main(){
int tc;
scanf("%d",&tc);
while(tc--){
int n,m;
scanf("%d %d",&m,&n);
int i;
int j=0;
for(i=1;i<m;i++){
scanf("%lld",&a[j].val);
a[j].horv=1; // its a horizontal line
j++;
}
for(i=1;i<n;i++){
scanf("%lld",&a[j].val);
a[j].horv=0; // verticle line
j++;
}
sort(a,a+j,compare);
int v=0;
int h=0;
long long int cost=0;
for(i=0;i<j;i++){
if(a[i].horv==1){ // making cut along horizontal line
cost=(cost+(a[i].val*(v+1))%1000000007)%1000000007;
h++;
}
else if(a[i].horv==0){ // making cut along verticle line
cost=(cost+(a[i].val*(h+1))%1000000007)%1000000007;
v++;
}
}
printf("%lld\n",cost);
}
return 0;
}
https://codepair.hackerrank.com/paper/LXB3fHk8?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6ImplZmZlcnl5dWFuIiwiZW1haWwiOiJ5dWFueXVuLmtlbm55QGdtYWlsLmNvbSJ9public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int m = sc.nextInt();
int n = sc.nextInt();
Integer[] yi = new Integer[m-1];
Integer[] xi = new Integer[n-1];
for(int j=0;j<m-1;j++){
yi[j]= sc.nextInt();
}
for(int j=0;j<n-1;j++){
xi[j]= sc.nextInt();
}
Arrays.sort(yi,Collections.reverseOrder());
Arrays.sort(xi,Collections.reverseOrder());
int ny=1,nx=1;
long c=0;
while(ny<m || nx<n) {
if(ny<m && (nx>=n || yi[ny-1]>xi[nx-1])) {
c= (c + ((long)nx*(long)yi[ny-1])%1000000007)%1000000007;
ny++;
} else if(nx<n && (ny>=m || xi[nx-1]>=yi[ny-1])) {
c= (c + ((long)ny*(long)xi[nx-1])%1000000007)%1000000007;
nx++;
}
}
System.out.println(c);
}
}