fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std ;
  4. typedef long long ll ;
  5.  
  6. struct element{
  7.  
  8. int position ;
  9. int age ;
  10. string name ;
  11.  
  12. element(int position, int age, string name)
  13. : position(position), age(age), name(name)
  14. {
  15. }
  16.  
  17. } ;
  18.  
  19. // position will be different so two people cannot be totally same
  20. bool comparator(element &a, element &b){
  21.  
  22. if(a.age == b.age){
  23.  
  24. if(a.name == b.name){
  25.  
  26. return a.position < b.position ;
  27.  
  28. } else
  29. return a.name < b.name ;
  30.  
  31. } else
  32. return a.age < b.age ;
  33.  
  34. }
  35.  
  36. void solve(){
  37.  
  38. deque < element > q ; q.clear() ;
  39. vector < element > vec ; vec.clear() ;
  40.  
  41. int n, m ; cin >> n >> m ;
  42. for(int i = 0 ; i < n ; i++){
  43. int age ; string name ;
  44. cin >> name >> age ;
  45. q.push_back(element(i, age, name)) ;
  46. }
  47.  
  48. for(int i = 0 ; i < m ; i++){
  49.  
  50. bool flag = comparator(q.front(), q.back()) ;
  51.  
  52. if(flag == 1){
  53. vec.push_back(q.front()) ;
  54. q.pop_front() ;
  55. } else{
  56. vec.push_back(q.back()) ;
  57. q.pop_back() ;
  58. }
  59.  
  60. }
  61.  
  62. sort(vec.begin(), vec.end(), comparator) ;
  63.  
  64. for(int i = 0 ; i < m ; i++){
  65. cout << vec[i].position << " " ;
  66. }
  67.  
  68. cout << "\n" ;
  69.  
  70. }
  71.  
  72. int main(){
  73.  
  74. #ifndef ONLINE_JUDGE
  75. freopen("sample input.txt", "r", stdin) ;
  76. freopen("outputc.txt", "w", stdout) ;
  77. #endif
  78.  
  79. ios::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0) ;
  80.  
  81. int t ; cin >> t ;
  82. while(t--) solve() ;
  83.  
  84. return 0 ;
  85. }
  86.  
Success #stdin #stdout 0s 15248KB
stdin
3
4 3
kaustubh 32
anshul 56
ravindra 22
rajeev 35
3 3
a 21
c 21
b 21
4 2
taylor 65
shawn 67
kim 35
kim 35
stdout
2 0 3 
0 2 1 
2 3