CodeForces 1176C - Lose it!(思维)

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

给你一个由n个整数组成的数组a。每个ai是以下六个数字之一:4、8、15、16、23、42。

您的任务是删除使该数组良好的最小元素数。
长度k的数组称为good,如果k可被6整除,并且可以将其拆分为k6子序列4、8、15、16、23、42。

良好数组的示例:

[4,8,15,16,23,42](整个数组是必需的序列);

[4,8,4,15,16,8,23,15,16,42,23,42](第一个序列由第一、第二、第四、第五、第七和第十个元素构成,第二个序列由剩余元素构成);

[](空数组很好)。
坏数组的示例:

[4,8,15,16,42,23](元素的顺序应该正好是4,8,15,16,23,42);

[4,8,15,16,23,42,4](数组的长度不能被6整除);

[4,8,15,16,23,42,4,8,15,16,23,23](第一个序列可以由前六个元素构成,但剩余的数组不能构成所需的序列)。

输入

输入的第一行包含一个整数n(1≤n≤5⋅105)——a中的元素数。

输入的第二行包含n个整数a1、a2、…、an(每个a i是以下数字之一:4、8、15、16、23、42),其中ai是a的第i个元素。

输出
打印一个整数-要获得一个好的数组,必须删除的最小元素数。

题目大意:

给出一个数组,数组内的元素都由 4 8 15 16 23 42 组成,良好的数组是4 8 15 16 23 42 依次数过去(可以不连续),并且良好的数组一定是6的倍数,求要想让当前数组成为良好的数组至少要删除几个元素。

解题思路:

这个题用 O(n) 的方法去求解,要使良好的数组成立,必须使这6个数依次出现,可以不连续,我用map依次映射4 8 15 16 23 42 为1-6,用ans[]表示良好数组中这些数的出现次数,即ans中的数都是已经添加在良好数组中的数。
如果输入的是4时 ans[mp[4]]++,如果不是,则要判断比他小的那一个数出现次数是不是大于当前数,如果是才能++。
比如:要想把15这个数添加到良好数组中,数组中必须要已经有4和8。即前面的数出现次数高于这个数时才能++,如果能顺利加到 42 ,则说明良好序列的数量+1,最后输出n-ans[6]*6的值即可。AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
map<int ,int >mp;
int ans[10]={0};
int main()
{
	mp[4]=1;
	mp[8]=2;
	mp[15]=3;
	mp[16]=4;
	mp[23]=5;
	mp[42]=6;
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a;
		cin>>a;
		if(mp[a]==1)
		  ans[mp[a]]++;
		else if(ans[mp[a]-1]>ans[mp[a]])//如果输入的不是4,则判断一下上一个数的出现次数是否比当前数要多
		  ans[mp[a]]++;
	}
	cout<<n-6*ans[6]<<endl;
	//system("pause");
	return 0;
}
posted @ 2020-06-14 19:35  Hayasaka  阅读(103)  评论(0编辑  收藏  举报