알고리즘 문제

[C/C++ 백준 10814번] 나이순 정렬 (Silver 5)

새파란 공대생 2020. 8. 20. 16:44

https://www.acmicpc.net/problem/10814

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 �

www.acmicpc.net

string을 써서 cin과 cout를 이용했는데 그냥 사용하면 시간 초과가 걸린다.

 

ios::sync_with_stdio(false);cin.tie(NULL);을 이용하자.

 

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef struct judge{
   int age;
   string name;
   int num;
}judge;
judge People[100001];
bool comp(judge a, judge b){
   if(a.age==b.age)
      return a.num<b.num;
   return a.age<b.age;
}
int main(void){
ios::sync_with_stdio(false);cin.tie(NULL);
   int N;
   cin>>N;
   for(int i=0; i<N; i++){
      cin>>People[i].age>>People[i].name;
      People[i].num=i;
   }
   sort(People, People + N, comp);
   for(int i=0; i<N; i++){
      cout<<People[i].age<<" "<<People[i].name<<"\n";   
   }
}