1. 程式人生 > >bzoj 1011[HNOI2008]遙遠的行星 - 近似

bzoj 1011[HNOI2008]遙遠的行星 - 近似

sin 上界 net des 線上 發現 else plan getch

1011: [HNOI2008]遙遠的行星

Time Limit: 10 Sec Memory Limit: 162 MBSec Special Judge

Description

  直線上N顆行星,X=i處有行星i,行星J受到行星I的作用力,當且僅當i<=AJ.此時J受到作用力的大小為 Fi->j=
Mi*Mj/(j-i) 其中A為很小的常量,故直觀上說每顆行星都只受到距離遙遠的行星的作用。請計算每顆行星的受力
,只要結果的相對誤差不超過5%即可.

Input

  第一行兩個整數N和A. 1<=N<=10^5.0.01< a < =0.35,接下來N行輸入N個行星的質量Mi,保證0<=Mi<=10^7

Output

  N行,依次輸出各行星的受力情況

Sample Input

5 0.3
3
5
6
2
4

Sample Output

0.000000
0.000000
0.000000
1.968750
2.976000

HINT

  精確結果應該為0 0 0 2 3,但樣例輸出的結果誤差不超過5%,也算對

這題還算是比較神的

因為精度誤差範圍很大

所以對於i很大的情況, 我們會發現 上界的 (i - A * j) 和 下界的(i - 1) 其實相對的差不是很大

所以我們就直接得到了答案

$$ANS = \frac{M[i] * \sum_{1}^{j}M[j]}{i - \frac{j}{2}}$$

小數據暴力, 大數據 近似

然而依舊很玄學 ~

技術分享圖片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #define LL long long
 5 
 6 using namespace std;
 7 const int MAXN = 5e5 + 10;
 8 const double eps = 1e-8;
 9 int N;
10 double sum[MAXN];
11 double M[MAXN], A;
12 inline LL read()
13 {
14     LL x = 0
, w = 1; char ch = 0; 15 while(ch < 0 || ch > 9) { 16 if(ch == -) { 17 w = -1; 18 } 19 ch = getchar(); 20 } 21 while(ch >= 0 && ch <= 9) { 22 x = x * 10 + ch - 0; 23 ch = getchar(); 24 } 25 return x * w; 26 } 27 int main() 28 { 29 // freopen("planet10.in", "r", stdin); 30 // freopen("t.out", "w", stdout); 31 N = read(); 32 scanf("%lf", &A); 33 for(int i = 1; i <= N; i++) { 34 scanf("%lf", &M[i]); 35 sum[i] = sum[i - 1] + M[i]; 36 } 37 if(N <= 3000) { 38 for(int i = 1; i <= N; i++) { 39 double ans = 0; 40 int m = (int)(A * (double)i + eps); 41 for(int j = 1; j <= m; j++) { 42 ans += M[i]* M[j] / (i - j); 43 } 44 printf("%lf\n", ans); 45 } 46 } else { 47 double ans; 48 for(int i = 1; i <= 3000; i++) { 49 ans = 0; 50 int m = (int)(A * (double)i + eps); 51 for(int j = 1; j <= m; j++) { 52 ans += M[i]* M[j] / (i - j); 53 } 54 printf("%lf\n", ans); 55 } 56 for(int i = 3000 + 1; i <= N; i++) { 57 int m = (int)(A * (double)i + eps); 58 ans = M[i] * sum[m] / (double)(i - m / 2); 59 printf("%lf\n", ans); 60 } 61 } 62 // fclose(stdin); 63 // fclose(stdout); 64 return 0; 65 } 66 67 /* 68 5 0.3 69 70 3 71 72 5 73 74 6 75 76 2 77 78 4 79 */
View Code

bzoj 1011[HNOI2008]遙遠的行星 - 近似