Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Problem 818 D, don't get the explanation

Правка en1, от 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;
}

Теги #implementation

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Officer_K 2017-07-06 13:41:44 1163 Initial revision (published)