Escape Room (贪心)

Escape Room

利用 贪心排序,利用id还原真的是6

这结构体的x,y也用的很巧妙

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 100010
struct Node
{
    int x,y;
    int id;
};
Node xxx[MAXN];
int cmp1(Node a,Node b)
{
    if(a.x!=b.x)
    return a.x<b.x;
    return a.id<b.id;
}
int cmp2(Node a,Node b)
{
    return a.id<b.id;
}
int main()
{
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
    {
        scanf("%d",&xxx[i].x);
        xxx[i].id=i;
    }
    sort(xxx+1,xxx+n+1,cmp1);
    for(i=1;i<=n;i++)
    xxx[i].y=n-i+1;
    sort(xxx+1,xxx+n+1,cmp2);
    printf("%d",xxx[1].y);
    for(i=2;i<=n;i++)
    printf(" %d",xxx[i].y);
    printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/82083598