1. 程式人生 > >LOJ#2087 國王飲水記

LOJ#2087 國王飲水記

-s closed isp tdi end return mil 單獨 ...

技術分享圖片

解:這個題一臉不可做...

比1小的怎麽辦啊,好像沒用,扔了吧。

先看部分分,n = 2簡單,我會分類討論!n = 4簡單,我會搜索!n = 10,我會剪枝!

k = 1怎麽辦,好像選的那些越大越好啊,那麽我就排序之後枚舉後綴!

k = INF怎麽辦啊,好像最優策略是從小到大一個一個連通啊,那直接模擬好了。

寫一寫,有40分了。

別的怎麽辦啊,拿搜索找規律吧?於是發現一個規律(偽):最優策略一定是單獨選擇最後k - 1個,和前面的一個後綴。

於是枚舉後綴,然後模擬後面的部分,成功得到了61分!

技術分享圖片
  1 #include <bits/stdc++.h>
  2
3 // ---------- decimal lib start ---------- 4 5 // ---------- decimal lib end ---------- 6 7 const int N = 8010; 8 9 int n, k, p; 10 Decimal a[N]; 11 12 inline void output(Decimal x) { 13 std::cout << x.to_string(p + 5) << std::endl; 14 return; 15
} 16 17 inline void output(double x) { 18 printf("%.10f\n", x); 19 return; 20 } 21 22 inline void out(int x) { 23 for(int i = 0; i < n; i++) { 24 printf("%d", (x >> i) & 1); 25 } 26 return; 27 } 28 29 namespace bf { 30 31 const
double eps = 1e-12; 32 33 double ans = 0, a[N], temp[101][101]; 34 int lm, pw[110101], Ans[N], now[N]; 35 36 void DFS(int x) { 37 /// check 38 double large(0); 39 for(int i = 1; i <= n; i++) { 40 if(large < a[i]) large = a[i]; 41 } 42 if(large < ans + eps) { 43 return; 44 } 45 if(fabs(large - a[1]) < eps || x > k) { 46 if(ans < a[1]) { 47 ans = a[1]; 48 memcpy(Ans + 1, now + 1, x * sizeof(int)); 49 } 50 return; 51 } 52 53 for(int i = 1; i <= n; i++) { 54 temp[x - 1][i] = a[i]; 55 } 56 for(int s = lm - 1; s > 1; s--) { 57 if(!(s - (s & (-s)))) continue; 58 double tot = 0; 59 int cnt = 0; 60 int t = s; 61 while(t) { 62 int tt = pw[t & (-t)] + 1; 63 tot += a[tt]; 64 cnt++; 65 t ^= 1 << (tt - 1); 66 } 67 tot /= cnt; 68 t = s; 69 while(t) { 70 int tt = pw[t & (-t)] + 1; 71 a[tt] = tot; 72 t ^= 1 << (tt - 1); 73 } 74 now[x] = s; 75 DFS(x + 1); 76 t = s; 77 while(t) { 78 int tt = pw[t & (-t)] + 1; 79 a[tt] = temp[x - 1][tt]; 80 t ^= 1 << (tt - 1); 81 } 82 } 83 now[x] = 0; 84 return; 85 } 86 87 inline void solve() { 88 89 /// DFS 90 lm = 1 << n; 91 for(int i = 0; i < n; i++) { 92 pw[1 << i] = i; 93 } 94 for(int i = 1; i <= n; i++) { 95 a[i] = ::a[i].to_double(); 96 } 97 DFS(1); 98 99 output(ans); 100 101 /*for(int i = 1; i <= k + 1; i++) { 102 out(Ans[i]); printf(" "); 103 } 104 puts("");*/ 105 return; 106 } 107 } 108 109 int main() { 110 111 //freopen("in.in", "r", stdin); 112 113 scanf("%d%d%d", &n, &k, &p); 114 for(int i = 1, x; i <= n; i++) { 115 scanf("%d", &x); 116 a[i] = x; 117 } 118 119 std::sort(a + 2, a + n + 1); 120 if(n == 1) { 121 output(a[1]); 122 return 0; 123 } 124 if(n == 2) { 125 if(a[1] > a[2]) { 126 output(a[1]); 127 } 128 else { 129 a[1] = (a[1] + a[2]) / 2; 130 output(a[1]); 131 } 132 return 0; 133 } 134 if(a[1] >= a[n]) { 135 output(a[1]); 136 return 0; 137 } 138 if(k == 1) { 139 Decimal tot = a[1], ans = a[1]; 140 int cnt = 1; 141 for(int i = n; i >= 2; i--) { 142 cnt++; 143 tot += a[i]; 144 ans = std::max(ans, tot / cnt); 145 } 146 output(ans); 147 return 0; 148 } 149 if(k >= n - 1) { 150 for(int i = 2; i <= n; i++) { 151 if(a[1] > a[i]) continue; 152 a[1] = (a[1] + a[i]) / 2; 153 } 154 output(a[1]); 155 return 0; 156 } 157 if(n <= 10) { 158 bf::solve(); 159 return 0; 160 } 161 else { 162 Decimal tot = a[1], ans = a[1]; 163 int cnt = 1; 164 for(int i = n - k + 1; i >= 2; i--) { 165 cnt++; 166 tot += a[i]; 167 ans = std::max(ans, tot / cnt); 168 } 169 a[1] = ans; 170 for(int i = n - k + 2; i <= n; i++) { 171 if(a[1] > a[i]) continue; 172 a[1] = (a[1] + a[i]) / 2; 173 } 174 output(a[1]); 175 return 0; 176 } 177 return 0; 178 }
61分代碼

正確的規律:

LOJ#2087 國王飲水記