当前位置:网站首页>luoguP3694邦邦的大合唱站队
luoguP3694邦邦的大合唱站队
2022-07-03 12:07:00 【Oops_Corsini】
题目背景
BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题。
题目描述
N个偶像排成一列,他们来自M个不同的乐队。每个团队至少有一个偶像。
现在要求重新安排队列,使来自同一乐队的偶像连续的站在一起。重新安排的办法是,让若干偶像出列(剩下的偶像不动),然后让出列的偶像一个个归队到原来的空位,归队的位置任意。
请问最少让多少偶像出列?
输入格式
第一行2个整数N,M。
接下来N个行,每行一个整数 a i ( 1 ≤ a i ≤ M ) a_i(1\le a_i \le M) ai(1≤ai≤M),表示队列中第i个偶像的团队编号。
输出格式
一个整数,表示答案
样例 #1
样例输入 #1
12 4
1
3
2
4
2
1
2
3
1
1
3
4
样例输出 #1
7
提示
【样例解释】
1 3 √
3 3
2 3 √
4 4
2 4 √
1 2 √
2 2
3 2 √
1 1
1 1
3 1 √
4 1 √
【数据规模】
对于20%的数据, N ≤ 20 , M = 2 N\le 20, M=2 N≤20,M=2
对于40%的数据, N ≤ 100 , M ≤ 4 N\le 100, M\le 4 N≤100,M≤4
对于70%的数据, N ≤ 2000 , M ≤ 10 N\le 2000, M\le 10 N≤2000,M≤10
对于全部数据, 1 ≤ N ≤ 1 0 5 , M ≤ 20 1\le N\le 10^5, M\le 20 1≤N≤105,M≤20
分析
对于乐队总数只有20,考虑状压DP
dp数组的设定:
设状态 S S S表示为当前已经排好的团队的集合
如 011 011 011为已经排好了 1 , 2 1,2 1,2号团队
d p [ s ] dp[s] dp[s]为排列这些团队最小出队人数
排列好这些团队的人,即为把队伍转换为
11111222223333....44444758666859595 11111222223333....44444 758666859595 11111222223333....44444758666859595(假设有9个团队
此时 S S S为1111,即团队 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4的人都排列好了
转移:
排列好 x x x个团队排列的长度为这 x x x个团队的人数之和 l e n len len
所以我们需要统计每个团队的人数 n u m [ i ] num[i] num[i](团队i的总人数)
假设我们当前正在将第 j j j个团队的人安排到队伍后面(就是在 S S S状态下 j j j团队的人位于排列好团队的后边)
那么 j j j团队的人站的位置为 n u m [ j ] num[j] num[j] 个
所以 j j j团队的人将站在 [ l e n − n u m [ j ] , l e n ] [len-num[j],len] [len−num[j],len]的位置
设 s u m [ i ] [ j ] sum[i][j] sum[i][j]为到当前位置 i i i处 j j j团队的人出现了几个(前缀和)
那么转移为:
d p [ i ] = m i n ( d p [ i ] , d p [ i x o r ( 1 < < ( j − ) ) ] + n u m [ j ] − s u m [ l e n ] [ j ] + s u m [ l e n − n u m [ j ] ] [ j ] dp[i]=min(dp[i],dp[i xor (1<<(j-))]+num[j]-sum[len][j]+sum[len-num[j]][j] dp[i]=min(dp[i],dp[ixor(1<<(j−))]+num[j]−sum[len][j]+sum[len−num[j]][j]
PS:画个图:灰色为产生价值的区间
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mUBQBH10-1655909895836)(file://C:\Users\Administrator\AppData\Roaming\marktext\images\2022-06-22-22-56-37-image.png?msec=1655909797525)]
为什么是$ xor $呢?
i x o r ( 1 < < ( j − 1 ) ) ixor(1<<(j-1)) ixor(1<<(j−1))相当于将 i i i二进制下的第 j − 1 j-1 j−1赋值为0,表示 j j j团队没有被排列前的状态
CODE
#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
int n,m;
int dp[1<<22];
int num[maxn];
int sum[maxn][22];
int main(){
scanf("%d%d",&n,&m);
for(int x,i=1;i<=n;i++){
scanf("%d",&x);
num[x]++;
for(int j=1;j<=m;j++)sum[i][j]=sum[i-1][j];
sum[i][x]++;
}
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
for(int i=1;i<(1<<m);i++){
int len=0;
for(int j=1;j<=m;j++){
if(i&(1<<(j-1)))len+=num[j];
}
for(int j=1;j<=m;j++){
if(i&(1<<(j-1)))dp[i]=min(dp[i],dp[i^(1<<(j-1))]+num[j]-sum[len][j]+sum[len-num[j]][j]);
}
}
cout<<dp[(1<<m)-1];
return 0;
}
边栏推荐
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
- C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
- Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
- initial、inherit、unset、revert和all的区别
- Sword finger offer05 Replace spaces
- Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
- Quick learning 1.8 front and rear interfaces
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
- An example of newtonjason
- Dix règles de travail
猜你喜欢
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

最新版抽奖盲盒运营版

Glide question you cannot start a load for a destroyed activity

Sword finger offer14 the easiest way to cut rope

公纵号发送提示信息(用户微服务--消息微服务)

如何在微信小程序中获取用户位置?

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Sword finger offer06 Print linked list from end to end

【Colab】【使用外部数据的7种方法】

ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
随机推荐
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
自抗扰控制器七-二阶 LADRC-PLL 结构设计
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
RedHat5 安装Socket5代理服务器
Project video based on Linu development
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
并网-低电压穿越与孤岛并存分析
[problem exploration and solution of one or more filters or listeners failing to start]
How to stand out quickly when you are new to the workplace?
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
Leetcode234 palindrome linked list
Ali & ant self developed IDE
【数据库原理及应用教程(第4版|微课版)陈志泊】【第五章习题】
CNN MNIST handwriting recognition
Exploration of sqoop1.4.4 native incremental import feature
The solution to change the USB flash disk into a space of only 2m
Harmonic current detection based on synchronous coordinate transformation
Sword finger offer06 Print linked list from end to end
Enable SASL authentication for memcached