Roast Lezi to play chess

Insert picture description here

This is a sorting and optimization problem a bit similar to the Swiss round.
After reading the problem, I found that the absolute position of every two people who lose is the same
every time they win, and the absolute position of every two people is the same. So it can be optimized according to this found rule

#include<bits/stdc++.h>
using namespace std;
struct node
{
    
    
    int cs;
    int s;
    int h;
}a[650000];
bool cmp(node x,node y)
{
    
    
    if(x.cs==y.cs)return x.h<y.h;
    return x.cs>y.cs;
}
int main()
{
    
    
     
    int n;cin>>n;n*=2;
    for(int i=1;i<=n;i++)cin>>a[i].cs,a[i].h=i;
    for(int i=1;i<=n;i++)cin>>a[i].s;
    int m;cin>>m;
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=m;i++)
    {
    
       
        for(int j=1;j<=n;j+=2)
        {
    
      
           int tmp;
           if(a[j].s>a[j+1].s)a[j].cs++,tmp=j;
           else a[j+1].cs++,tmp=j+1;
           while(tmp>1&&(a[tmp].cs>a[tmp-1].cs||(a[tmp].cs==a[tmp-1].cs&&a[tmp].h<a[tmp-1].h)))//循环优化
           {
    
    
            swap(a[tmp],a[tmp-1]);
               tmp--;
           }
        }
    }
    for(int i=1;i<=n;i++)
    {
    
    if(a[i].h==1){
    
    cout<<i<<' '<<a[i].cs;return 0;}}
}

Guess you like

Origin blog.csdn.net/yhhy666/article/details/108222955