【迭代加深搜索】 Editing a Book

本文介绍了一种通过剪切和粘贴操作来重新排序乱序段落的算法,旨在找到最少的操作次数达到正确顺序。该算法通过递归深度优先搜索进行尝试,并设置剪枝条件以提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Editing a Book

You have n equal-length paragraphs numbered 1 to n. Now you want to arrange them in the order
of 1, 2, … , n. With the help of a clipboard, you can easily do this: Ctrl-X (cut) and Ctrl-V (paste)
several times. You cannot cut twice before pasting, but you can cut several contiguous paragraphs at
the same time - they’ll be pasted in order.
For example, in order to make {2, 4, 1, 5, 3, 6}, you can cut 1 and paste before 2, then cut 3 and
paste before 4. As another example, one copy and paste is enough for {3, 4, 5, 1, 2}. There are two
ways to do so: cut {3, 4, 5} and paste after {1, 2}, or cut {1, 2} and paste before {3, 4, 5}.

Input

The input consists of at most 20 test cases. Each case begins with a line containing a single integer n
(1 < n < 10), thenumber of paragraphs. The next line contains a permutation of 1, 2, 3, … , n. The
last case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and the minimal number of cut/paste operations.

Sample Input

6
2 4 1 5 3 6
5
3 4 5 1 2
0

Sample Output

Case 1: 2
Case 2: 1

【解析】

这道题应该也没有多深的难度。其特点是需要我们控制它搜索的深度。而其他的地方则是暴力枚举罢了。
AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 15
int n,a[MAXN];
int D(int a[])//判断错误点 
{
    int cnt=0;
    for(int i=0;i<n-1;i++)
        if(a[i]+1!=a[i+1]) cnt++;
    if(a[n-1] !=n) cnt++;
    return cnt;
}
bool dfs(int d,int maxd,int a[])
{
    int t=D(a);
    if(!t) return 1;
    if(d*3+t>maxd*3) return 0;//理想状态(如果我们已经改变的位置加上需要改变的超过了最大深度则失效)失效
    int Next[MAXN];
    int len;
    for(int i=1;i<=n;i++)//起点 
        for(int j=i;j<=n;j++)//终点 
            for(int k=1;k<i;k++)//插入点 
            {
                memcpy(Next,a,sizeof Next);
                len=j-i+1;
                for(int p=0;p<len;p++)
                    Next[k+p-1]=a[i+p-1];//复制 
                len=i-k;//n-j+i-1
                for(int p=0;p<len;p++)
                    Next[j-p-1]=a[i-p-2];
                if(dfs(d+1,maxd,Next)) return 1;
             } 
    return 0;
}
int solve()
{
    if(!D(a)) return 0;
    int max_ans=12;
    for(int maxd=1;maxd<max_ans;maxd++)
        if(dfs(0,maxd,a)) return maxd;
    return max_ans;
}
int main()
{
    int kase=0;
    while(scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
        printf("Case %d: %d\n",++kase,solve());
    }
    return 0;
}
### 基于扩散模型的无训练音频编辑框架 #### 概述 为了满足无需额外训练即可执行高质量音频编辑的需求,研究者们探索了一种基于预训练扩散模型的方法。这种方法允许直接操作音频信号,在不改变其基本结构的前提下调整特定属性,如音调、语速或背景噪声水平。 #### 实现方式 该框架的核心在于利用预先训练好的条件扩散模型作为基础架构[^3]。通过微调输入特征而不是重新训练整个网络权重来适应不同的编辑需求。具体而言: - **特征提取**:从原始音频文件中抽取梅尔频谱图或其他形式的时间频率表示; - **条件编码**:将目标修改指令转换成可以被扩散模型理解的形式; - **反向采样过程**:根据给定条件逐步逆推生成期望效果下的新音频样本; 值得注意的是,由于采用了已经充分优化过的大型语言模型作为后台支持,因此能够有效减少计算资源消耗并提高处理效率。 ```python import torch from diffusers import StableDiffusionPipeline, DDIMScheduler model_id = "path_to_pretrained_model" scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, num_train_timesteps=1000) pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler).to("cuda") def edit_audio(audio_input, condition): # 将音频转化为适合模型接收的数据格式 input_features = preprocess(audio_input) with torch.no_grad(): outputs = pipe(input_features, guidance_scale=7.5, eta=0.0, strength=condition) edited_audio = postprocess(outputs["sample"]) return edited_audio ``` 上述代码片段展示了一个简化版的操作流程,其中`preprocess()`函数负责准备合适的输入数据,而`postprocess()`则用来恢复最终输出为可播放的声音文件。 #### 应用场景 此类技术特别适用于那些希望快速获得定制化声音素材而不愿投入过多时间成本进行专门培训的人群,比如播客创作者或者短视频制作者等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值