标签:des style blog http color java os io
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 557 Accepted Submission(s):
159
1 #include <cstdio> 2 #include <cstring> 3 #include <stack> 4 #define eps 0.00000001 5 using namespace std; 6 7 const int LEN = 100010; 8 int arr[LEN]; 9 struct line 10 { 11 int l, r, sum; 12 double rate; 13 }; 14 stack<line> s; 15 int main() 16 { 17 int T, n; 18 line tmp; 19 scanf("%d", &T); 20 while(T--) 21 { 22 scanf("%d", &n); 23 for(int i = 0; i < n; i++) 24 scanf("%d", &arr[i]); 25 int h = 0; 26 while(arr[h] == 0) 27 h++; 28 int k = n - 1; 29 while(arr[k] == 1) 30 k--; 31 for(int i = h; i <= k; i++) 32 { 33 if (i == h || i > h && arr[i-1] == 0 && arr[i] == 1) 34 { 35 tmp.l = i; 36 tmp.sum = 0; 37 } 38 if (i < k && arr[i] == 0 && arr[i+1] == 1 || i == k) 39 { 40 tmp.r = i; 41 tmp.rate = tmp.sum * 1.0 / ((tmp.r - tmp.l + 1) * 1.0); 42 while(true) 43 { 44 if (s.empty() || s.top().rate - tmp.rate < eps) 45 { 46 s.push(tmp); 47 break; 48 } 49 if (s.top().rate - tmp.rate > eps) 50 { 51 tmp.l = s.top().l; 52 tmp.sum += s.top().sum; 53 tmp.rate = tmp.sum*1.0 / ((tmp.r - tmp.l + 1)*1.0); 54 s.pop(); 55 } 56 } 57 } 58 if (arr[i] == 1) 59 tmp.sum++; 60 } 61 double ans = 0; 62 while(!s.empty()) 63 { 64 ans += ((1 - s.top().rate) * (1 - s.top().rate) * s.top().sum + s.top().rate * s.top().rate * (s.top().r - s.top().l + 1 - s.top().sum)); 65 s.pop(); 66 } 67 printf("%f\n", ans); 68 } 69 return 0; 70 }
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/lxm940130740/p/3898660.html