Guest User

Untitled

a guest
Jul 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <limits.h>
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9.  
  10. struct DATA{
  11. ll pos;
  12. ll group;
  13. ll self_idx, other_idx;
  14. DATA(){}
  15. bool operator<(const DATA& target)const{
  16. return pos<target.pos;
  17. }
  18. };
  19. vector<DATA> input;
  20. ll T;
  21.  
  22. int main()
  23. {
  24. scanf("%lld", &T);
  25. while(T--){
  26. ll N, M;
  27. scanf("%lld%lld", &N, &M);
  28.  
  29. input = vector<DATA>(N+M+1);
  30.  
  31. for(int i=1;i<=N;i++){
  32. scanf("%lld", &input[i].pos);
  33. input[i].group = -1;
  34. }
  35. // A그룹 입력
  36.  
  37. for(int i=(int)(N+1);i<=M+N;i++){
  38. scanf("%lld", &input[i].pos);
  39. input[i].group = 1;
  40. }
  41. // B그룹 입력
  42.  
  43. sort(input.begin()+1, input.end());
  44. // 정렬
  45.  
  46. vector<ll> asum, rasum, bsum, rbsum;
  47. asum = vector<ll>(N+M+2);
  48. rasum = vector<ll>(N+M+2);
  49. bsum = vector<ll>(N+M+2);
  50. rbsum = vector<ll>(N+M+2);
  51. // 누적합 배열 선언
  52.  
  53. int aidx=0, bidx=0;
  54. for(int i=1;i<=N+M;i++){
  55. if(input[i].group==-1){
  56. input[i].self_idx=++aidx;
  57. input[i].other_idx=bidx;
  58. }else{
  59. input[i].self_idx=++bidx;
  60. input[i].other_idx=aidx;
  61. }
  62. }
  63. // 전체 배열에서 상대 인덱스 저장
  64.  
  65. for(int i=1;i<=N+M;i++){
  66. asum[i] = asum[i-1];
  67. bsum[i] = bsum[i-1];
  68. if(input[i].group == -1){
  69. asum[i] += input[i].pos;
  70. }else{
  71. bsum[i] += input[i].pos;
  72. }
  73. }
  74.  
  75. for(int i=(int)(N+M);i>=1;i--){
  76. rasum[i] = rasum[i+1];
  77. rbsum[i] = rbsum[i+1];
  78. if(input[i].group == -1){
  79. rasum[i] += input[i].pos;
  80. }else{
  81. rbsum[i] += input[i].pos;
  82. }
  83. }
  84. // 누적합
  85.  
  86. ll min_idx=0;
  87. ll min_val = LONG_LONG_MAX;
  88. for(int i=1;i<=N+M;i++){
  89.  
  90. ll resa = 0, resb = 0;
  91. if(input[i].group == -1){
  92. if(i>1){
  93. resa += llabs(asum[i-1]-input[i].pos*(input[i].self_idx-1));
  94. resb += llabs(bsum[i-1]-input[i].pos*(input[i].other_idx));
  95. }
  96. if(i<N+M){
  97. resa += llabs(rasum[i+1]-input[i].pos*(N-input[i].self_idx));
  98. resb += llabs(rbsum[i+1]-input[i].pos*(M-input[i].other_idx));
  99. }
  100. }else{
  101. if(i>1){
  102. resa += llabs(asum[i-1]-input[i].pos*(input[i].other_idx));
  103. resb += llabs(bsum[i-1]-input[i].pos*(input[i].self_idx-1));
  104. }
  105. if(i<N+M){
  106. resa += llabs(rasum[i+1]-input[i].pos*(N-input[i].other_idx));
  107. resb += llabs(rbsum[i+1]-input[i].pos*(M-input[i].self_idx));
  108. }
  109. }
  110. ll res = resa*M+resb*N;
  111. // printf("i:%d val:%d %lld\n", i, input[i].pos, res);
  112. if(res<min_val){
  113. min_idx = i;
  114. min_val = res;
  115. }
  116. }
  117. // 누적합 배열 이용해서 한 정점에서 다른 모든 정점까지의 거리의 합 계산
  118.  
  119. printf("%lld.0\n", input[min_idx].pos);
  120. }
  121. return 0;
  122. }
  123.  
  124. // 3 2 2 7 12 5 8
Add Comment
Please, Sign In to add comment