标签:
第一行两个数n,k(1<=n<=100000,1<=k<=n*(n+1)/2)。接下来一行n个数表示LYK的区间(1<=ai<=100000)。
一行表示第k大的平均数,误差不超过1e-4就算正确。
5 31 2 3 4 5
4.000
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MAXN 100010 5 typedef long long ll; 6 using namespace std; 7 ll sum[MAXN]; 8 double a[MAXN],b[MAXN]; 9 int tree[MAXN]; 10 int n; 11 ll k; 12 13 int lowbit( int x) 14 { 15 return x & -x; 16 } 17 void add(int k) 18 { 19 while(k <= n + 1) 20 { 21 tree[k] ++; 22 k += lowbit(k); 23 } 24 } 25 int Sum(int k) 26 { 27 int sum = 0; 28 while(k) 29 { 30 sum += tree[k]; 31 k -= lowbit(k); 32 } 33 return sum; 34 } 35 long long check(double x) 36 { 37 memset(tree, 0, sizeof(tree)); 38 for(int i = 0; i <= n; i++) 39 a[i] = b[i] = sum[i] - i * x; 40 sort(a, a + 1 + n); 41 int num = unique(a, a + 1 + n) - a; 42 long long ans = 0; 43 for(int i = 0; i <= n; i++) 44 { 45 int temp = lower_bound(a, a + num, b[i]) - a + 1; 46 ans += Sum(temp); 47 add(temp); 48 } 49 return ans; 50 } 51 int main() 52 { 53 scanf("%d%I64d",&n,&k); 54 int temp; 55 sum[0] = 0; 56 for(int i = 1; i <= n; i++) 57 { 58 scanf("%d",&temp); 59 sum[i] = sum[i - 1] + temp; 60 } 61 double l = 1.0, r = 100000.0; 62 while(r - l >= 1e-6) 63 { 64 double mid = (l + r) / 2.0; 65 if (check(mid) >= k) 66 l = mid; 67 else 68 r = mid; 69 } 70 printf("%.4f\n",(r + l)/2.0); 71 return 0; 72 }
标签:
原文地址:http://www.cnblogs.com/yutingmoran/p/5901463.html