LOJ#2087 国王饮水记

本文深入解析了一道算法竞赛题目,通过逐步分析不同情况下的最优策略,最终提供了两种解题思路:一种是基于枚举和模拟的61分解法;另一种是通过动态规划优化得到的60分解法。文章详细展示了代码实现过程,为读者提供了宝贵的解题思路和技巧。

解:这个题一脸不可做...

比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

06-21
<think>我们正在查询LOJ6279相关的编程题目或解决方案。LOJ(LibreOJ)是一个在线的评测系统,主要收录算法竞赛题目。根据题号6279,我们需要确定该题目的具体内容。由于我无法直接访问网络,我将基于已知信息进行推理。在算法竞赛中,LOJ的题目编号通常特定的题目对应。题号6279可能对应一个特定的问题。根据常见的题目类型,我们可以推测它可能涉及数据结构算法。然而,为了准确回答,我们可以尝试回忆或推断常见的LOJ题目。例如,LOJ6000系列题目多为数据结构题,如线段树、树状数组等。实际上,通过搜索忆,我回想起LOJ6279是“数列分块入门3”(BlockArray3)题目。这是“分块”系列题目中的一题。题目描述:给出一个长为n的数列,以及n个操作,操作涉及区间加法,询问区间内小于某个值x的前驱(即最大的小于x的数)。因此,用户需要的是关于LOJ6279题目的描述和解决方案。解决方案:由于题目要求分块处理,我们将数列分成若干块,每块大小为sqrt(n)。对于每个块,我们维护一个有序的数组(用于二分查找)以及一个加法标录整个块被加了多少)。操作:1.区间加法:对于整块,直接更新加法标;对于不完整的块,暴力更新元素值,然后重新排序该块(或者重新构建有序数组)。2.查询区间内小于x的前驱:遍历区间内的所有块。对于整块,在块的有序数组中二分查找小于(x-加法标)的最大值;对于不完整的块,暴力遍历每个元素,判断是否小于x(注意加上加法标)并更新最大值。代码实现(C++)示例:```cpp#include<iostream>#include<vector>#include<cmath>#include<algorithm>#include<climits>usingnamespacestd;constintMAXN=100010;constintBLOCK_SIZE=320;//约sqrt(MAXN)intn;vector<int>a;vector<int>block_add;//每块的加法标vector<vector<int>>sorted_blocks;//每块的有序数组voidinit(){intblock_num=(n+BLOCK_SIZE-1)/BLOCK_SIZE;block_add.assign(block_num,0);sorted_blocks.resize(block_num);for(inti=0;i<n;i++){sorted_blocks[i/BLOCK_SIZE].push_back(a[i]);}for(inti=0;i<block_num;i++){sort(sorted_blocks[i].begin(),sorted_blocks[i].end());}}voidupdate_block(intblock_id){sorted_blocks[block_id].clear();intstart=block_id*BLOCK_SIZE;intend=min((block_id+1)*BLOCK_SIZE,n);for(inti=start;i<end;i++){sorted_blocks[block_id].push_back(a[i]);}sort(sorted_blocks[block_id].begin(),sorted_blocks[block_id].end());}voidadd(intl,intr,intc){intblock_l=l/BLOCK_SIZE,block_r=r/BLOCK_SIZE;if(block_l==block_r){for(inti=l;i<=r;i++){a[i]+=c;}update_block(block_l);}else{for(inti=l;i<(block_l+1)*BLOCK_SIZE;i++){a[i]+=c;}update_block(block_l);for(inti=block_l+1;i<block_r;i++){block_add[i]+=c;}for(inti=block_r*BLOCK_SIZE;i<=r;i++){a[i]+=c;}update_block(block_r);}}intquery(intl,intr,intc){intblock_l=l/BLOCK_SIZE,block_r=r/BLOCK_SIZE;intans=-1;if(block_l==block_r){for(inti=l;i<=r;i++){intval=a[i]+block_add[block_l];if(val<c){if(val>ans)ans=val;}}}else{for(inti=l;i<(block_l+1)*BLOCK_SIZE;i++){intval=a[i]+block_add[block_l];if(val<c){if(val>ans)ans=val;}}for(inti=block_l+1;i<block_r;i++){//在块i的有序数组中二分查找小于c-block_add[i]的最大值inttarget=c-block_add[i];autoit=lower_bound(sorted_blocks[i].begin(),sorted_blocks[i].end(),target);if(it!=sorted_blocks[i].begin()){it--;intval=*it+block_add[i];if(val>ans)ans=val;}}for(inti=block_r*BLOCK_SIZE;i<=r;i++){intval=a[i]+block_add[block_r];if(val<c){if(val>ans)ans=val;}}}returnans;}intmain(){cin>>n;a.resize(n);for(inti=0;i<n;i++){cin>>a[i];}init();for(inti=0;i<n;i++){intop,l,r,c;cin>>op>>l>>r>>c;l--;r--;//如果题目输入是1-indexed,则转换为0-indexedif(op==0){add(l,r,c);}else{cout<<query(l,r,c)<<endl;}}return0;}```注意:上述代码是示例代码,可能需要根据题目具体输入要求进行调整。§§相关问题§§1.LOJ6279题目的具体描述是什么?2.分块算法的时间复杂度是多少?3.除了分块,还有哪些方法可以解决LOJ6279?4.如何优化分块算法的常数?5.分块算法适用于哪些场景?</think>### LOJ 6279 题目分析解决方案 **题目描述**:LOJ 6279 是 LibreOJ 上的 **"数列分块入门 3"**,属于分块算法的经典练习题。核心要求如下: - **操作 1**:区间 $[l, r]$ 内每个数加上值 $c$ - **操作 2**:查询区间 $[l, r]$ 内小于 $c$ 的**最大元素值**(即前驱),不存在时返回 $-1$ #### 分块算法设计 分块的核心思想是将数列划分为 $\sqrt{n}$ 大小的块,平衡暴力操作批量处理。 ##### 关键数据结构 1. **块内有序数组**: - 每块维护一个排序后的数组,用于二分查找前驱。 - 示例:块 $B_i$ 的排序数组为 $S_i$。 2. **加法懒标**: - 录整块被加的总值 $add_i$(不影响块内有序性)。 ##### 操作实现 1. **区间加法($[l, r] + c$)**: - **整块**:直接更新懒标 $add_i \gets add_i + c$,时间复杂度 $O(1)$。 - **碎块**:暴力修改元素值,并**重构有序数组**(排序),时间复杂度 $O(\sqrt{n} \log \sqrt{n})$。 2. **查询前驱($[l, r]$ 内 $< c$ 的最大值)**: - **整块**:在有序数组 $S_i$ 中二分查找 $c - add_i$ 的前驱,时间复杂度 $O(\log \sqrt{n})$。 - **碎块**:暴力遍历元素,检查 $a_j + add_i < c$ 并更新最大值,时间复杂度 $O(\sqrt{n})$。 #### 代码实现(C++) ```cpp #include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <climits> using namespace std; const int MAXN = 100010; int n, block_size; vector<int> a; vector<int> add_tag; // 块的加法标 vector<vector<int>> blocks; // 每块的有序数组 void init() { block_size = sqrt(n); int block_num = (n + block_size - 1) / block_size; add_tag.assign(block_num, 0); blocks.resize(block_num); for (int i = 0; i < n; i++) blocks[i / block_size].push_back(a[i]); for (int i = 0; i < block_num; i++) sort(blocks[i].begin(), blocks[i].end()); } void rebuild_block(int block_id) { blocks[block_id].clear(); int start = block_id * block_size; int end = min(start + block_size, n); for (int i = start; i < end; i++) blocks[block_id].push_back(a[i]); sort(blocks[block_id].begin(), blocks[block_id].end()); } void range_add(int l, int r, int c) { int block_l = l / block_size, block_r = r / block_size; if (block_l == block_r) { // 同一块内 for (int i = l; i <= r; i++) a[i] += c; rebuild_block(block_l); } else { // 左侧碎块 for (int i = l; i < (block_l + 1) * block_size; i++) a[i] += c; rebuild_block(block_l); // 中间整块 for (int i = block_l + 1; i < block_r; i++) add_tag[i] += c; // 右侧碎块 for (int i = block_r * block_size; i <= r; i++) a[i] += c; rebuild_block(block_r); } } int query_predecessor(int l, int r, int c) { int ans = -1; int block_l = l / block_size, block_r = r / block_size; // 左侧碎块暴力查询 for (int i = l; i < min(r + 1, (block_l + 1) * block_size); i++) { int val = a[i] + add_tag[block_l]; if (val < c && val > ans) ans = val; } // 中间整块二分查找 for (int i = block_l + 1; i < block_r; i++) { int target = c - add_tag[i]; auto it = lower_bound(blocks[i].begin(), blocks[i].end(), target); if (it != blocks[i].begin()) { it--; int val = *it + add_tag[i]; if (val < c && val > ans) ans = val; } } // 右侧碎块暴力查询 if (block_l != block_r) { for (int i = block_r * block_size; i <= r; i++) { int val = a[i] + add_tag[block_r]; if (val < c && val > ans) ans = val; } } return ans; } int main() { cin >> n; a.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; init(); for (int i = 0; i < n; i++) { int op, l, r, c; cin >> op >> l >> r >> c; l--; r--; // 转为0-indexed if (op == 0) range_add(l, r, c); else cout << query_predecessor(l, r, c) << endl; } return 0; } ``` #### 算法分析 - **时间复杂度**: - 单次修改/查询:$O(\sqrt{n} \log \sqrt{n})$(碎块排序主导)。 - 总操作 $m$ 次:$O(m \sqrt{n} \log n)$。 - **空间复杂度**:$O(n)$。 #### 优化技巧 1. **减少排序次数**: - 碎块修改时只重构受影响块的有序数组。 2. **块大小调整**: - 实测调整块大小为 $n^{0.6}$ 可能更快(需测试)。 #### 应用场景 分块算法适用于**强制在线**的区间问题(如 LOJ 的数列分块系列题),在 $O(\sqrt{n})$ 复杂度下平衡修改查询[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值