当前位置:网站首页>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;
}
边栏推荐
- CVPR 2022 图像恢复论文
- Differences and connections between final and static
- [judgment question] [short answer question] [Database Principle]
- 2022-01-27 redis cluster technology research
- Powerful avatar making artifact wechat applet
- 最新版盲盒商城thinkphp+uniapp
- context. Getexternalfilesdir() is compared with the returned path
- C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
- Swift Error Handling
- 强大的头像制作神器微信小程序
猜你喜欢

剑指 Offer 12. 矩阵中的路径

Ali & ant self developed IDE

Two solutions of leetcode101 symmetric binary tree (recursion and iteration)

Sitescms v3.1.0 release, launch wechat applet

Sword finger offer14 the easiest way to cut rope

T430 toss and install OS majave 10.14

基于同步坐标变换的谐波电流检测

低代码平台国际化多语言(i18n)技术方案
![[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
![[data mining review questions]](/img/96/00f866135e06c4cc0d765c6e499b29.png)
[data mining review questions]
随机推荐
Ten workplace rules
Simple use and precautions of kotlin's array array and set list
SSH登录服务器发送提醒
电压环对 PFC 系统性能影响分析
Grid connection - Analysis of low voltage ride through and island coexistence
context. Getexternalfilesdir() is compared with the returned path
Xctf mobile--rememberother problem solving
How to get user location in wechat applet?
A large select drop-down box, village in Chaoyang District
01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
Differences between initial, inherit, unset, revert and all
最新版盲盒商城thinkphp+uniapp
【习题七】【数据库原理】
Cache penetration and bloom filter
2022-02-10 introduction to the design of incluxdb storage engine TSM
Xctf mobile--app3 problem solving
When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
[judgment question] [short answer question] [Database Principle]
关于CPU缓冲行的理解