当前位置:网站首页>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;
}
边栏推荐
- [data mining review questions]
- Kotlin - 改良装饰者模式
- [exercise 7] [Database Principle]
- Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
- 基于Linu开发的项目视频
- [review questions of database principles]
- C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
- 高效能人士的七个习惯
- 01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
- Low code platform international multilingual (I18N) technical solution
猜你喜欢

Deeply understand the mvcc mechanism of MySQL

解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.

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

Attack and defense world mobile--ph0en1x-100

Integer case study of packaging

How to convert a decimal number to binary in swift
![[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

(latest version) WiFi distribution multi format + installation framework

高效能人士的七个习惯

Harmonic current detection based on synchronous coordinate transformation
随机推荐
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
电压环对 PFC 系统性能影响分析
Leetcode234 palindrome linked list
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
Express abstract classes and methods
Xctf mobile--app1 problem solving
剑指 Offer 14- I. 剪绳子
CVPR 2022 图像恢复论文
【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
SQL learning notes (I)
【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
sitesCMS v3.1.0发布,上线微信小程序
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
The solution to change the USB flash disk into a space of only 2m
Swift5.7 extend some to generic parameters
Keep learning swift
最新版盲盒商城thinkphp+uniapp