2994. 数组排序

Canis

include

include

include

include

include

using namespace std;

int main()
{
int t;
cin >> t;
for(int q = 0;q < t; q++)
{
int n;
cin >> n;
int a[500] = {0};//桶排序,下标是数,a[i]是数i出现的次数

        for(int i = 0; i < n; i++)
        {
            int temp;
            cin >> temp;
            a[temp]++;
        }
        int cnt = 0;
        cout << "case #" << q << ':'<< endl;
        while(cnt < n)
        {
            int max = 0,maxindex = 0;
            for(int i = 0; i < 500; i++)
            {
                if(a[i] > max)
                {
                    maxindex = i;//maxindex是出现次数最多的元素的下标
                    max = a[i]; // max是出现次数最多的元素的出现次数
                }
            }
            for(int i = 0; i < max; i++,cnt++)
            {
                cout << maxindex;
                if (cnt != n - 1)
                    cout << ' ';
                else
                    cout << endl;
            }
            a[maxindex] = -1;//由最大变最小
        }
    }




return 0;

}
桶排序了解一下?

spongebob

Python 3

N = int(input())
for case_n in range(N):
    num = int(input())
    ans = list(map(int, input().split()))

    counts = {}

    for item in ans:
        counts[item] = counts[item] + 1 if item in counts else 0

    ans = sorted(ans, key=lambda x: (-counts[x], x))

    print(f"case #{case_n}:")
    print(" ".join(map(str,ans)))
Fifnmar

用两个键比较即可。如果数据量较大,可能不得不借助 map 等设施。但这道题很小所以直接调用 find

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    uint32_t t;
    cin >> t;
    for (uint32_t query = 0; query < t; ++query) {
        struct Num {
            uint32_t val;
            uint32_t cnt;
            bool operator==(uint32_t val) const { return val == this->val; }
        };
        vector<Num> nums;
        uint32_t n; cin >> n;
        for (uint32_t i = 0; i < n; ++i) {
            uint32_t temp; cin >> temp;
            auto ptr = find(nums.begin(), nums.end(), temp);
            if (ptr == nums.end())
                nums.push_back({temp, 1});
            else
                ++ptr->cnt;
        }
        sort(nums.begin(), nums.end(), [](Num a, Num b) {
            if (a.cnt == b.cnt)
                return a.val < b.val;
            return a.cnt > b.cnt;
        });
        cout << "case #" << query << ":\n";
        for (auto i : nums)
            for (uint32_t j = 0; j < i.cnt; ++j)
                cout << i.val << ' ';
        cout.put('\n');
    }
}
Li Dao

排序题,次数用cnt数组来统计,类似于基数排序

include

using namespace std;
int T,n;
struct Num
{
int ci,val;
};
vector V;
int cnt[600];
int cmp(Num aa,Num bb)
{
if(aa.ci!=bb.ci) return aa.ci>bb.ci;
else return aa.val>n;
V.clear();
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
{
int xx;cin>>xx;
V.push_back((Num){0,xx});
cnt[xx]++;
}
for(int i=0;i<n;i++)
V[i].ci=cnt[V[i].val];
sort(V.begin(),V.end(),cmp);
cout<<V[0].val;
for(int i=1;i<V.size();i++) cout<<” “<<V[i].val;
cout<>T;
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

一剑无痕雪满山

2994试了很多数据输出都对 但还是WA 大神求教TAT
窝就是借鉴了你的。。我的代码发到 泥的OJ邮箱里了希望有帮助
柑橘复杂化了

你当前正在回复 博客/题目
存在问题!