Group Photo

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 ith of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo, everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is WH, 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 jth(1jn) the photo had everybody except for the jth 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 nlong long w[], long long h[])
{
    long long h1[n];
    for (long long i = 0i < ni++)
        h1[i] = h[i];
    sort(h1h1 + n);
    long long max1 = h1[n - 1];
    long long max2 = h1[n - 2];
    long long sum = 0;
    for (long long i = 0i < ni++)
        sum += w[i];
    long long res;

    for (long long i = 0i < ni++)
    {
        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] = {213}, h[n] = {1097};

    groupPhoto(nwh);

    return 0;
}


No comments:

Post a Comment