51nod average (Marathon 14)

Source: Internet
Author: User

Average alpq654321(propositional person)Base time limit: 4 seconds Space limit: 131072 KB score: 80LYK has a sequence of length n. He is studying the average recently. He even wanted to know the average of all the intervals, but the number of intervals was too large. For the sake of convenience, you just have to tell him the average of the K-large in all the intervals (n (n+1)/2 intervals). Input
The first line is two digits n,k (1<=n<=100000,1<=k<=n* (n+1)/2). The next row n number represents the interval of lYK (1<=ai<=100000).
Output
A row represents the average of the K-large, which is correct if the error does not exceed 1e-4.
Input example
5 31 2 3 4 5
Output example
4.000

/*51nod Average title: Give you the number of n, you can know the total can be composed of (n+1) *N/2 interval, then I ask these intervals, the K-large average value at the beginning is a little idea is not, and later saw a somewhat similar topic, found that we can change the problem into The number of intervals with an average greater than x is so that it can be resolved by enumerating the answers and then judging them. Using f[i] to denote the number of the first I and, then: Ave = (F[r]-f[l])/(r-l) So ave >= x---> F[r]-rx >= f[l]-lx that appears earlier than the current value of the number of small but found F[r]-rx can reach-1 0^10, and is a floating-point type. So again tangled for a long time, at the beginning also thought of discretization (but always think it will time out Orz) results found that they really think too much, using sort, each time to find the current value is the first, and then use the tree-like array maintenance. HHH 2016/05/28 16:22:06*/#include <iostream> #include <vector> #include <cstring> #include <string > #include <cstdio> #include <queue> #include <cmath> #include <algorithm> #include < Functional> #include <map>using namespace std; #define Lson (i<<1) #define Rson ((i<<1) |) typedef Long long ll;using namespace Std;const int maxn = 101010;const double PI = 3.1415926;const double eps = 1e-6;double A[MAXN ];ll S[MAXN];d ouble F[MAXN];d ouble g[maxn];int n;ll k;void Add (int x,int val) {while (x <= 100000) {S[x] +        = Val;    x + = (x& (-X)); }}LL sum (int pos) {ll ans = 0;        while (pos > 0) {ans + = S[pos];    pos-= (pos& (-pos)); } return ans;    BOOL Cal (double x) {memset (s,0,sizeof (s));    F[0] = 0;    for (int i = 1; I <= n; i++) f[i] = F[i-1] + a[i];    ll num = 0;        for (int i = 1; I <= n; i++) {f[i]-= (double) i*x;        G[i] = F[i];    if (F[i] > 0) num + +;    } sort (g+1,g+n+1);//for (int i = 1;i <= n;i++)//cout << G[i] << "";//cout << Endl;        for (int i = 1; I <= n; i++) {int pla = Lower_bound (G+1,g+n+1,f[i])-G;        num + = SUM (PLA);    Add (pla,1);    } if (num >= k) return true; else return false;}    int main () {//freopen ("In.txt", "R", stdin);    scanf ("%d", &n);    scanf ("%lld", &k);    Double Max = 0;        for (int i = 1; I <= n; i++) {scanf ("%lf", &a[i]);    max = max (A[i],max);    } double L = 0;    Double r = Max; while (R-l > EPs) {double mid = (L+R)/2;        if (Cal (mid)) L = mid;    else R = mid-eps;    } printf ("%f\n", L); return 0;}

  

51nod average (Marathon 14)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.