警钟 and 求助
查看原帖
警钟 and 求助
587261
pengzixiang楼主2022/11/27 14:31

先比较学生的进入+答疑时间,若相等则比较出去的时间,但不知为何只能过一个点 代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
struct node{
    long long eAndS;//entering and solving
    long long O;//out

};
node students[1005];
long long times[1005];
int n,s,a,e;
long long sumTime;
long long Time;
bool compare(node n1,node n2){
    if(n1.eAndS<n2.eAndS){
        return n1.eAndS<n2.eAndS;
    }else if(n1.eAndS>n2.eAndS){
        return n1.eAndS<n2.eAndS;
    }else {
        return n1.O<n2.O;
    }

}
int main()
{
    cin >> n;
    students[0].eAndS=0;
    students[0].O=0;
    for(int i=1;i<=n;i++){
        cin >> s >> a >> e;
        students[i].eAndS=s+a;
        students[i].O=e;
    }
    sort(students+1,students+n+1,compare);
//    for(int i=1;i<=n;i++)printf("%lld %lld\n",students[i].eAndS,students[i].O);
    for(int i=1;i<=n;i++){
        Time+=students[i-1].O+students[i].eAndS;

        sumTime+=Time;
    }

    cout << sumTime << endl;
    return 0;
}

之后直接比较按三个事件的时间总和进行排序发现AC了,想来可能是由于限定的出去时间相较于进入和答疑时间太小了,因此没什么影响 改后的代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
struct node{
    long long eAndS;//entering and solving
    long long O;//out

};
node students[1005];
long long times[1005];
int n,s,a,e;
long long sumTime;
long long Time;
bool compare(node n1,node n2){
   long long  time1=n1.eAndS+n1.O;
   long long  time2=n2.eAndS+n2.O;
    return time1<time2;

}
int main()
{
    cin >> n;
    students[0].eAndS=0;
    students[0].O=0;
    for(int i=1;i<=n;i++){
        cin >> s >> a >> e;
        students[i].eAndS=s+a;
        students[i].O=e;
    }
    sort(students+1,students+n+1,compare);
//    for(int i=1;i<=n;i++)printf("%lld %lld\n",students[i].eAndS,students[i].O);
    for(int i=1;i<=n;i++){
        Time+=students[i-1].O+students[i].eAndS;

        sumTime+=Time;
    }

    cout << sumTime << endl;
    return 0;
}

不知有无大佬知道为什么第一种解法不能AC

2022/11/27 14:31
加载中...