Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 중앙값
- #include <iostream>
- #include <cstring>
- #include <unordered_map>
- #include <fstream>
- #include <cstdio>
- #include <cmath>
- #include <ctime>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <functional>
- #include <list>
- #include <deque>
- #include <numeric>
- #include <set>
- #include <climits>
- #include <utility>
- #include <map>
- #include <algorithm>
- #define INF 987654321
- #define MOD 1000000007
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- inline int max( int x, int y ){ return x > y ? x : y ; }
- inline int min( int x, int y ){ return x < y ? x : y ; }
- inline ll max( ll x, ll y ){ return x > y ? x : y ; }
- inline ll min( ll x, ll y ){ return x < y ? x : y ; }
- inline ull max( ull x, ull y ){ return x > y ? x : y ; }
- inline ull min( ull x, ull y ){ return x < y ? x : y ; }
- int gcd( int a, int b ) { return b ? gcd( b , a%b ) : a; }
- int N, K;
- int arr[250000+1], Tree[65536+1];
- ll ansValue = 0;
- void update( int index, int diff ){
- while( index <= 65536 ){
- Tree[index] += diff;
- index += ( index & -index );
- }
- }
- ll sum( int index ){
- ll ans = 0;
- while( index > 0 ){
- ans += Tree[index];
- index -= ( index & -index );
- }
- return ans;
- }
- int Calc(){
- int front = 0;
- int back = 65536;
- int mid, target = (K+1) >> 1;
- while( front < back ){
- mid = ( front + back ) >> 1;
- if( sum(mid+1) < target ){
- front = mid+1;
- }else{
- back = mid;
- }
- }
- return front;
- }
- int main() {
- scanf("%d %d", &N, &K);
- memset( arr, 0, sizeof(arr) );
- memset( Tree, 0, sizeof(Tree) );
- for( int i=1; i<=N; ++i ){
- scanf("%d", &arr[i]);
- }
- for( int i=1; i<=K; ++i ){
- update( arr[i]+1, 1 );
- }
- ansValue += Calc();
- for( int i=1; i<=N-K; ++i ){
- update( arr[i]+1, -1 );
- update( arr[i+K]+1, 1 );
- ansValue += Calc();
- }
- printf("%lld", ansValue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement