Lose it! CodeForces - 1176C 题解

Lose it! CodeForces - 1176C

  • 题目
  • 分析
  • 代码
  • 传送门

题目

You are given an array a consisting of n integers. Each ai is one of the six following numbers: 4,8,15,16,23,42.

Your task is to remove the minimum number of elements to make this array good.

An array of length k is called good if k is divisible by 6 and it is possible to split it into k6 subsequences 4,8,15,16,23,42.

Examples of good arrays:

[4,8,15,16,23,42] (the whole array is a required sequence);
[4,8,4,15,16,8,23,15,16,42,23,42] (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements);
[] (the empty array is good).
Examples of bad arrays:

[4,8,15,16,42,23] (the order of elements should be exactly 4,8,15,16,23,42);
[4,8,15,16,23,42,4] (the length of the array is not divisible by 6);
[4,8,15,16,23,42,4,8,15,16,23,23] (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence).
Input
The first line of the input contains one integer n (1≤n≤5⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (each ai is one of the following numbers: 4,8,15,16,23,42), where ai is the i-th element of a.

Output
Print one integer — the minimum number of elements you have to remove to obtain a good array.

Examples
Input

5
4 8 15 16 23

Output

5

Input

12
4 8 4 15 16 8 23 15 16 42 23 42

Output

0

Input

15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42

Output

3

分析

本题关键在于求解出可以组成[4,8.15.16.23.42]序列的个数,需要注意的是每个字母的顺序不能改变,这里我采用map对这些数字进行映射,以方便计算,然后每输入一个数字就进行判断,保证前一个数字的个数始终要比输入的数的个数多。

好了,具体的解题过程可以见我代码,有详细的代码注释哦~

代码

#include
#include
using namespace std;

map<int, int> mp;

int main()
{
	int n;
    cin >> n;
    mp[4] = 0;
    mp[8] = 1;
    mp[15] = 2;
    mp[16] = 3;
    mp[23] = 4;
    mp[42] = 5;
    int ans[6] = {0}; //存放数字的个数 

    for (int i = 0;i < n;i++)
    {
        int a;
        cin >> a;
        if (mp[a] == 0){
			ans[mp[a]]++;
		}else if (ans[mp[a]-1]>0) //只有前面的数字较多时,后面的数才做效。这里让ans[0,4]判定数字的顺序,最后一位存放成组数的数量 
        {
            ans[mp[a]-1]--; //后面的数字消耗前面的数字 
            ans[mp[a]]++;
        }
    }
    cout << n-6*ans[5] << endl; //需要删除的即为数字总数减去所有成组数的数字 

    return 0;
}

传送门

Lose it! CodeForces - 1176C

你可能感兴趣的:(Lose it! CodeForces - 1176C 题解)