Editorial for CCC '15 S2 - Jerseys


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: FatalEagle

A simple greedy algorithm goes like this:

If a jersey is available for a person, give it to him immediately.

This works because we just want to maximize the number of people with jerseys, and each jersey given counts.

You can make the implementation easier by converting the sizes to integers (0 for small, 1 for medium, 2 for large, and -1 for taken).

Complexity: \mathcal{O}(J+A)

Solution — C++

#include <bits/stdc++.h>

using namespace std;

int N, M;
int A[1000001];

int main()
{
    scanf("%d%d", &N, &M);
    for(int i=1; i<=N; i++)
    {
        char c;
        scanf(" %c", &c);
        if(c=='S')
            A[i]=0;
        else if(c=='M')
            A[i]=1;
        else if(c=='L')
            A[i]=2;
    }
    int ans=0;
    for(int i=1; i<=M; i++)
    {
        int a;
        char c;
        scanf(" %c%d", &c, &a);
        int s;
        if(c=='S')
            s=0;
        else if(c=='M')
            s=1;
        else if(c=='L')
            s=2;
        if(1<=a && a<=N && A[a]>=s)
            ans++, A[a]=-1;
    }
    printf("%d\n", ans);
    return 0;
}

Comments

There are no comments at the moment.