Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. // 중앙값
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5. #include <unordered_map>
  6. #include <fstream>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <vector>
  11. #include <stack>
  12. #include <queue>
  13. #include <functional>
  14. #include <list>
  15. #include <deque>
  16. #include <numeric>
  17. #include <set>
  18. #include <climits>
  19. #include <utility>
  20. #include <map>
  21. #include <algorithm>
  22. #define INF 987654321
  23. #define MOD 1000000007
  24. using namespace std;
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. inline int max( int x, int y ){ return x > y ? x : y ; }
  28. inline int min( int x, int y ){ return x < y ? x : y ; }
  29. inline ll max( ll x, ll y ){ return x > y ? x : y ; }
  30. inline ll min( ll x, ll y ){ return x < y ? x : y ; }
  31. inline ull max( ull x, ull y ){ return x > y ? x : y ; }
  32. inline ull min( ull x, ull y ){ return x < y ? x : y ; }
  33. int gcd( int a, int b ) { return b ? gcd( b , a%b ) : a; }
  34.  
  35. int N, K;
  36. int arr[250000+1], Tree[65536+1];
  37. ll ansValue = 0;
  38.  
  39. void update( int index, int diff ){
  40.  
  41. while( index <= 65536 ){
  42. Tree[index] += diff;
  43. index += ( index & -index );
  44. }
  45. }
  46.  
  47. ll sum( int index ){
  48.  
  49. ll ans = 0;
  50. while( index > 0 ){
  51. ans += Tree[index];
  52. index -= ( index & -index );
  53. }
  54.  
  55. return ans;
  56. }
  57.  
  58. int Calc(){
  59.  
  60. int front = 0;
  61. int back = 65536;
  62. int mid, target = (K+1) >> 1;
  63.  
  64. while( front < back ){
  65. mid = ( front + back ) >> 1;
  66. if( sum(mid+1) < target ){
  67. front = mid+1;
  68. }else{
  69. back = mid;
  70. }
  71. }
  72.  
  73. return front;
  74. }
  75.  
  76. int main() {
  77.  
  78. scanf("%d %d", &N, &K);
  79. memset( arr, 0, sizeof(arr) );
  80. memset( Tree, 0, sizeof(Tree) );
  81. for( int i=1; i<=N; ++i ){
  82. scanf("%d", &arr[i]);
  83. }
  84.  
  85. for( int i=1; i<=K; ++i ){
  86. update( arr[i]+1, 1 );
  87. }
  88. ansValue += Calc();
  89. for( int i=1; i<=N-K; ++i ){
  90. update( arr[i]+1, -1 );
  91. update( arr[i+K]+1, 1 );
  92. ansValue += Calc();
  93. }
  94. printf("%lld", ansValue);
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement