B. MUH and Important Things(Codeforces Round #269)

探讨了为三只动物设计独特任务执行计划的问题,确保每只动物的任务执行顺序不同且符合任务难度约束。
B. MUH and Important Things
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are n tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.

Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all the n tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The larger number hi is, the more difficult the i-th task is.

Output

In the first line print "YES" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line "NO" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form.

If there are multiple possible answers, you can print any of them.

Sample test(s)
input
4
1 3 3 1
output
YES
1 4 2 3 
4 1 2 3 
4 1 3 2 
input
5
2 4 1 4 8
output
NO
Note

In the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.

In the second sample there are only two sequences of tasks that meet the conditions — [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector<int> son[2014];
int main()
{
    int n;
    scanf("%d",&n);
    int x;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        son[x].push_back(i);
    }
    int coun=0;
    int sign=0;
    for(int i=1;i<=2000;i++)
    {
        if(son[i].size()==2)
        {
            coun+=2;
        }
        if(son[i].size()>2)
        {
            sign=i;
            coun+=4;
            break;
        }
    }
    if(coun>=3)
    {
        printf("YES\n");
        int cr=1;
        for(int i=1;i<=2000;i++)
        {
            for(int j=0;j<son[i].size();j++)
            {
                if(cr)
                {
                    cr=0;
                    printf("%d",son[i][j]);
                }
               else
                   printf(" %d",son[i][j]);
            }
        }
        printf("\n");
        if(sign)
        {
            cr=1;
            for(int i=1;i<=2000;i++)
            {
            for(int j=0;j<son[i].size();j++)
            {
                if(i==sign&&(j==0||j==1))
                {
                    if(cr)
                   {
                    cr=0;
                    printf("%d",son[i][1-j]);
                   }
                   else
                    printf(" %d",son[i][1-j]);
                }
                else
                {
                if(cr)
                {
                    cr=0;
                    printf("%d",son[i][j]);
                }
               else
                    printf(" %d",son[i][j]);
                }
            }
           }
           printf("\n");
           cr=1;
            for(int i=1;i<=2000;i++)
            {
            for(int j=0;j<son[i].size();j++)
            {
                if(i==sign&&(j==0||j==1||j==2))
                {
                    if(cr)
                   {
                    cr=0;
                    printf("%d",son[i][2-j]);
                   }
                   else
                    printf(" %d",son[i][2-j]);
                }
                else
                {
                if(cr)
                {
                    cr=0;
                    printf("%d",son[i][j]);
                }
               else
                    printf(" %d",son[i][j]);
                }
            }
           }
           printf("\n");
        }
        else
        {
            int gt=0;
            cr=1;
            for(int i=1;i<=2000;i++)
            {
            for(int j=0;j<son[i].size();j++)
            {
                if(son[i].size()==2&&(j==0||j==1))
                {
                    gt=i;
                    if(cr)
                   {
                    cr=0;
                    printf("%d",son[i][1-j]);
                   }
                   else
                    printf(" %d",son[i][1-j]);
                }
                else
                {
                if(cr)
                {
                    cr=0;
                    printf("%d",son[i][j]);
                }
               else
                    printf(" %d",son[i][j]);
                }
            }
           }
           printf("\n");
           cr=1;
            for(int i=1;i<=2000;i++)
            {
            for(int j=0;j<son[i].size();j++)
            {
                if(son[i].size()==2&&(j==0||j==1)&&i!=gt)
                {

                    if(cr)
                   {
                    cr=0;
                    printf("%d",son[i][1-j]);
                   }
                   else
                    printf(" %d",son[i][1-j]);
                }
                else
                {
                if(cr)
                {
                    cr=0;
                    printf("%d",son[i][j]);
                }
               else
                    printf(" %d",son[i][j]);
                }
            }
           }
           printf("\n");
        }
    }
    else
    {
        printf("NO\n");
    }
    return 0;
}



我按照给出的代码修改了我的代码,现在提示我缩进有问题,请参考下列代码,完整的抄写一边,并给我完整的代码 #!/usr/bin/env python3 import rclpy from rclpy.node import Node from sensor_msgs.msg import Joy import simpleaudio as sa import time from collections import defaultdict, deque class JoyControlNode(Node): def __init__(self): super().__init__(&#39;joy_control_node&#39;) # 订阅手柄输入 self.subscription = self.create_subscription( Joy, &#39;/joy&#39;, self.joy_callback, 10 ) # 创建周期性定时器(1秒周期) self.timer = self.create_timer(1.0, self.timer_callback) self.counter = 0 # 音频播放管理 self.active_players = [] # 存储当前播放的音频对象 # 按键状态管理 self.key_history = defaultdict(lambda: deque(maxlen=2)) # 按键时序记录 self.active_combo = set() # 当前激活的组合键按键 self.combo_tracker = {} # 组合键检测状态 self.prev_buttons = None # 前一次按钮状态 # 时间阈值配置 (单位:秒) self.DETECTION_WINDOW = 1.5 # 按键检测窗口 self.DOUBLE_PRESS_THRESHOLD = 0.25 # 双击最大间隔 self.COMBO_WINDOW = 0.15 # 组合键检测时间窗 # 停止键定义 self.STOP_BUTTON = 7 # 33个指令的按键绑定配置 self.bindings = { # 单键绑定 0: "/home/booster/Workspace/展会/介绍/FH.wav", 1: "/home/booster/Workspace/展会/介绍/握手.wav", 2: "/home/booster/Workspace/展会/介绍/挥手.wav", 3: "/home/booster/Workspace/展会/介绍/FV.wav", 4: "/home/booster/Workspace/展会/介绍/MU.wav", 5: "/home/booster/Workspace/展会/介绍/MUH3.wav", 6: "/home/booster/Workspace/展会/介绍/PH4X.wav", 8: "/home/booster/Workspace/展会/介绍/企业介绍.wav", 9: "/home/booster/Workspace/展会/介绍/转场.wav", # 双击绑定 (0,0): "/home/booster/Workspace/展会/介绍/英文-FH.wav", # 组合键0+1 (3,3): "/home/booster/Workspace/展会/介绍/英文-FV.wav", # 组合键0+1 (4,4): "/home/booster/Workspace/展会/介绍/英文-MU.wav", # 组合键0+1 (5,5): "/home/booster/Workspace/展会/介绍/英文-MUH3.wav", # 组合键0+1 (6,6): "/home/booster/Workspace/展会/介绍/英文-PH4X.wav", # 组合键0+1 (8,8): "/home/booster/Workspace/展会/介绍/英文-企业介绍.wav", # 组合键0+1 (9,9): "/home/booster/Workspace/展会/介绍/英文-转场.wav", # 组合键0+1 # 组合键绑定 (4,0): "/home/booster/Workspace/展会/介绍/PMC.wav", (4,1): "/home/booster/Workspace/展会/介绍/S2H.wav", # 组合键0+1 (4,2): "/home/booster/Workspace/展会/介绍/SHXP.wav", # 组合键0+1 (4,3): "/home/booster/Workspace/展会/介绍/TCE.wav", # 组合键0+1 (4,8): "/home/booster/Workspace/展会/介绍/TH.wav", # 组合键0+1 (4,9): "/home/booster/Workspace/展会/介绍/XIO.wav", # 组合键0+1 (6,0): "/home/booster/Workspace/展会/介绍/英文-PMC.wav", # 组合键0+1 (6,1): "/home/booster/Workspace/展会/介绍/英文-S2H.wav", # 组合键0+1 (6,2): "/home/booster/Workspace/展会/介绍/英文-SHXP.wav", # 组合键0+1 (6,3): "/home/booster/Workspace/展会/介绍/英文-TCE.wav", # 组合键0+1 (6,8): "/home/booster/Workspace/展会/介绍/英文-TH.wav", # 组合键0+1 (6,9): "/home/booster/Workspace/展会/介绍/英文-XIO.wav", # 组合键0+1 (5,1): "/home/booster/Workspace/展会/介绍/英文-握手.wav", # 组合键0+1 (5,2): "/home/booster/Workspace/展会/介绍/英文-挥手.wav", # 组合键0+1 } # 按键检测定时器 self.detection_timer = None self.last_key_time = 0 # 最后按键时间 def is_new_press(self, current_buttons, button_index): """检测按键是否刚刚按下(上升沿检测)""" if self.prev_buttons is None: return False if button_index >= len(current_buttons) or button_index >= len(self.prev_buttons): return False return current_buttons[button_index] == 1 and self.prev_buttons[button_index] == 0 def is_new_release(self, current_buttons, button_index): """检测按键是否刚刚释放(下降沿检测)""" if self.prev_buttons is None: return False if button_index >= len(current_buttons) or button_index >= len(self.prev_buttons): return False return current_buttons[button_index] == 0 and self.prev_buttons[button_index] == 1 def play_sound(self, file_path): """非阻塞播放音频文件""" try: # 清理已完成播放的音频 self.cleanup_players() wave_obj = sa.WaveObject.from_wave_file(file_path) play_obj = wave_obj.play() self.active_players.append(play_obj) self.get_logger().info(f"播放音频: {file_path}") except Exception as e: self.get_logger().error(f"播放失败: {e}") def stop_all_sounds(self): """停止所有音频播放""" for player in self.active_players: player.stop() self.active_players = [] self.get_logger().info("所有音频已停止") def cleanup_players(self): """清理已完成播放的音频线程""" self.active_players = [p for p in self.active_players if p.is_playing()] def reset_all_states(self): """重置所有按键状态""" self.key_history.clear() self.active_combo.clear() self.combo_tracker.clear() # 重置定时器 if self.detection_timer: self.detection_timer.cancel() self.detection_timer = None def timer_callback(self): self.counter += 1 self.get_logger().info(f&#39;定时触发 #{self.counter}&#39;) if self.counter >= 5: self.timer.cancel() # 5次后停止定时器 def detection_timeout(self): """检测窗口超时处理""" self.get_logger().info("检测窗口超时,处理按键事件") # 处理所有按键事件 self.process_key_events() # 重置状态 self.reset_all_states() self.detection_timer = None def process_key_events(self): """处理累积的按键事件""" # 1. 优先级2: 双击检测 for key, times in self.key_history.items(): if len(times) == 2: time_diff = times[1] - times[0] if time_diff < self.DOUBLE_PRESS_THRESHOLD: double_key = (key, key) if double_key in self.bindings: self.play_sound(self.bindings[double_key]) return # 高优先级触发,停止后续检测 # 2. 优先级3: 组合键检测 for combo in list(self.combo_tracker.keys()): if combo in self.bindings: self.play_sound(self.bindings[combo]) return # 组合键触发,停止后续检测 # 3. 优先级4: 单键检测 for key, times in self.key_history.items(): if key in self.bindings: self.play_sound(self.bindings[key]) return # 单键触发 def joy_callback(self, msg): current_time = time.time() buttons = msg.buttons # 初始化前次按钮状态 if self.prev_buttons is None: self.prev_buttons = [0] * len(buttons) # === 1. 最高优先级: 停止键处理 === if self.is_new_press(buttons, self.STOP_BUTTON): self.stop_all_sounds() self.reset_all_states() self.prev_buttons = buttons return # 检测按键事件 any_key_pressed = False # === 2. 组合键检测 === active_keys = [i for i, state in enumerate(buttons) if state == 1 and i != self.STOP_BUTTON] # 更新组合键状态 for combo in [k for k in self.bindings if isinstance(k, tuple) and len(k) == 2]: if all(buttons[k] == 1 for k in combo): if combo not in self.combo_tracker: self.combo_tracker[combo] = current_time self.active_combo.update(combo) any_key_pressed = True else: if combo in self.combo_tracker: del self.combo_tracker[combo] self.active_combo.difference_update(combo) # === 3. 单键/双击检测 === for key in range(len(buttons)): if key == self.STOP_BUTTON or key in self.active_combo: continue # 按键按下事件检测 if self.is_new_press(buttons, key): self.key_history[key].append(current_time) any_key_pressed = True # 启动检测定时器 if any_key_pressed: self.start_detection_timer() self.last_key_time = current_time # 更新按钮状态 self.prev_buttons = buttons self.cleanup_players() def main(args=None): rclpy.init(args=args) node = JoyControlNode() try: rclpy.spin(node) except KeyboardInterrupt: pass finally: node.destroy_node() rclpy.shutdown() if __name__ == &#39;__main__&#39;: main()
最新发布
09-19
运行后报错:&#39;JoyControlNode&#39; object has no attribute &#39;active_players&#39; #!/usr/bin/env python3 import rclpy from rclpy.node import Node from sensor_msgs.msg import Joy import simpleaudio as sa import time from collections import defaultdict, deque class JoyControlNode(Node): def __init__(self): super().__init__(&#39;joy_control_node&#39;) self.subscription = self.create_subscription( Joy, &#39;/joy&#39;, self.joy_callback, 10 ) # 状态管理容器 self.key_history = defaultdict(lambda: deque(maxlen=2)) # 按键时序记录 self.active_combo = set() # 当前激活的组合键按键 self.combo_tracker = {} # 组合键检测状态 self.prev_buttons = None # 前一次按钮状态 # 时间阈值配置 (单位:秒) self.DOUBLE_PRESS_THRESHOLD = 0.25 # 双击最大间隔 self.COMBO_WINDOW = 0.15 # 组合键检测时间窗 # 按键绑定配置(根据实际需求修改路径) self.STOP_BUTTON = 7 self.bindings = { # 单键绑定 0: "/path/FH.wav", 1: "/path/握手.wav", 2: "/path/挥手.wav", 3: "/path/FV.wav", 4: "/path/MU.wav", 5: "/path/MUH3.wav", 6: "/path/PH4X.wav", 8: "/path/企业介绍.wav", 9: "/path/转场.wav", # 双击绑定 (0,0): "/path/英文-FH.wav", (1,1): "/path/英文-握手.wav", (2,2): "/path/英文-挥手.wav", (3,3): "/path/英文-FV.wav", (4,4): "/path/英文-MU.wav", (5,5): "/path/英文-MUH3.wav", (6,6): "/path/英文-PH4X.wav", (8,8): "/path/英文-企业介绍.wav", (9,9): "/path/英文-转场.wav", # 组合键绑定 (4,0): "/path/PMC.wav", (4,1): "/path/S2H.wav", (4,2): "/path/SHXP.wav", (4,3): "/path/TCE.wav", (4,8): "/path/TH.wav", (4,9): "/path/XIO.wav", (6,0): "/path/英文-PMC.wav", (6,1): "/path/英文-S2H.wav", (6,2): "/path/英文-SHXP.wav", (6,3): "/path/英文-TCE.wav", (6,8): "/path/英文-TH.wav", (6,9): "/path/英文-XIO.wav", (5,1): "/path/英文-握手.wav", (5,2): "/path/英文-挥手.wav", } def is_new_press(self, current_buttons, button_index): """检测按键是否刚刚按下(上升沿检测)""" if not hasattr(self, &#39;prev_buttons&#39;) or not self.prev_buttons: return False if button_index >= len(current_buttons) or button_index >= len(self.prev_buttons): return False return current_buttons[button_index] == 1 and self.prev_buttons[button_index] == 0 def is_new_release(self, current_buttons, button_index): """检测按键是否刚刚释放(下降沿检测)""" if not hasattr(self, &#39;prev_buttons&#39;) or not self.prev_buttons: return False if button_index >= len(current_buttons) or button_index >= len(self.prev_buttons): return False return current_buttons[button_index] == 0 and self.prev_buttons[button_index] == 1 def play_sound(self, file_path): """非阻塞播放音频文件""" try: wave_obj = sa.WaveObject.from_wave_file(file_path) play_obj = wave_obj.play() self.active_players.append(play_obj) self.get_logger().info(f"播放音频: {file_path}") except Exception as e: self.get_logger().error(f"播放失败: {e}") def stop_all_sounds(self): """停止所有音频播放""" for player in self.active_players: player.stop() self.active_players = [] self.get_logger().info("所有音频已停止") def cleanup_players(self): """清理已完成播放的音频线程""" self.active_players = [p for p in self.active_players if p.is_playing()] def reset_all_states(self): """重置所有按键状态""" self.key_history.clear() self.active_combo.clear() self.combo_tracker.clear() def joy_callback(self, msg): current_time = time.time() buttons = msg.buttons # 初始化解锁状态 if self.prev_buttons is None: self.prev_buttons = [0] * len(buttons) # === 1. 停止键处理 (最高优先级) === if self.is_new_press(buttons, self.STOP_BUTTON): self.stop_all_sounds() self.reset_all_states() self.prev_buttons = buttons return # === 2. 组合键检测 (第二优先级) === active_keys = [i for i, state in enumerate(buttons) if state == 1 and i != self.STOP_BUTTON] for combo in [k for k in self.bindings if isinstance(k, tuple)]: if all(buttons[k] == 1 for k in combo): if combo not in self.combo_tracker: self.combo_tracker[combo] = current_time self.active_combo.update(combo) pressed_duration = current_time - self.combo_tracker[combo] if pressed_duration > self.COMBO_WINDOW: self.play_sound(self.bindings[combo]) self.combo_tracker.pop(combo, None) # 组合键优先,阻止其他检测 else: self.combo_tracker.pop(combo, None) self.active_combo.difference_update(combo) # === 3. 双击/单键检测 (第三优先级) === for key in range(len(buttons)): if key == self.STOP_BUTTON or key in self.active_combo: continue # 按键按下事件检测 if self.is_new_press(buttons, key): self.key_history[key].append(current_time) # 双击检测 if len(self.key_history[key]) == 2: time_diff = self.key_history[key][1] - self.key_history[key][0] if time_diff < self.DOUBLE_PRESS_THRESHOLD: double_key = (key, key) if double_key in self.bindings: self.play_sound(self.bindings[double_key]) self.key_history[key].clear() continue # 按键释放事件检测 if self.is_new_release(buttons, key): if key in self.bindings and len(self.key_history[key]) == 1: elapsed = current_time - self.key_history[key][0] if elapsed > self.DOUBLE_PRESS_THRESHOLD: self.play_sound(self.bindings[key]) # 清除历史记录 self.key_history[key].clear() # 更新按钮状态 self.prev_buttons = buttons self.cleanup_players() def main(args=None): rclpy.init(args=args) node = JoyControlNode() rclpy.spin(node) node.destroy_node() rclpy.shutdown() if __name__ == &#39;__main__&#39;: main()
09-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值