当前位置:网站首页>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;
}
边栏推荐
- context. Getexternalfilesdir() is compared with the returned path
- ORM use of node -serialize
- Huffman coding experiment report
- 【R】【密度聚类、层次聚类、期望最大化聚类】
- 【習題五】【數據庫原理】
- (latest version) WiFi distribution multi format + installation framework
- Integer case study of packaging
- 并网-低电压穿越与孤岛并存分析
- 剑指 Offer 17. 打印从1到最大的n位数
- 【数据挖掘复习题】
猜你喜欢
[problem exploration and solution of one or more filters or listeners failing to start]
Brief introduction to mvcc
我的创作纪念日:五周年
When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
Powerful avatar making artifact wechat applet
自抗扰控制器七-二阶 LADRC-PLL 结构设计
电压环对 PFC 系统性能影响分析
Social community forum app ultra-high appearance UI interface
Analysis of a music player Login Protocol
Differences between initial, inherit, unset, revert and all
随机推荐
【R】【密度聚类、层次聚类、期望最大化聚类】
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
context. Getexternalfilesdir() is compared with the returned path
对业务的一些思考
How to get user location in wechat applet?
Method overloading and rewriting
[review questions of database principles]
The upward and downward transformation of polymorphism
How to stand out quickly when you are new to the workplace?
Kotlin notes - popular knowledge points asterisk (*)
Xctf mobile--app3 problem solving
Grid connection - Analysis of low voltage ride through and island coexistence
【数据库原理复习题】
Harmonic current detection based on synchronous coordinate transformation
Kung Fu pays off, and learning is done
The solution to change the USB flash disk into a space of only 2m
Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
Quick learning 1.8 front and rear interfaces
[problem exploration and solution of one or more filters or listeners failing to start]
2022-01-27 redis cluster cluster proxy predixy analysis