当前位置:网站首页>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;
}
边栏推荐
- T430 toss and install OS majave 10.14
- 【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
- 2022-01-27 redis cluster cluster proxy predixy analysis
- Kotlin notes - popular knowledge points asterisk (*)
- Create a dojo progress bar programmatically: Dojo ProgressBar
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
- [review questions of database principles]
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
- CNN MNIST handwriting recognition
- When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
猜你喜欢

剑指 Offer 12. 矩阵中的路径

The latest version of lottery blind box operation version

Dojo tutorials:getting started with deferrals source code and example execution summary
![[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number

Huffman coding experiment report
![[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)](/img/45/c2d7934b886d8090373ca9e6e23c97.gif)
[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)

【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】

2022-02-09 survey of incluxdb cluster

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

Ali & ant self developed IDE
随机推荐
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep271
Swift bit operation exercise
[exercise 6] [Database Principle]
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
Node.js: express + MySQL的使用
【习题七】【数据库原理】
Do you feel like you've learned something and forgotten it?
Swift return type is a function of function
Dix règles de travail
initial、inherit、unset、revert和all的区别
2022-02-11 practice of using freetsdb to build an influxdb cluster
Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
Seven second order ladrc-pll structure design of active disturbance rejection controller
I'm too lazy to write more than one character
Harmonic current detection based on synchronous coordinate transformation
Sitescms v3.1.0 release, launch wechat applet
(最新版) Wifi分销多开版+安装框架
Swift Error Handling
【R】【密度聚类、层次聚类、期望最大化聚类】