2020 Jan Bronze Problem 2 Photoshoot

From Wiki
Revision as of 23:07, 11 June 2023 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Official Problem Statement[edit]

Photoshoot

Official Problem Statement[edit]

Photoshoot

Problem Statement:[edit]

Photoshoot is a problem from the USACO Contest. The goal is to determine the maximum number of distinct photographs that can be taken using a given set of cameras.

Given a set of N cameras, each with a unique field of view, determine the maximum number of distinct photographs that can be taken using the cameras.

Solution:[edit]

The solution to this problem is to use a greedy approach. We can iterate through the cameras and select the camera with the widest field of view. We then add the photographs taken by this camera to our set of distinct photographs. We then repeat the process, selecting the next widest camera and adding its photographs to our set. We continue this process until all cameras have been considered.

The following C++ code implements this solution:

#include <iostream>
#include <vector>

using namespace std;

int photoshoot(vector<int> cameras) {
    int maxPhotos = 0;
    while (!cameras.empty()) {
        int maxFOV = 0;
        int maxIndex = 0;
        for (int i = 0; i < cameras.size(); i++) {
            if (cameras[i] > maxFOV) {
                maxFOV = cameras[i];
                maxIndex = i;
            }
        }
        maxPhotos++;
        cameras.erase(cameras.begin() + maxIndex);
    }
    return maxPhotos;
}

int main() {
    vector<int> cameras = {10, 5, 15, 20};
    cout << photoshoot(cameras) << endl;
    return 0;
}