题意: 给定一个序列,有n个元素, 每个元素只能用一次,求出最多能拼成的数组"4 ,8, 15, 16, 23, 42"的个数T,并输出n - 6T;
策略:离散化原数组,然后遍历,若当前位置为i时,如果a[i] = 1 则cnt[1]++,否则必须保证i-1的个数大于i已有的个数,才能将i计入一个新的子序列,cnt[i]+1。最后cnt[6]就是最多的满足要求的子序列个数。
题目传送门
#include
#include
#define oo INT_MAX
#define ll long long
#define db double
#define mp(a, b) make_pair(a, b)
#define met(a, b) memset(a, b, sizeof(a))
#define maxn 500009
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i = (a); i < (b) ;++i)
using namespace std;
using namespace __gnu_pbds;
int n, a[maxn], cnt[maxn];
int main(){
ios::sync_with_stdio(0);
int n;
cin >> n;
_rep(i, 1, n){
cin >> a[i];
if(a[i] == 4)a[i] = 1;
else if(a[i] == 8) a[i] = 2;
else if(a[i] == 15) a[i] = 3;
else if(a[i] == 16) a[i] = 4;
else if(a[i] == 23) a[i] = 5;
else a[i] = 6;
}
_rep(i, 1, n){
_rep(j, 1, 6){
if(a[i] == j && (j == 1 || cnt[j - 1] > cnt[j]))cnt[j] ++;
}
}
cout << n - cnt[6] * 6 << endl;
}
#include
#include
#define oo INT_MAX
#define ll long long
#define db double
#define mp(a, b) make_pair(a, b)
#define met(a, b) memset(a, b, sizeof(a))
#define maxn 500009
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define _rev(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i = (a); i < (b) ;++i)
using namespace std;
using namespace __gnu_pbds;
int n, a[maxn], cnt[maxn];
int main(){
ios::sync_with_stdio(0);
int n;
cin >> n;
vector<int> p ({4,8,15,16,23,42});
_rep(i, 1, n){
cin >> a[i];
a[i] = upper_bound(p.begin(), p.end(), a[i]) - p.begin();
}
vector<int> cnt (7);
_rep(i, 1, n){
if(a[i] == 1)cnt[1] ++ ;
else if(cnt[a[i] - 1] > cnt[a[i]]){
cnt[a[i]] ++ ;
}
}
cout << n - cnt[6] * 6 << endl;
}