CHEEZY PIZZA - SEPT005 - EDITORIAL
PROBLEM LINK:
Author: codechefsrm
Editorialist : codechefsrm
DIFFICULTY:
MEDIUM-HARD
PREREQUISITES:
Nested looping, Nested Conditional statements
PROBLEM:
To find cheesiness of two arrays given by C(P,Q)= sum of (C(P[i], Q[j])), for all 0 ≤ i < n1 and i>=j, where C(P[i], Q[j])=i/j if i is completely divisible by j, P[i]>=Q[j] and i>=j ( if j is equal to 0, C(P[i], Q[0])=0), otherwise if P[i]<Q[j] or i is not divisible by j, C(P[i], Q[j])=i+j
EXPLANATION:
Given two arrays P and Q of sizes n1 and n2, the ith element of P has to be compared with jth element of Q for all i>=j.
Now there can be two separate cases,
(i) if P[i]>=Q[j]
In this case, check if j=0, if yes, cheesiness of P[i] and Q[j] is 0. Now check if i is completely divisible j, if yes, find i/j to find the cheesiness of P[i] and Q[j], if not, cheesiness can be found out like in the second case.
(ii)if P[i]<Q[j]
In this case, find the sum of i and j to find the cheesiness if P[i] and Q[j]
Now, you need to find the sum of cheesiness obtained for all all P[i] and Q[j], given i>=j
INPUT
3 5
8 3 2
5 4 3 2 1
OUTPUT
12
Here, firstly i=0 and j=0, since i<=j, 8 and 5 will be compared. Since 8 is greater than 5, this is the first case. Now since j=0, C(8,5)=0.
Next, i=0 and j=1, but since i>j, cheesiness will not be found.
Next, i=1 and j=0, since i<=j, 3 and 5 will be compared. Since 3 is lesser than 5, this is the second case. Therefore, C(3,5)=1+0=1.
Similarly, cheesiness of P[i] and Q[j] for all possible values of i and j, given i>=j and their sum has to be printed.
SOLUTIONS:
Setter's Solution
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cin >> n1>>n2;
int a[n1], b[n2];
for(int i=0; i<n1; i++){
cin >> a[i];
}
for(int i=0; i<n2; i++){
cin >> b[i];
}
int sum = 0, c;
for(int i=0; i<n1; i++){
for(int j=0; j<=i && j<n2; j++){
if(a[i]>=b[j]){
if(j==0){
c=0;
}
else{
if (i%j==0){
c=i/j;
}
else {
c=i+j;
}
}
}
else{
c=i+j;
}
sum=sum+c;
}
}
cout << sum;
return 0;
}