德才论问题运行超时,不清楚是代码有问题还是代码是正确的只不过排序的时间复杂度太高,求解。

问题遇到的现象和发生背景

img

img

img

问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
typedef struct stu
{
    int id;
    int de;
    int cai;
}stu;
void paixu(struct stu x[],int p)
{struct stu t;
    int i,j;
    for(i=0;i<p-1;i++)
    {
        for(j=0;j<p-1-i;j++)
        {
            if(x[j].de+x[j].cai==x[j+1].de+x[j+1].cai&&x[j].de<x[j+1].de)
            {
                t=x[j+1];x[j+1]=x[j];x[j]=t;
            }
        
            if(x[j].de+x[j].cai==x[j+1].de+x[j+1].cai&&x[j].de==x[j+1].de&&x[j].id>x[j+1].id)
            {
                t=x[j+1];x[j+1]=x[j];x[j]=t;
            }
            if(x[j].de+x[j].cai<x[j+1].de+x[j+1].cai)
            {
                t=x[j+1];x[j+1]=x[j];x[j]=t;
            }
        }
    }
}
int main()
{
    struct stu *a,*b,*c,*d;int n,di,gao,id,de,cai,i,i1=0,i2=0,i3=0,i4=0;
    a=(struct stu*)malloc(100000*sizeof(struct stu));
    b=(struct stu*)malloc(100000*sizeof(struct stu));
    c=(struct stu*)malloc(100000*sizeof(struct stu));
    d=(struct stu*)malloc(100000*sizeof(struct stu));
    scanf("%d%d%d",&n,&di,&gao);
    for(i=0;i<n;i++)
    {
        scanf("%d%d%d",&id,&de,&cai);
        if(de>=gao&&cai>=gao)
        {
            a[i1].id=id;a[i1].de=de;a[i1].cai=cai;i1++;
        }
        else {if(de>=gao&&cai<gao&&cai>=di)
        {
            b[i2].id=id;b[i2].de=de;b[i2].cai=cai;i2++;
        }
        else {if(de<gao&&cai<gao&&de>=cai&&cai>=di)
        {
            c[i3].id=id;c[i3].de=de;c[i3].cai=cai;i3++;
        }
        else {if(de>=di&&cai>=di)
        {
            d[i4].id=id;d[i4].de=de;d[i4].cai=cai;i4++;
        }}}}paixu(a,i1);paixu(b,i2);paixu(c,i3);paixu(d,i4);
    }printf("%d\n",i1+i2+i3+i4);
    for(i=0;i<i1;i++)
    {
        printf("%d %d %d\n",a[i].id,a[i].de,a[i].cai);
    }
    for(i=0;i<i2;i++)
    {
        printf("%d %d %d\n",b[i].id,b[i].de,b[i].cai);
    }
    for(i=0;i<i3;i++)
    {
        printf("%d %d %d\n",c[i].id,c[i].de,c[i].cai);
    }
    for(i=0;i<i4;i++)
    {
        printf("%d %d %d\n",d[i].id,d[i].de,d[i].cai);
    }
    return 0;
}

运行结果及报错内容

img

修改如下,供参考:

#include<stdio.h>
#include<stdlib.h>
typedef struct stu
{
    int id;
    int de;
    int cai;
}stu;
void paixu(struct stu x[], int p)
{
    struct stu t;
    int i, j;
    for (i = 0; i < p - 1; i++)
    {
        for (j = 0; j < p - 1 - i; j++)
        {
            if (x[j].de + x[j].cai == x[j + 1].de + x[j + 1].cai && x[j].de < x[j + 1].de)
            {
                t = x[j + 1]; x[j + 1] = x[j]; x[j] = t;
            }

            if (x[j].de + x[j].cai == x[j + 1].de + x[j + 1].cai && x[j].de == x[j + 1].de && x[j].id > x[j + 1].id)
            {
                t = x[j + 1]; x[j + 1] = x[j]; x[j] = t;
            }
            if (x[j].de + x[j].cai < x[j + 1].de + x[j + 1].cai)
            {
                t = x[j + 1]; x[j + 1] = x[j]; x[j] = t;
            }
        }
    }
}
int main()
{
    struct stu* a, * b, * c, * d; 
    int n, di, gao, id, de, cai, i, i1 = 0, i2 = 0, i3 = 0, i4 = 0;
    scanf("%d%d%d", &n, &di, &gao);
    a = (struct stu*)malloc(n * sizeof(struct stu));
    b = (struct stu*)malloc(n * sizeof(struct stu));
    c = (struct stu*)malloc(n * sizeof(struct stu));
    d = (struct stu*)malloc(n * sizeof(struct stu));
    for (i = 0; i < n; i++)
    {
        scanf("%d%d%d", &id, &de, &cai);
        if (de >= gao && cai >= gao)
        {
            a[i1].id = id; a[i1].de = de; a[i1].cai = cai; i1++;
        }
        else {
            if (de >= gao && cai < gao && cai >= di)
            {
                b[i2].id = id; b[i2].de = de; b[i2].cai = cai; i2++;
            }
            else {
                if (de < gao && cai < gao && de >= cai && cai >= di)
                {
                    c[i3].id = id; c[i3].de = de; c[i3].cai = cai; i3++;
                }
                else {
                    if (de >= di && cai >= di)
                    {
                        d[i4].id = id; d[i4].de = de; d[i4].cai = cai; i4++;
                    }
                }
            }
        }
    }
    paixu(a, i1); paixu(b, i2); paixu(c, i3); paixu(d, i4);
    printf("%d\n", i1 + i2 + i3 + i4);
    for (i = 0; i < i1; i++)
    {
        printf("%d %d %d\n", a[i].id, a[i].de, a[i].cai);
    }
    for (i = 0; i < i2; i++)
    {
        printf("%d %d %d\n", b[i].id, b[i].de, b[i].cai);
    }
    for (i = 0; i < i3; i++)
    {
        printf("%d %d %d\n", c[i].id, c[i].de, c[i].cai);
    }
    for (i = 0; i < i4; i++)
    {
        printf("%d %d %d\n", d[i].id, d[i].de, d[i].cai);
    }
    return 0;
}