Problem Description
给你一个曲谱由音符组成 曲谱长度是n(1 <= n <= 1e4),有m(1 <= m <= 16)行,每行有7个音符。一个演奏家可以演奏任意两行的所有音符。曲谱得按顺序演奏(不按顺序演奏,曲谱还有什么意义。。),问你至少需要几个演奏家,就可以把曲谱演奏完。
思路
看到至少,很快想到二分,但是不好判断当前演奏家个数,能否完成。所以换一种方法。
看到m很小,就想到状态压缩。状态压缩后发现暴力可行。
有两种暴力方法:
一:枚举曲谱,判断演奏家能否弹出,还得记录状态。
二:枚举演奏家,判断曲谱最多能弹到哪。(这个比较好理解,也比较好敲,代码是这个)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+100;
int get_num(char s[])//最多有21种音符,用数字0-20代替
{
int t = (s[0] - 'A')*3;
if(s[1] == '\0') return t;
else if(s[1] == 'b') return t+1;
else return t+2;
}
int a[20], song[N];
int main()
{
int n, m;
cin >> m;
char s[5];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 7; j++)
{
scanf("%s", s);
a[i] |= 1<<get_num(s);//把每行的音符都状态压缩为数字
}
}
cin >> n;
for(int i = 0; i < n; i++)
{
scanf("%s", s);
song[i] = get_num(s);
}
int pos = 0, ans = 0, k, i, j;
while(true) {
int Mpos = 0;
for(i = 0; i < m; i++)//枚举所有演奏家
{
for(j = 0; j < m; j++)
{
k = pos;
while(k < n)//判断曲谱最远能到哪
{
if((a[i]&(1<<song[k])) || (a[j]&(1<<song[k])))
k++;
else break;
}
Mpos = max(Mpos, k);
if(Mpos == n) break;
}
if(j < m) break;
}
pos = Mpos; ans++;//我当然选择要最远的,这样人才可能最少
if(i < m) break;
}
printf("%d\n", ans);
}