当前位置:网站首页>Luogup3694 Bangbang chorus standing in line
Luogup3694 Bangbang chorus standing in line
2022-07-03 12:59:00 【Oops_ Corsini】
Background
BanG Dream! All the idols in the band are going to sing together , But there were some problems in the queue .
Title Description
N A line of idols , They come from M A different band . Each team has at least one idol .
Now it's required to reschedule , Make the idols from the same band stand together continuously . The way to rearrange is , Let some idols stand out ( The rest of the idols don't move ), Then let the idols return one by one to the original empty seats , Return to any position .
At least how many idols are allowed to be listed ?
Input format
first line 2 It's an integer N,M.
Next N Row , One integer per row a i ( 1 ≤ a i ≤ M ) a_i(1\le a_i \le M) ai(1≤ai≤M), Represents the second in the queue i An idol's team number .
Output format
An integer , Answer
Examples #1
The sample input #1
12 4
1
3
2
4
2
1
2
3
1
1
3
4
Sample output #1
7
Tips
【 Sample explanation 】
1 3 √
3 3
2 3 √
4 4
2 4 √
1 2 √
2 2
3 2 √
1 1
1 1
3 1 √
4 1 √
【 Data scale 】
about 20% The data of , N ≤ 20 , M = 2 N\le 20, M=2 N≤20,M=2
about 40% The data of , N ≤ 100 , M ≤ 4 N\le 100, M\le 4 N≤100,M≤4
about 70% The data of , N ≤ 2000 , M ≤ 10 N\le 2000, M\le 10 N≤2000,M≤10
For all data , 1 ≤ N ≤ 1 0 5 , M ≤ 20 1\le N\le 10^5, M\le 20 1≤N≤105,M≤20
analysis
For the total number of bands, only 20, Consider the pressure DP
dp Array setting :
Set the State S S S Represents a collection of currently scheduled teams
Such as 011 011 011 It's already arranged 1 , 2 1,2 1,2 Team
d p [ s ] dp[s] dp[s] To arrange the minimum number of people out of these teams
Arrange the people of these teams , That is to convert the team into
11111222223333....44444758666859595 11111222223333....44444 758666859595 11111222223333....44444758666859595( Suppose there is 9 A team
here S S S by 1111, The team 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4 All the people are lined up
Transfer :
Arrange well x x x The length of the teams is this x x x The sum of the number of teams l e n len len
So we need to count the number of people in each team n u m [ i ] num[i] num[i]( The team i The total number of people )
Suppose we are currently putting the j j j A team of people are arranged behind the team ( Is in the S S S State, j j j The people in the team are behind the arranged team )
that j j j The position of the team is n u m [ j ] num[j] num[j] individual
therefore j j j The team will stand [ l e n − n u m [ j ] , l e n ] [len-num[j],len] [len−num[j],len] The location of
set up s u m [ i ] [ j ] sum[i][j] sum[i][j] To the current location i i i It's about j j j There are several people in the team ( The prefix and )
So transfer to :
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: Draw a picture : Grey is the range of value
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mUBQBH10-1655909895836)(file://C:\Users\Administrator\AppData\Roaming\marktext\images\2022-06-22-22-56-37-image.png?msec=1655909797525)]
Why $ xor $ Well ?
i x o r ( 1 < < ( j − 1 ) ) ixor(1<<(j-1)) ixor(1<<(j−1)) Is equivalent to i i i The second under binary j − 1 j-1 j−1 The assignment is 0, Express j j j The state before the team is not ranked
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;
}
边栏推荐
- 剑指 Offer 17. 打印从1到最大的n位数
- Grid connection - Analysis of low voltage ride through and island coexistence
- Harmonic current detection based on synchronous coordinate transformation
- Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
- ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
- Leetcode234 palindrome linked list
- Seven second order ladrc-pll structure design of active disturbance rejection controller
- CVPR 2022 图像恢复论文
- Nodejs+express+mysql realizes login function (including verification code)
猜你喜欢

initial、inherit、unset、revert和all的区别

自抗扰控制器七-二阶 LADRC-PLL 结构设计

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment

Idea full text search shortcut ctr+shift+f failure problem

最新版盲盒商城thinkphp+uniapp

When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution

OpenHarmony应用开发之ETS开发方式中的Image组件

Analysis of the influence of voltage loop on PFC system performance
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]

4. 无线体内纳米网:电磁传播模型和传感器部署要点
随机推荐
剑指 Offer 16. 数值的整数次方
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
Express abstract classes and methods
alright alright alright
Xctf mobile--app3 problem solving
Powerful avatar making artifact wechat applet
I'm too lazy to write more than one character
ORM use of node -serialize
OpenHarmony应用开发之ETS开发方式中的Image组件
[problem exploration and solution of one or more filters or listeners failing to start]
Harmonic current detection based on synchronous coordinate transformation
剑指 Offer 14- I. 剪绳子
[data mining review questions]
Everything comes to him who waits
【习题六】【数据库原理】
【Colab】【使用外部数据的7种方法】
电压环对 PFC 系统性能影响分析
Redhat5 installing socket5 proxy server
Solve the problem of VI opening files with ^m at the end
2022-01-27 redis cluster brain crack problem analysis