woo960131   7년 전

중복되는 원소에 대한 처리도 되어 있는 것 같은데 틀렸다고 하네요 ㅠㅠ

제가 놓치고 있는 부분이 뭘까요

rhs0266   7년 전

사전순~

rhs0266   7년 전

#include <iostream>
#include <algorithm>
#include <cmath>
  
using namespace std;
  
typedef struct {
    int value, index, rank;
} node;
  
bool cmpvalue(node a, node b) {
    if (a.value!=b.value) return a.value < b.value;
    return a.index<b.index;
}
  
bool cmpindex(node a, node b) {
    return a.index < b.index;
}
  
int main() {
    int n, a;
    node nodelist[60];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a;
        nodelist[i].value = a;
        nodelist[i].index = i;
    }
    sort(nodelist, nodelist + n, cmpvalue);
    for (int i = 0; i < n; i++) {
        nodelist[i].rank = i;
    }
    sort(nodelist, nodelist + n, cmpindex);
    for (int i = 0; i < n; i++) {
        cout << nodelist[i].rank;
        if (i != n - 1) cout << " ";
    }
}

woo960131   7년 전

std::sort가 같은 숫자들의 순서를 보장해준다고 생각하고 있었는데 아니었군요!

작은 n에 대해서 되길래 되는줄 알았어요 ㅎㅎ 감사합니다!

댓글을 작성하려면 로그인해야 합니다.