Drop Voicing(2023牛客五一集训派对day5,D题)

10 篇文章 1 订阅
4 篇文章 0 订阅
文章讲述了Inakao的朋友Miyako如何通过Drop-2和Invert操作改变音乐和弦,目标是将无序排列变为有序1,2,...,n。问题转化为求解在最少的多drop操作下实现最大上升子序列。给出了一段C++代码来解决这个问题。
摘要由CSDN通过智能技术生成

Inaka composes music. Today's arrangement includes a chord of n notes that are pairwise distinct, represented by a permutation p1…n​ of integers from 1 to n (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​,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​,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,…,n, in the fewest number of multi-drops possible. Please help her find the number of multi-drops needed.

思路:

本题通过连续的Drop-2与Invert交换任意两个数字的位子,所以此题可以简化为区间最大上升子序列。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
 
const int N = 1e4 + 10;
 
int n,a[N],ans=1e9;
int f[N];
int work() {
    int res = 0;
    for(int i = 1; i <= n;i++){
        f[i] = 1;
        for(int j = 1;j<i;j++)
            if(a[i] > a[j]) f[i] = max(f[i],f[j] + 1);
        res = max(res, f[i]);
    }
    return res;
}    
void move() {
    int ans = a[1];
    for (int i = 1; i < n; i ++ )
        a[i] = a[i + 1];
    a[n] = ans;
}
 
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    for (int i = 1; i <= n; i ++ )
        ans = min(ans, n - work()), move();
    cout << ans;
}

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值