Codeforces Round #334(div.2)(新增不用二分代码) B
Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.
Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.
The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively.
The next line contains n space-separated integers s1, s2, ..., sn (1 ≤ s1 ≤ s2 ≤ ... ≤ sn ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.
Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.
2 1
2 5
7
4 3
2 3 5 9
9
3 2
3 5 7
8
In the first sample, Kevin must pack his two cowbells into the same box.
In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.
In the third sample, the optimal solution is {3, 5} and {7}.
贪心+二分(不用二分也可以)
还有,每个容器只能放两个,要符合条件,当然得最大的加最小的
当然,输入的数字已经是非递减的了,更多解释可以看代码注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include<stdio.h> //#include<bits/stdc++.h> #include<string.h> #include<iostream> #include<math.h> #include<sstream> #include<set> #include<queue> #include<map> #include<vector> #include<algorithm> #include<limits.h> #define inf 0x3fffffff #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define ULL unsigned long long using namespace std; const int MAXN = 100005; int n, k; LL a[MAXN]; int i,j; bool BS(LL key) { int sum=0; int l=1; int r=n; while (l<=r) { if (l!=r&&a[l]+a[r]<=key) { l++; r--; } else { r--; //哎呀,大了,把右界限减少一点 } sum++; if (sum>k) return 0; //超过限制 } return 1; } int main() { cin>>n>>k; for (i=1;i<=n;i++) { cin>>a[i]; } LL l = a[n], r = 1e12, ans; while (l <= r) { LL mid = (l + r) >> 1; if (BS(mid)) { ans = mid; r = mid - 1; //符合条件,减小右边界,我们要的是最优的 } else l = mid + 1; //不符合,左边界推,增大数值 } cout<<ans<<endl; return 0; } |
好啦,窝萌不用二分怎么办呢,这样想,反正容器最多放二个,那么我就放K个进去,不过是从后往前放的,比如是 1 2 3 4 5 K=3 那么我先把 3 4 5放进去,把它写成公式就是还剩下n-k个,接下来还是最大的和最小的相加(相对),如 2+3 1+4 最后一个是最大的数,窝萌把它和之前的和比较 ,得出 5 。(1 4)(2 3)(5)这样符合条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<stdio.h> //#include<bits/stdc++.h> #include<string.h> #include<iostream> #include<math.h> #include<sstream> #include<set> #include<queue> #include<map> #include<vector> #include<algorithm> #include<limits.h> #define inf 0x3fffffff #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define ULL unsigned long long using namespace std; int main() { int n,k; int a[100010]; int i,j; int ans1,ans2; int sum1=0,sum2=0; cin>>n>>k; for (i=0;i<n;i++) { cin>>a[i]; } sum1=a[n-1]; ans1=n-k-1;ans2=n-k; for (i=ans1;i>=0;i--) { sum1=max(sum1,a[ans1--]+a[ans2++]); } cout<<sum1<<endl; return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~