2947. 行数据的排序

CCXXXI_

Life is short


def f():
    return [int(x) for x in input().split()[:-1]]

for _ in range(int(input())):
    for a in sorted([f() for _ in range(int(input()))], reverse=True):
        print(*a)
徐摆渡
#include <bits/stdc++.h>
using namespace std;
//sorting of line data
//similar to dictionary-order sorting

int T, N;
const int n=1001;
struct data
{
    int cnt;
    int a[n];
}d[n];

bool cmp(data x, data y)
{
    //similar to sorting by dictionary-order(keep the head aligned and sort from head to tail)
    int i,j;
    int lenx=x.cnt, leny=y.cnt;
    int len=min(lenx,leny);
    for(i=0;i<len;++i){
        if(x.a[i]==y.a[i]) continue;
        return x.a[i]>y.a[i];
    }
    return lenx>leny;
}
int main()
{
    cin>>T;
    while(T--)
    {
        cin>>N;
        int i,j,temp;
        for(i=0;i<N;++i){
            j=0;//put j=0 int the first layer loop
            while(cin>>temp&&temp!=-1){
                d[i].a[j]=temp;
                j++;
            }
            d[i].cnt=j;
        }

        sort(d,d+N,cmp);

        for(i=0;i<N;++i){
            for(j=0;j<d[i].cnt;++j)
                printf("%d%c",d[i].a[j]," \n"[j==d[i].cnt-1]);
        }

    }
    return 0;
}
CatFlowers

结构体里的数组本身不是乱序的么?

我太难了

实际上直接利用vector的大小比较就好

#include <bits/stdc++.h>
using namespace std;

bool cmp(vector<int> a,vector<int> b){
    return a > b;
}

int main()
{
    ios::sync_with_stdio(false);    
    cin.tie(0);
    int t, x;
    cin >> t;
    for (int i = 0; i < t;i++){
        int n;
        cin >> n;
        vector<int> p[n];
        for (int j = 0; j < n;j++){
            cin >> x;
            while(x!=-1){
                p[j].push_back(x);
                cin >> x;
            }
        }
        sort(p, p + n, cmp);
        for (int j = 0; j < n;j++){
            for (int k = 0; k < p[j].size();k++){
                cout << p[j][k] << " ";
            }
            cout << endl;
        }
    }

        return 0;
}
zzpzbl

这道题啊有点怪

CatFlowers
#include<iostream>
#include<algorithm>
#define MAXN 51
#define MAXK 1001
using namespace std;

struct my_sort{
  int num;
  int a[MAXN];
}s[MAXK];

bool cmp(my_sort p1, my_sort p2)
{
  for(int i = 0; i < min(p1.num, p2.num); ++i)
  {
    if(p1.a[i] != p2.a[i])
        return p1.a[i] > p2.a[i];
  }
  return p1.num > p2.num;
}

int main()
{
  int T,N;
  cin >> T;
  while(T--)
  {
    cin >> N;
    int t, i, j;
    for(i = 0; i < N; ++i)
    {
      j = 0;
      while(cin>>t&&t!=-1){
                s[i].a[j]=t;
                j++;
      }
      s[i].num = j;
    }
      sort(s, s+N, cmp);
      for(i = 0; i < N; ++i)
      {
        for(int j = 0; j < s[i].num; ++j)
          cout << s[i].a[j] << ' ';
        cout << endl;
      }
  }
  return 0;
}

不太明白,为什么用了sort()以后,结构体里面的数组会自动降序处理?

Fifnmar

non-trival 类型进行赋值和拷贝是错误的。数组不是 TriviallyCopiable。如果要不出错,只能自定义相关的函数。

解决方法是使用 array 代替内置数组。

CatFlowers

学弟厉害了。

Fifnmar
int main() {
    using namespace std;
    uint32_t t; cin >> t;
    for (uint32_t query = 0; query < t; ++query) {
        int32_t n; cin >> n;
        vector<vector<int32_t>> nums(n);
        for (auto &i : nums) {
            for (int32_t temp; cin >> temp, temp != -1;) {
                i.push_back(temp);
            }
        }
        vector<uint32_t> indices(n);
        iota(indices.begin(), indices.end(), (uint32_t)0);
        sort(indices.begin(), indices.end(), [&nums](uint32_t a, uint32_t b) {
            uint32_t sz = std::min(nums[a].size(), nums[b].size());
            for (uint32_t i = 0; i < sz; ++i) {
                if (nums[a][i] == nums[b][i]) {
                    continue;
                } else {
                    return nums[a][i] > nums[b][i];
                }
            }
            return nums[a].size() > nums[b].size();
        });
        for (auto &i : indices) {
            if (!nums[i].empty()) {
                cout << nums[i][0];
                for (auto iter = nums[i].begin() + 1, ed = nums[i].end(); iter != ed; ++iter) {
                    cout << ' ' << *iter;
                }
            }
            cout.put('\n');
        }
    }
}
你当前正在回复 博客/题目
存在问题!