Preparing for Merge Sort CodeForces - 847B

Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.

Ivan represent his array with increasing sequences with help of the following algorithm.

While there is at least one unused number in array Ivan repeats the following procedure:

  • iterate through array from the left to the right;
  • Ivan only looks at unused numbers on current iteration;
  • if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.

For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one — [2, 4].

Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of elements in Ivan's array.

The second line contains a sequence consisting of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — Ivan's array.

Output

Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.

Examples

Input

5
1 3 2 5 4

Output

1 3 5 
2 4 

Input

4
4 3 2 1

Output

4 
3 
2 
1 

Input

4
10 30 50 101

Output

10 30 50 101 

题意

给你N个不同的数 让你最小行 每行为递增数组

思路

所有行最后一个数是按递减的,我们可以反向存,那么就是递增的,就可以用二分了,开一个数组只存每行的最后一个,二分查找当前数第一个大于他的,把他放在前一行

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 200005;
int a[maxn];
vector<int> v[maxn];
int main()
{
    int n,i,j,m,x;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x);
        int it = lower_bound(a,a+n+1,x) - a -1;
        //cout<<it<<endl;
        a[it] = x;
        v[it].push_back(x);
    }
    for(i=n;i>=0;i--)
    {
        int l = v[i].size();
        if(l)
        {
            for(j=0;j<l;j++)
            {
                if(j) printf(" ");
                printf("%d",v[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

root@autodl-container-46894b864e-f6b4fda5:~/EGE-UNet-main# python train.py #----------Creating logger----------# #----------GPU init----------# #----------Preparing dataset----------# Traceback (most recent call last): File "train.py", line 185, in <module> main(config) File "train.py", line 60, in main val_dataset = NPY_datasets(config.data_path, config, train=False) File "/root/EGE-UNet-main/datasets/dataset.py", line 29, in __init__ mask_path = path_Data+'val/masks/' + masks_list[i] IndexError: list index out of rangeroot@autodl-container-46894b864e-f6b4fda5:~/EGE-UNet-main# python train.py #----------Creating logger----------# #----------GPU init----------# #----------Preparing dataset----------# Traceback (most recent call last): File "train.py", line 185, in <module> main(config) File "train.py", line 60, in main val_dataset = NPY_datasets(config.data_path, config, train=False) File "/root/EGE-UNet-main/datasets/dataset.py", line 29, in __init__ mask_path = path_Data+'val/masks/' + masks_list[i] IndexError: list index out of rangeroot@autodl-container-46894b864e-f6b4fda5:~/EGE-UNet-main# python train.py #----------Creating logger----------# #----------GPU init----------# #----------Preparing dataset----------# Traceback (most recent call last): File "train.py", line 185, in <module> main(config) File "train.py", line 60, in main val_dataset = NPY_datasets(config.data_path, config, train=False) File "/root/EGE-UNet-main/datasets/dataset.py", line 29, in __init__ mask_path = path_Data+'val/masks/' + masks_list[i] IndexError: list index out of rangeroot@autodl-container-46894b864e-f6b4fda5:~/EGE-UNet-main# python train.py #----------Creating logger----------# #----------GPU init----------# #----------Preparing dataset----------# Traceback (most recent call last): File "train.py", line 185, in <module> main(config) File "train.py", line 60, in main val_dataset = NPY_datasets(config.data_path, config, train=False) File "/root/EGE-UNet-main/datasets/dataset.py", line 29, in __init__ mask_path = path_Data+'val/masks/' + masks_list[i] IndexError: list index out of range
07-31
你遇到的错误是: ``` IndexError: list index out of range ``` 出现在验证集加载阶段的这一行代码中: ```python mask_path = path_Data + 'val/masks/' + masks_list[i] ``` --- ### 🔍 错误原因分析: 这个错误说明你正在尝试访问 `masks_list` 列表中一个**不存在的索引 `i`**,常见原因如下: #### ❌ 原因 1:`masks_list` 为空 ```python masks_list = sorted(glob.glob(path_Data + 'val/masks/*.npy')) ``` - 如果 `path_Data + 'val/masks/'` 路径下**没有 `.npy` 文件**,`masks_list` 就是空列表。 - 后续使用 `masks_list[i]` 就会报错。 ✅ **解决方法**:检查路径是否存在 `.npy` 文件。 ```python import os print("验证集掩码路径是否存在?", os.path.exists(path_Data + 'val/masks/')) print("掩码文件列表:", masks_list) ``` --- #### ❌ 原因 2:`images_list` 和 `masks_list` 长度不一致 如果: ```python images_list = sorted(glob.glob(path_Data + 'val/images/*.npy')) masks_list = sorted(glob.glob(path_Data + 'val/masks/*.npy')) ``` 这两个列表长度不一样,遍历时就会出现索引越界。 ✅ **解决方法**:添加断言确保数量一致。 ```python assert len(images_list) == len(masks_list), \ f"验证集图像和掩码数量不一致:{len(images_list)} vs {len(masks_list)}" ``` --- #### ❌ 原因 3:路径拼接错误 如果你的 `path_Data` 没有正确设置,可能导致路径拼接错误,找不到文件。 ✅ **建议**:使用 `os.path.join()` 安全拼接路径: ```python mask_path = os.path.join(path_Data, 'val', 'masks', masks_list[i]) ``` --- ### ✅ 修改建议(修改 `dataset.py`) ```python import glob import os from torch.utils.data import Dataset import numpy as np class NPY_datasets(Dataset): def __init__(self, path_Data, config, train=True): if train: self.path = os.path.join(path_Data, 'train') else: self.path = os.path.join(path_Data, 'val') self.images = sorted(glob.glob(os.path.join(self.path, 'images', '*.npy'))) self.masks = sorted(glob.glob(os.path.join(self.path, 'masks', '*.npy'))) # 检查数量是否一致 assert len(self.images) == len(self.masks), \ f"图像和掩码数量不一致:{len(self.images)} vs {len(self.masks)}" print(f"加载了 {len(self.images)} 个验证/训练样本") def __len__(self): return len(self.images) def __getitem__(self, idx): image_path = self.images[idx] mask_path = self.masks[idx] image = np.load(image_path) mask = np.load(mask_path) return image, mask ``` --- ### ✅ 补充建议 | 操作 | 命令 | |------|------| | 查看目录下 `.npy` 文件数量 | `ls -l *.npy | wc -l` | | 查看验证集路径是否正确 | 在代码中打印路径 | | 确保图像和掩码文件名一一对应 | 使用 `sorted()` 保持顺序一致 | --- ### ✅ 总结 | 问题 | 原因 | 解决方法 | |------|------|-----------| | `IndexError: list index out of range` | `masks_list` 为空或长度不一致 | 检查路径、打印列表、添加断言 | | 图像路径错误 | 拼接方式不规范 | 使用 `os.path.join()` | | 数据集加载失败 | 文件缺失或命名不一致 | 手动检查 `.npy` 文件 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值