1. 程式人生 > >AtCoder ARC 101 D Median of Medians(逆序對)

AtCoder ARC 101 D Median of Medians(逆序對)

size 我們 完美解決 lap () 特殊 sse owb 問題

題意:
給一個數組,取每一段區間的中位數重新構成一個一個數組,求出該數組的中位數
中位數是指序列中A的第$\frac{|A|}{2}+1$個元素
分析:
剛讀完題感覺此題很神,做完之後發現確實很神,不得不贊嘆Atcoder出題人太強了。。。
考慮二分答案。
我們二分最後的中位數是mid,把原序列大於mid的數變成1,小於等於的是-1
之後處理出前綴和sum
然後發現一個重要的性質:只要一段區間的和大於0,那麽這段區間的中位數就一定大於等於mid
於是要求的答案就轉化成了當前序列中的順序對($i < j\ \&\& \ a[i] < a[j]$)個數。
大家肯定知道逆序對的求法可以通過樹狀數組和歸並排序解決,那麽順序對也一樣的,筆者用的是樹狀數組去求。

如果算出來的順序對個數$>n*(n-1)/4$就是$return \ true$(n*(n-1)個區間的中間是要在/2)
於是問題就被完美解決了
代碼:

技術分享圖片
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <cstdio>
#include 
<cctype> #include <string> #include <cstring> #include <cassert> #include <climits> #include <cstdlib> #include <iostream> #include <algorithm> #include <functional> using namespace std ; #define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) #define
Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++) #define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) #define clr(a) memset(a, 0, sizeof(a)) #define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp) #define ass(a, sum) memset(a, sum, sizeof(a)) #define ls ((rt) << 1) #define rs ((rt) << 1 | 1) #define lowbit(x) (x & -x) #define mp make_pair #define pb push_back #define fi first #define se second #define endl ‘\n‘ #define ENDL cout << endl #define SZ(x) ((int)x.size()) typedef long long ll ; typedef unsigned long long ull ; typedef vector <int> vi ; typedef pair <int, int> pii ; typedef pair <ll, ll> pll ; typedef map <int, int> mii ; typedef map <string, int> msi ; typedef map <ll, ll> mll ; const int N = 100010 ; const double eps = 1e-8 ; const int iinf = INT_MAX ; const ll linf = 2e18 ; const double dinf = 1e30 ; const int MOD = 1000000007 ; inline int read(){ int X = 0, w = 0 ; char ch = 0 ; while (!isdigit(ch)) { w |= ch == - ; ch = getchar() ; } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ; return w ? - X : X ; } void write(int x){ if (x < 0) putchar(-), x = - x ; if (x > 9) write(x / 10) ; putchar(x % 10 + 0) ; } void print(int x) { cout << x << endl ; exit(0) ; } void PRINT(string x) { cout << x << endl ; exit(0) ; } void douout(double x){ printf("%lf\n", x + 0.0000000001) ; } int a[N], sum[N * 10], bit[N * 10] ; int n, l, r ; void add(int a) { for (; a <= 2 * N; a += lowbit(a)) bit[a]++ ; } int query(int a){ int res = 0 ; for (; a; a -= lowbit(a)) res += bit[a] ; return res ; } int check(int x) { for (int i = 0; i <= 2 * N; i++) bit[i] = 0 ; clr(sum) ; for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + (a[i] >= x ? 1 : -1) ; } ll ans = 0 ; for (int i = 0; i <= n; i++) { ans += query(sum[i] + N) ; add(sum[i] + N) ; } return ans >= 1ll * n * (n + 1) / 4 ; } signed main(){ scanf("%d", &n) ; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), r = max(r, a[i]) ; while (l <= r) { int mid = (l + r) >> 1 ; if (check(mid)) l = mid + 1 ; else r = mid - 1 ; } printf("%d\n", r) ; } /* 寫代碼時請註意: 1.是否要開Long Long?數組邊界處理好了麽? 2.實數精度有沒有處理? 3.特殊情況處理好了麽? 4.做一些總比不做好。 思考提醒: 1.最大值和最小值問題可不可以用二分答案? 2.有沒有貪心策略?否則能不能dp? */
AC

AtCoder ARC 101 D Median of Medians(逆序對)