the solution of Gym102222I

Правка en18, от tarjen, 2022-05-01 14:05:06

102222I - Bubble Sort

2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest

We define A is the permutation of n, and B is a sequence of n

Bi is defined as the number of subscripts that satisfy the condition k<i&&Ak>Ai

Obviously,after a round of bubbling,Biwill minus one (if Bi>0), and Ai will also move forward one space (i--)(although this is useless)

We consider to use sequence B to solve the problem, we first prove that for every valid sequenceB, there is exactly one permutation A corresponding to it

We consider how to infer A from B

It is very obvious that An (the length of the permutation is n) can be directly infered by Bn, and $$$A n-1$$$ is also easily to know if An is known,and so on. So as long as B is legal, there is an A permutation obtained, certificated

That is to say, for each bubbling, every Bi greater than 0 is decremented by one, and equal to 0, it remains unchanged

So we only need to calculate the legal number of sequence of B

Consider what final states are legal:

  1. All Bi=0 (ie A is ordered)

  2. There is a consecutive subsequence of Bi=1 in the permutation, and the rest are 0, such as 0 0 1 1 0 (1 4 2 3 5)

  3. There is only one Bi!=0 in the permutation, and the others are 0, such as 0 0 2 0 0 (2 3 1 4 5)

I think it is easy to use Dynamic programming to solve this problem, set 3 states is ok

memset(dp,0,sizeof(dp));
    int ans2=0;
    cin>>n>>m>>mod;
    dp[0][0]=1;
    for(int i=1;i<=n;i++){
        if(i<=m+1){
            (dp[i][0]+=dp[i-1][0]*(i))%=mod;
        }
        else{
            (dp[i][0]+=dp[i-1][0]*(m+1))%=mod;
            (dp[i][1]+=dp[i-1][0]+dp[i-1][1])%=mod;
            (dp[i][2]+=dp[i-1][1]*(m+1)+dp[i-1][2]*(m+1)+dp[i-1][0]*max(0ll,i-1-(m+1)))%=mod;
        }
    }
    int ans=0;
    for(int i=0;i<3;i++)ans+=dp[n][i];
    cout<<"Case #"<<ts<<": "<<ans%mod<<"\n";

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en32 Английский tarjen 2022-05-02 07:22:04 1 Tiny change: 'ously,...)\n\nSo we ' -> 'ously,...).\n\nSo we '
en31 Английский tarjen 2022-05-02 07:21:35 6 Tiny change: 'stimulate ,use the concl' -> 'stimulate by the concl'
en30 Английский tarjen 2022-05-02 07:20:31 86
en29 Английский tarjen 2022-05-01 14:50:17 8
en28 Английский tarjen 2022-05-01 14:47:45 4 Tiny change: 'es is ok\n```\n\n ' -> 'es is ok\n\n\n```\n\n '
en27 Английский tarjen 2022-05-01 14:46:00 13
en26 Английский tarjen 2022-05-01 14:45:26 14
en25 Английский tarjen 2022-05-01 14:12:32 2 Tiny change: 'on $$ k<i and Ak>Ai $$\' -> 'on $$ k<i ~and~ Ak>Ai $$\'
en24 Английский tarjen 2022-05-01 14:11:53 4 Tiny change: 'on $$ k<i & Ak>Ai $$\' -> 'on $$ k<i and Ak>Ai $$\'
en23 Английский tarjen 2022-05-01 14:11:30 2 Tiny change: 'ondition $ k<i & Ak>Ai $\n\nObvio' -> 'ondition $$ k<i & Ak>Ai $$\n\nObvio'
en22 Английский tarjen 2022-05-01 14:09:58 2 Tiny change: 'ion $ k<i ~ Ak>Ai $\n' -> 'ion $ k<i & Ak>Ai $\n'
en21 Английский tarjen 2022-05-01 14:09:31 5 Tiny change: 'tion $ k<i&&Ak>Ai $\n\' -> 'tion $ k<i ~ Ak>Ai $\n\'
en20 Английский tarjen 2022-05-01 14:07:28 3
en19 Английский tarjen 2022-05-01 14:06:52 100
en18 Английский tarjen 2022-05-01 14:05:06 4 Tiny change: '`Bn`, and `A n-1` is also e' -> '`Bn`, and $A n-1$ is also e'
en17 Английский tarjen 2022-05-01 13:48:05 135
en16 Английский tarjen 2022-05-01 13:38:40 39
en15 Английский tarjen 2022-05-01 13:37:35 26 Tiny change: ' bubbling,the number of k`Ak>Ai&&k>i` will minus' -> ' bubbling,`Bi`will minus'
en14 Английский tarjen 2022-05-01 13:35:57 47
en13 Английский tarjen 2022-05-01 13:32:50 15
en12 Английский tarjen 2022-05-01 13:28:08 2 Tiny change: 'm:102222I][contest:1' -> 'm:102222I]\n[contest:1'
en11 Английский tarjen 2022-05-01 13:27:34 16 Tiny change: 'm:102222I]\n\n\nObv' -> 'm:102222I][contest:102222]\n\n\nObv'
en10 Английский tarjen 2022-05-01 13:25:59 0 (published)
en9 Английский tarjen 2022-05-01 13:25:32 208
en8 Английский tarjen 2022-05-01 13:21:39 126
en7 Английский tarjen 2022-05-01 13:16:46 1166
en6 Английский tarjen 2022-05-01 13:08:15 192
en5 Английский tarjen 2022-05-01 13:04:38 6
en4 Английский tarjen 2022-05-01 13:04:07 28 Tiny change: '\n\n[problem' -> '\n[problem'
en3 Английский tarjen 2022-05-01 13:02:31 104
en2 Английский tarjen 2022-05-01 13:01:06 30 Tiny change: '[contesthttpscodeforces.comgym102222]\n\n给定`n,' -> '[problem:102222I]\n\n给定`n,'
en1 Английский tarjen 2022-05-01 13:00:17 1193 Initial revision (saved to drafts)