LOJ#2087 国王饮水记
解:这个题一脸不可做...
比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分代码
正确的规律:最优策略一定是把一个后缀分成连续若干段。
于是以此DP,设f[i][j]表示前i次操作取到了j,此时1号点的最大值。转移就枚举从哪来即可。注意初始化。
1 #include <bits/stdc++.h> 2 3 // ---------- decimal lib start ---------- 4 5 const int PREC = 120; 6 7 // ---------- decimal lib end ---------- 8 9 const int N = 8010; 10 11 int n, k, p; 12 Decimal a[N]; 13 14 inline void output(Decimal x) { 15 std::cout << x.to_string(p + 5) << std::endl; 16 return; 17 } 18 19 inline void output(double x) { 20 printf("%.10f\n", x); 21 return; 22 } 23 24 inline void out(int x) { 25 for(int i = 0; i < n; i++) { 26 printf("%d", (x >> i) & 1); 27 } 28 return; 29 } 30 31 namespace bf { 32 33 const double eps = 1e-12; 34 35 double ans = 0, a[N], temp[101][101]; 36 int lm, pw[110101], Ans[N], now[N]; 37 38 void DFS(int x) { 39 /// check 40 double large(0); 41 for(int i = 1; i <= n; i++) { 42 if(large < a[i]) large = a[i]; 43 } 44 if(large < ans + eps) { 45 return; 46 } 47 if(fabs(large - a[1]) < eps || x > k) { 48 if(ans < a[1]) { 49 ans = a[1]; 50 memcpy(Ans + 1, now + 1, x * sizeof(int)); 51 } 52 return; 53 } 54 55 for(int i = 1; i <= n; i++) { 56 temp[x - 1][i] = a[i]; 57 } 58 for(int s = lm - 1; s > 1; s--) { 59 if(!(s - (s & (-s)))) continue; 60 double tot = 0; 61 int cnt = 0; 62 int t = s; 63 while(t) { 64 int tt = pw[t & (-t)] + 1; 65 tot += a[tt]; 66 cnt++; 67 t ^= 1 << (tt - 1); 68 } 69 tot /= cnt; 70 t = s; 71 while(t) { 72 int tt = pw[t & (-t)] + 1; 73 a[tt] = tot; 74 t ^= 1 << (tt - 1); 75 } 76 now[x] = s; 77 DFS(x + 1); 78 t = s; 79 while(t) { 80 int tt = pw[t & (-t)] + 1; 81 a[tt] = temp[x - 1][tt]; 82 t ^= 1 << (tt - 1); 83 } 84 } 85 now[x] = 0; 86 return; 87 } 88 89 inline void solve() { 90 91 /// DFS 92 lm = 1 << n; 93 for(int i = 0; i < n; i++) { 94 pw[1 << i] = i; 95 } 96 for(int i = 1; i <= n; i++) { 97 a[i] = ::a[i].to_double(); 98 } 99 DFS(1); 100 101 output(ans); 102 103 /*for(int i = 1; i <= k + 1; i++) { 104 out(Ans[i]); printf(" "); 105 } 106 puts("");*/ 107 return; 108 } 109 } 110 111 Decimal f[105][105]; 112 int sum[N]; 113 114 inline void solve() { 115 int I = 2; 116 while(a[1] > a[I]) ++I; 117 for(int i = 1; i <= n; i++) { 118 f[0][i] = a[1]; 119 } 120 for(int i = 1; i <= k; i++) { 121 f[i][I - 1] = a[1]; 122 for(int j = I; j <= n; j++) { 123 /// f[i][j] 124 f[i][j] = f[i - 1][j]; 125 for(int p = I - 1; p < j; p++) { 126 Decimal t = (f[i - 1][p] + sum[j] - sum[p]) / (j - p + 1); 127 if(f[i][j] < t) { 128 f[i][j] = t; 129 } 130 } 131 //printf("i = %d j = %d f = ", i, j); output(f[i][j]); 132 } 133 } 134 output(f[k][n]); 135 return; 136 } 137 138 int main() { 139 140 //freopen("in.in", "r", stdin); 141 //printf("%d \n", (sizeof(f)) / 1048576); 142 143 scanf("%d%d%d", &n, &k, &p); 144 for(int i = 1, x; i <= n; i++) { 145 scanf("%d", &x); 146 a[i] = x; 147 } 148 149 std::sort(a + 2, a + n + 1); 150 for(int i = 1; i <= n; i++) { 151 sum[i] = sum[i - 1] + (int)(a[i].to_double() + 0.5); 152 } 153 if(n == 1) { 154 output(a[1]); 155 return 0; 156 } 157 if(n == 2) { 158 if(a[1] > a[2]) { 159 output(a[1]); 160 } 161 else { 162 a[1] = (a[1] + a[2]) / 2; 163 output(a[1]); 164 } 165 return 0; 166 } 167 if(a[1] >= a[n]) { 168 output(a[1]); 169 return 0; 170 } 171 if(k == 1) { 172 Decimal tot = a[1], ans = a[1]; 173 int cnt = 1; 174 for(int i = n; i >= 2; i--) { 175 cnt++; 176 tot += a[i]; 177 ans = std::max(ans, tot / cnt); 178 } 179 output(ans); 180 return 0; 181 } 182 if(k >= n - 1) { 183 for(int i = 2; i <= n; i++) { 184 if(a[1] > a[i]) continue; 185 a[1] = (a[1] + a[i]) / 2; 186 } 187 output(a[1]); 188 return 0; 189 } 190 if(n <= 4) { 191 bf::solve(); 192 return 0; 193 } 194 else { 195 solve(); 196 return 0; 197 } 198 return 0; 199 }
60分代码
考虑如何优化这个DP。
转载于:https://www.cnblogs.com/huyufeifei/p/10755551.html
LOJ#2087 国王饮水记相关推荐
- [loj2087][NOI2016]国王饮水记
前言 回归OI,随便找一道清真dp题写写吧 做完发现一点都不清真 题目相关 链接 题目大意 现在有nnn个数,每次可以取若干个数,将每个数赋成平均值,限制kkk次,问第一个数最大能变成多少 数据范围 ...
- 【NOI2016】国王饮水记【贪心】【斜率优化】【决策单调性】
传送门 首先比h1h_1h1小的肯定没用,直接无视 然后考虑合并的顺序 ①在无限制的情况下,合并多个不如一个一个合并 a<b<ca<b<ca<b<c时,a+b2+ ...
- 【BZOJ3157/3516】国王奇遇记(数论)
[BZOJ3157/3516]国王奇遇记(数论) 题面 BZOJ3157 BZOJ3516 题解 先考虑怎么做\(m\le 100\)的情况. 令\(f(n,k)=\displaystyle \sum ...
- 3157: 国王奇遇记 3516: 国王奇遇记加强版 - BZOJ
果然我数学不行啊,题解君: http://www.cnblogs.com/zhuohan123/p/3726933.html 1 const 2 h=1000000007; 3 var 4 fac,f ...
- bzoj 3157 bzoj 3516 国王奇遇记 —— 推式子
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3157 https://www.lydsy.com/JudgeOnline/problem.p ...
- bzoj 3157: 国王奇遇记3516: 国王奇遇记加强版
题意 给定 n , m n,m n,m 计算 ∑ i = 1 n i m ∗ m i \sum_{i=1}^ni^m*m^i ∑i=1nim∗mi n ≤ 1 0 9 , m ≤ 5000 n\le ...
- 无名数学题1(国王奇遇记减弱版)
给出n,k,求出1^k+2^k+3^k+......n^k ,结果Mod 1234567891输出 (n<=10^9,k<=100) 真的是难啊...... 证明如下: 然后k^2递推就可 ...
- 【BZOJ 35164126】国王奇遇记
problem 给定 n , m n,m n,m,求: ∑ i = 1 n i m m i \sum_{i=1}^ni^mm^i i=1∑nimmi BZOJ 3516: 1 ≤ n ≤ 1 0 9 ...
- NOI2010~NOI2018选做
[NOI2010] [NOI2010]海拔 高度只需要0/1,所以一个合法方案就是一个割,平面图求最小割. [NOI2010]航空管制 反序拓扑排序,每次取出第一类限制最大的放置,这样做答案不会更劣. ...
最新文章
- 预测 motif 的计算原理
- R语言使用subset函数基于组合逻辑筛选dataframe符合条件的数据行(select observations)、并指定需要保留的dataframe数据列或者字段
- 敏捷开发一千零一问系列之四:优先级排错怎么办?
- 面试必问之【数组】篇
- 我把上课的机器学习课件发给了国内300多位高校老师
- UVa 1583 Digit Generator(枚举+打表)
- sphinx 全文检索 笔记一
- 微服务中的健康监测以及其在ASP.NET Core服务中实现运行状况检查
- 最bt的搜索引擎插件!
- 远程服务器mstsc命令,远程桌面连接命令mstsc怎么用
- 74款android开机动画,修改Android系统开机动画
- unity3d游戏开发第2版 pdf_从零开始学基于ARKit的Unity3d游戏开发系列1
- Java Apache POI 小记(读取Word通过模板创建PPT)
- ZNS : 解决传统SSD问题的高性能存储栈设计(fs-->io-->device)
- 数字电路复位电路解析
- 麻雀爱上凤凰在线播放,麻雀爱上凤凰详细剧情
- 跟熊浩学沟通30讲读后感_跟着熊浩学沟通的笔记
- 2021年安全员-A证最新解析及安全员-A证试题及解析
- 2018,程序员生活的两个兴趣爱好
- 中国艺术孙溟㠭篆刻作品《零落成泥碾作尘,只有香如故》
热门文章
- Here Document和Expect概述(免交互,变量设定,控制,注释,)(shell里使用EOF报错)
- 操作篇 HYbrid的应用和学习
- mysql安装时损坏的图像_在PHP中从MySQL获取图像会导致图像损坏
- python如何强制转换数据类型_python数据类型强制转换实例详解
- 在php中页面布局 3列左右侧固定中间自适应居中,css三列布局--两边固定中间自适应和中间固定两边自适应...
- linux挂载efi分区,IBM X3850安装linux出现 您还么有创建/boot/efi分区 解决办法
- find linux 指定后缀_Linux终端命令入坑必备知识与技巧
- java孙膑和庞涓问题_庞涓与孙膑的故事
- html表单下拉美化教程,用javascript实现select的美化的方法
- maven项目 错误: 找不到或无法加载主类