问题 F: Escape Room

本文介绍了一道关于寻找最小子序列的逃出房间问题。任务是根据给定的每个位置开始的最长递增子序列长度,找出符合这些条件的字典序最小的排列。文章给出了两种解决方案,并附带了详细的代码实现。

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

问题 F: Escape Room

时间限制: 1 Sec  内存限制: 128 MB
提交: 63  解决: 27
[提交][状态][讨论版][命题人:admin]

题目描述

As you know, escape rooms became very popular since they allow you to play the role of a video game hero. One such room has the following quiz. You know that the locker password is a permutation of N numbers. A permutation of length N is a sequence of distinct positive integers, whose values are at most N. You got the following hint regarding the password - the length of the longest increasing subsequence starting at position i equals Ai. Therefore you want to find the password using these values. As there can be several possible permutations you want to find the lexicographically smallest one. Permutation P is lexicographically smaller than permutation Q if there is an index i such that Pi < Qi and Pj = Qj for all j < i. It is guaranteed that there is at least one possible permutation satisfying the above constraints. 
Can you open the door?

输入

The first line of the input contains one integer N (1≤N≤105).
The next line contains N space-separated integer Ai (1≤Ai≤N).
It’s guaranteed that at least one possible permutation exists.

输出

Print in one line the lexicographically smallest permutation that satisfies all the conditions.

样例输入

4
1 2 2 1

样例输出4 2 1 3


**************************************************************************
是一道关于最长上升子序列的思维题吧 就归到了dp里
题意:
给你n个数 是每个数从该位置到末位的最长上升子序列的最大长度
求一个符合的数列(如果多个输出最小)
//
例如样例输出 :4 2 1 3
(4的最长上升长度为1;2的最长上升长度为2;1的最长上升长度为2;3的最长上升长度为1)
即为样例输入:1 2 2 1

一开始的想法就是 1 2 2 1每次遍历最小的将4 3 2 1 顺序填入
但是每次都要遍历,我就试着同时遍历最大和最小,但还是tle了
我的tle错误代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
int main()
{
    int n;
    int a[100005]={0};
    int ans[100005]={0};
    scanf("%d",&n);
    int maxx = 0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>maxx)
            maxx = a[i];
    }
    int p=1,q=maxx; //min max order
    int cnt1=1,cnt2=n; // min max num
    while(1)
    {
        if(p>q)
            break;
        for(int i=0;i<n;i++)
        {
            if(a[i]==p)
            {
                ans[i]=cnt2;
                cnt2--;
            }
            if(a[n-1-i]==q)
            {
                ans[n-1-i]=cnt1;
                cnt1++;
            }
        }
//        for(int i=0;i<n;i++)
//        {
//            printf("%d ",ans[i]);
//        }
        //printf("\n");
        p++;
        q--;
    }
    for(int i=0;i<n-1;i++)
    {
        printf("%d ",ans[i]);
    }
    printf("%d",ans[n-1]);
}

然后队友想了一种算法:用三维的结构体

为了更容易明白 我写了组样例:1  1  2  3  3  2  1  1

第一步:将第一维的最大上升序列的下标记在第二维(用于记录原来的位置):

第一维x:     1  1  2  3  3  2  1  1

第二维num:1  2  3  4  5  6  7  8

按第一维排序:

x:        1  1  1  1  2  2  3  3

num:1  2  7  8  3  6  4  5

第三维:8  7  6  5  4  3  2  1(倒序填入对应的x)

为了将x变回原本的位置

将整个结构体按num再次排序

返回原本的顺序

x:     1  1  2  3  3  2  1  1

num:1  2  3  4  5  6  7  8

r:      8  7  4  2  1  3  6  5

输出的第三维就是答案

正确代码:

#include <sstream>
#include <iostream>
#include <string>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct po    //结构体
{
    int x,num,r;
};
bool cmp1(po a,po b)  
{
    if(a.x!=b.x)
        return a.x<b.x;
    return a.num<b.num;

}
bool cmp2(po a,po b)
{
    return a.num<b.num;
}
int main()
{
    struct po s[100008]= {0};
    int n,i,x,t;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&t);
        s[i].num=i;  //将原本顺序填入num中
        s[i].x=t;    
    }
    sort(s+1,s+1+n,cmp1);  //按x排
    int k=n;
    for(i=1; i<=n; i++)    //倒序填入
    {
        s[i].r=k;
        k--;
    }
    sort(s+1,s+1+n,cmp2);  //按num排序 使结构体恢复原本顺序
    for(i=1;i<n;i++)printf("%d ",s[i].r);  //输出 r 
    printf("%d\n",s[n].r);
    return 0;
}

 

 

 


  











      
   

转载于:https://www.cnblogs.com/hao-tian/p/8955063.html

``` import asyncio import re from wechaty import MessageType from wechaty.user import Message from bridge.context import ContextType from channel.chat_message import ChatMessage from common.log import logger from common.tmp_dir import TmpDir class aobject(object): """Inheriting this class allows you to define an async __init__. So you can create objects by doing something like `await MyClass(params)` """ async def __new__(cls, *a, **kw): instance = super().__new__(cls) await instance.__init__(*a, **kw) return instance async def __init__(self): pass class WechatyMessage(ChatMessage, aobject): async def __init__(self, wechaty_msg: Message): super().__init__(wechaty_msg) room = wechaty_msg.room() self.msg_id = wechaty_msg.message_id self.create_time = wechaty_msg.payload.timestamp self.is_group = room is not None if wechaty_msg.type() == MessageType.MESSAGE_TYPE_TEXT: self.ctype = ContextType.TEXT self.content = wechaty_msg.text() elif wechaty_msg.type() == MessageType.MESSAGE_TYPE_AUDIO: self.ctype = ContextType.VOICE voice_file = await wechaty_msg.to_file_box() self.content = TmpDir().path() + voice_file.name # content直接存临时目录路径 def func(): loop = asyncio.get_event_loop() asyncio.run_coroutine_threadsafe(voice_file.to_file(self.content), loop).result() self._prepare_fn = func else: raise NotImplementedError("Unsupported message type: {}".format(wechaty_msg.type())) from_contact = wechaty_msg.talker() # 获取消息的发送者 self.from_user_id = from_contact.contact_id self.from_user_nickname = from_contact.name # group中的from和to,wechaty跟itchat含义不一样 # wecahty: from是消息实际发送者, to:所在群 # itchat: 如果是你发送群消息,from和to是你自己和所在群,如果是别人发群消息,from和to是所在群和你自己 # 但这个差别不影响逻辑,group中只使用到:1.用from来判断是否是自己发的,2.actual_user_id来判断实际发送用户 if self.is_group: self.to_user_id = room.room_id self.to_user_nickname = await room.topic() else: to_contact = wechaty_msg.to() self.to_user_id = to_contact.contact_id self.to_user_nickname = to_contact.name if self.is_group or wechaty_msg.is_self(): # 如果是群消息,other_user设置为群,如果是私聊消息,而且自己发的,就设置成对方。 self.other_user_id = self.to_user_id self.other_user_nickname = self.to_user_nickname else: self.other_user_id = self.from_user_id self.other_user_nickname = self.from_user_nickname if self.is_group: # wechaty群聊中,实际发送用户就是from_user self.is_at = await wechaty_msg.mention_self() if not self.is_at: # 有时候复制粘贴的消息,不算做@,但是内容里面会有@xxx,这里做一下兼容 name = wechaty_msg.wechaty.user_self().name pattern = f"@{re.escape(name)}(\u2005|\u0020)" if re.search(pattern, self.content): logger.debug(f"wechaty message {self.msg_id} include at") self.is_at = True self.actual_user_id = self.from_user_id self.actual_user_nickname = self.from_user_nickname```你修改后的代码里class WechatyMessage(ChatMessage, metaclass=AsyncInitMeta): async def __init__(self, wechaty_msg: Message): super().__init__(wechaty_msg) 这里面async 的问题依然存在 还是报错
03-26
内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值