WC simulation (1.12) T1 small C drinking water

Little C drinking water

Topic background:

1.12 WC Simulation T1

Analysis: Violence + Linked List

 

First of all, it can be easily found that for an interval, it must be operated from the smallest interval to the largest interval, then for the i -th largest number, the contribution is x / 2 i , then we will consider the interval, and convert it into for each Considering the contribution of the number, the problem can be transformed into, we scan forward from the current number, each time a number larger than it is reached, the contribution of the current position is 1/2 of the previous position, otherwise it is the contribution of the previous position, so, Calculate the sum of the weights on the left and right sides, multiply and divide by two to get y, then the contribution of this number is x * y , and then we find that the accuracy requirement of this question is very small, which means that we do not need to To find all numbers larger than it, we only need to find a certain number. We can enumerate the numbers from small to large, and each time we want to find t numbers on the left and right , and then delete the current number, it is very simple to use a doubly linked list. maintenance. The complexity is O(nt + nlogn) , and t is about 35 ~ 45 .

 

Source:

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())	
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar())
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 1000000 + 10;
const int MAX = 35;

int n;
int a[MAXN];
struct data {
	int num, ori;
	inline bool operator < (const data &a) const {
		return (num == a.num) ? (ori < a.ori) : (num < a.num);
	}
} c[MAXN];

struct list {
	int pre, juice;
} l[MAXN];

inline void read_in() {
	R(n);
	for (int i = 1; i <= n; ++i) R(a[i]), c[i].num = a[i], c[i].ori = i;
	std::sort(c + 1, c + n + 1);
	for (int i = 1; i <= n; ++i) l[i].pre = i - 1, l[i].suc = i + 1;
}

inline void solve() {
	int rk, last, cnt, cur;
	double ret, f_ans, b_ans, ans = 0;
	for (int i = 1; i <= n; ++i) {
		cur = c [i] .ori;
		last = cur, ret = 1, f_ans = 0, cnt = 0;
		while (cnt <= MAX && last != 0) {
			rk = l[last].pre, f_ans += (double)(last - rk) * ret;
			ret / = 2.0, cnt ++, last = rk;
		}
		last = cur, ret = 1, b_ans = 0, cnt = 0;
		while (cnt <= MAX && last != n + 1) {
			rk = l[last].suc, b_ans += (double)(rk - last) * ret;
			ret / = 2.0, cnt ++, last = rk;
		}
		years += b_years * f_years * (double)c[i].num / 2.0;
		l [l [cur] .pre] .suc = l [cur] .suc, l [l [cur] .suc] .pre = l [cur] .pre;
	}
	printf("%0.9lf", ans / (double)n / (double)n);
}

int main() {
	freopen("drink.in", "r", stdin);
	freopen("drink.out", "w", stdout);
	read_in();
	solve();
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577619&siteId=291194637