Home > Net >  Image labeler google kickstart
Image labeler google kickstart

Time:08-04

problem :


Crowdsource is organizing a campaign for Image Labeler task with participants across N regions. The number of participants from each of these regions are represented by A1,A2,…,AN. In the Image Labeler task, there are M categories. Crowdsource assigns participants to these categories in such a way that all participants from a region are assigned to the same category, and each category has at least one region assigned to it. The success metric of the campaign is measured by the sum of medians of the number of participants in each category. (Let us remind you here that the median of a list of integers is the "middle" number when those numbers are sorted from smallest to largest. When the number of integers in a list is even, we have two "middle" numbers, therefore the median is defined as the arithmetic mean (average) of the two middle values.) Your task is to find the maximum possible value of the success metric that can be obtained by assigning participants in regions to the categories.***

#include <bits/stdc  .h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt;
    cin >> tt;
    for (int qq = 1; qq <= tt; qq  ) {
        cout << "Case #" << qq << ": ";
        int n, m;
        cin >> n >> m;
        vector<int> a(n);
        for (int i = 0; i < n; i  ) {
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        double ans = 0;
        for (int i = 0; i < m - 1; i  ) {
            ans  = a[n - 1 - i];
        }
        ans  = (a[(n - m   1) / 2]   a[(n - m) / 2]) / 2.0;
        cout << fixed << setprecision(12);
        cout << ans << '\n';
    }
    return 0;
}

I have so many questions, but thanks in advance for helping me out when i am good enough i will try to answer others like you help me tysm in advance!! :D

  • Question 1 what do these stuff mean?

    #ifdef tabr
    #include "library/debug.cpp"
    #else
    #define debug(...)
    #endif
    
  • Question 2

     for (int i = 0; i < m - 1; i  ) {
         ans  = a[n - 1 - i];
     }
     ans  = (a[(n - m   1) / 2]   a[(n - m) / 2]) / 2.0;
     cout << fixed << setprecision(12);
     cout << ans << '\n';
    

    }

can someone explain exactly what this block of code does an explanation like this like :

int i= 0;
cout << i;

will be explained like: an integer named i which equals 0 will be printed. because i have no idea what this code does other name the fixed setprecision part and even though i understand it i have no idea why it is in the code like what is its benefit

  • Question 3 why was using vector necessary here? and if it is possible can someone give me a resource that explains vectors in a simple way since i still don't understand it well .

tysm in advance and if my format of writing a question isn't good please give me advice on how to make it better thank you again :D

CodePudding user response:

Q1: These are just declarations for importing libraries

Q2: Before the start of this code, the vector contains n integers in ascending order. The for loop then will sum the last m elements of the array, so therefore summing largest m elements in the input. After the for loop the median of the remaining numbers is appended and the sum is printed to the console. Fixed precision simply refers to how many digits of the decimal are printed.

This technique works by assigning the largest m-1 regions to the their own categories. The median of a one element list is that element, so the largest m-1 regions are all medians. The remaining regions are all assigned to one category. The median of this list is also added after the for loop. Through this, the largest sum of medians is computed.

Q3: A vector is basically a more flexible version of an array. The reason it's used here is because the size of a vector can be declared during runtime and while arrays must be declared at compile-time. A great resource for vectors is the C reference page: https://cplusplus.com/reference/vector/vector/

It is a little hard to read for beginners so a good intro to the subject is here.

  • Related