1. 程式人生 > 實用技巧 >牛客網Drop Voicing

牛客網Drop Voicing

題目描述:

Inaka composes music. Today's arrangement includes a chord of nnn notes that are pairwise distinct, represented by a permutation p1…np_{1 \dots n}p1n of integers from 111 to nnn (inclusive) denoting the notes from the lowest to the highest.

Her friend, Miyako, sneaks in and plays a trick by altering the chord with the following two operations:

  • Drop-2: Take out the second highest note and move it to the lowest position, i.e. change the permutation to pn−1,p1,p2,…,pn−3,pn−2,pnp_{n-1}, p_1, p_2, \dots, p_{n-3}, p_{n-2}, p_npn1,p1,p2,,pn3,pn2,pn.
  • Invert: Take out the lowest note and move it to the highest position, i.e. change the permutation to p2,p3,…,pn−1,pn,p1p_2, p_3, \dots, p_{n-1}, p_n, p_1p2,p3,,pn1,pn,p1.

Any number of consecutive Drop-2 operations is considered a multi-drop. Miyako would like to change the permutation to an ordered permutation, 1,2,…,n1, 2, \dots, n1,2,,n, in the fewest number of multi-drops possible. Please help her find the number of multi-drops needed.

輸入描述:

輸出描述:

列式1:

列式2:

:題面翻譯自行解決

  這題我們將模型轉換成有一個圓盤,圓盤上有n個位置,且有一個指標(初始時指標指向原 來的最後一個元素)

  • 連續若干次操作1等價為改變指標指向的數所處的位置連續若干次操作
  • 連續若干次操作2等價為改變指標的位置

  這樣以後我們就可以得出答案是在這個環上取一個最長不下降子序列,在此之後調整其他數即可AC。

程式碼如下:

#include<bits/stdc++.h>
using namespace std;
int n,a[1007],dp[1007];
int main()
{
    int cnt=0;
    cin>>n;
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    for (int k=1; k<=n; k++)
    {
        for (int i=1; i<=n; i++)
        {
            dp[i]=1;
            for (int j=1; j<=i; j++)
                if (a[i]>a[j]) dp[i]=max(dp[i],dp[j]+1);//求最長不下降子序列 
            cnt=max(dp[i],cnt);
        }
        int t=a[1];
        for(int i=1;i<n;i++) a[i]=a[i+1]; a[n]=t;//圈圈轉一位 
    }
    cout<<n-cnt<<endl;
    return 0;//隨手帶上結束 
}