2 solutions

  • 0
    @ 2025-7-23 14:08:54
    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> PII;
    const int N=5010;
    PII a[N];
    bool cmp(PII a,PII b)
    {
       if(a.first>b.first)
       {
            return 1;
       }
       if(a.first==b.first&&a.second<b.second)
       {
            return 1;
       }
       return 0;
    }
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].second>>a[i].first;
        }
        sort(a+1,a+n+1,greater<PII>());
        int cnt=m*1.5;
        int res=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i].first>=a[cnt].first)
            {
                res++;
            }
        }
        cout<<a[cnt].first<<" "<<res<<endl;
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=res;i++)
        {
            cout<<a[i].second<<" "<<a[i].first<<endl;
        }
        return 0;
    }
    
    • 0
      @ 2024-6-6 10:56:48

      模拟排序(Onlognnlog^n)

      【算法分析】 将数据按照题目规则排序

      • 1.成绩从小到大
      • 2.成绩相同报名号从小到大

      找到分数相同的最后一个位置

      #include<bits/stdc++.h>
      using namespace std;
      struct Person{
          int id;
          int sc;
      };
      const int N=5010;
      Person p[N];
      bool cmp(Person a,Person b)
      {
          if(a.sc>b.sc)     return 1; //1.成绩从大到小 
          if(a.sc==b.sc&&a.id<b.id) return 1; //2.成绩相同报名号从小到大 
          return 0; 
      }
      int main()
      {
          int n,m;
          cin>>n>>m;
          for(int i=1;i<=n;i++)
              cin>>p[i].id>>p[i].sc;
          sort(p+1,p+n+1,cmp);
          int k=m*1.5;
          int t=k;
          while(t<=n&&p[k].sc==p[t].sc) t++; //走到第一个不相等的地方,或者越界 
          t--;  //回到相等的位置
          cout<<p[k].sc<<" "<<t<<endl;
          for(int i=1;i<=t;i++)
          {
              cout<<p[i].id<<" "<<p[i].sc<<endl;
          }
          return 0;
      }
      
      • 1

      Information

      ID
      426
      Time
      1000ms
      Memory
      128MiB
      Difficulty
      8
      Tags
      # Submissions
      13
      Accepted
      6
      Uploaded By