2 solutions

  • 1
    @ 2026-1-18 15:19:49

    季总写法:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1010;
    struct Node
    {
        int id;
        int t;
    }p[N];
    bool cmp(Node a,Node b)
    {
        if(a.t<b.t) return 1;
        if(a.t==b.t&&a.id<b.id) return 1;
        return 0;
    } 
    int main()
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>p[i].t;
            p[i].id=i+1;
        }
        sort(p,p+n,cmp);
        long long s=0;
        for(int i=0;i<n;i++)
        {
            cout<<p[i].id<<" ";
            s=s+(n-1-(i+1)+1)*p[i].t;
        }
        cout<<endl;
        printf("%.2lf",s*1.0/n);
    }
    • 1
      @ 2026-1-18 15:18:19

      简易写法:

      #include<bits/stdc++.h>
      using namespace std;
      int n;
      struct Node{
      	int idx;
      	int time;
      }arr[1010];
      bool cmp(Node a,Node b){
      	if(a.time<b.time) return 1;
      	else if(a.time==b.time&&a.idx<b.idx) return 1;
      	else return 0;
      }
      int main(){
      	cin>>n;
      	for(int i=1;i<=n;i++){
      		cin>>arr[i].time;
      		arr[i].idx=i;
      	}
      	sort(arr+1,arr+n+1,cmp);
      	double cnt=0;
      	for(int i=1;i<=n;i++){
      		cnt=cnt+(n-i)*arr[i].time;
      		cout<<arr[i].idx<<' ';
      	}
      	cout<<endl<<fixed<<setprecision(2)<<cnt/n;
      	return 0;
      }
      • 1

      Information

      ID
      1018
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      (None)
      # Submissions
      46
      Accepted
      15
      Uploaded By