Problem 818 D, don't get the explanation

Revision en1, by Officer_K, 2017-07-06 13:41:44

According to the solution to this problem , when we check a passing car, we should see if the number of appearances of cars with same color as the color of the car we check, is bigger than the number of appearances of the car with the color A and only then increase our counter for that car colour. Why we have to do that ?

here is my code which is obviously wrong

#include <iostream>

using namespace std;

int main(){
    int n, A;
    int cntA=0, cntB=0;
    int arr[100000];

    cin>>n>>A;

    int i,j;

    for(i=0;i<n;i++)
        cin>>arr[i];

    for(i=0;i<n;i++)
        if(arr[i]==A)
            cntA++;

    int max=0, mx, nr;
    for(i=0;i<n-1;i++){
        mx=1;
        for(j=i+1;j<n;j++)
            if(arr[i]==arr[j])
                mx++;
        if(mx>max){
            max=mx;
            nr=arr[i];
        }
    }

    if(cntA == 0)
        cout<<arr[0];//return anything
    else if(max>=cntA)
        cout<<nr;
    else
        cout<<-1;
}

Tags #implementation

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Officer_K 2017-07-06 13:41:44 1163 Initial revision (published)