1 条题解

  • 0
    @ 2024-11-19 18:01:03

    按颜色分类开 nnvectorvector 分别存在原序列中的位置和权值,然后把权值排个序,再按照原序列的位置放回原序列,最后输出。

    #include <bits/stdc++.h>
    
    using i64 = long long;
    using u32 = unsigned;
    using u64 = unsigned long long;
    
    int main() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    
        int n;
        std::cin >> n;
    
        std::vector<int> c(n);
        for (int i = 0; i < n; ++i) {
        	std::cin >> c[i];
        }
    
        std::vector<std::vector<int>> f(n + 1), pos(n + 1);
        for (int i = 0; i < n; ++i) {
        	int a;
        	std::cin >> a;
        	f[c[i]].emplace_back(a);
        	pos[c[i]].emplace_back(i);
        }
    
        std::vector<int> res(n);
        for (int i = 1; i <= n; ++i) {
        	std::sort(f[i].begin(), f[i].end(), [&](auto u, auto v) {
        		return u < v;
        	});
    
        	for (int j = 0; j < pos[i].size(); ++j) {
        		res[pos[i][j]] = f[i][j];
        	}
        }
    
        for (auto i : res) {
        	std::cout << i << " ";
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    139
    时间
    2000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    82
    已通过
    24
    上传者