You and your friends want to take group photos. The process of taking photos can be described as follows:
On the photo, each photographed friend occupies a rectangle of pixels: the of them occupies the rectangle of width pixels and height pixels. On the group photo, everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is , where W is the total sum of all widths and H is the maximum height among the heights of all the photographed friends.
The friends made n photos - the the photo had everybody except for the friend as he was the photographer.
Print the minimum size of each made photo in pixels.
Example:
Input: n = 3, w = [2,1,3], h = [10,9,7]
Output: 36 50 30
Approach
C++
#include <bits/stdc++.h>using namespace std;void groupPhoto(long long n, long long w[], long long h[]){long long h1[n];for (long long i = 0; i < n; i++)h1[i] = h[i];sort(h1, h1 + n);long long max1 = h1[n - 1];long long max2 = h1[n - 2];long long sum = 0;for (long long i = 0; i < n; i++)sum += w[i];long long res;for (long long i = 0; i < n; i++){if (h[i] == max1){res = max2 * (sum - w[i]);cout << res << " ";}else{res = max1 * (sum - w[i]);cout << res << " ";}}}int main(){long long n = 3;long long w[n] = {2, 1, 3}, h[n] = {10, 9, 7};groupPhoto(n, w, h);return 0;}
No comments:
Post a Comment