而且每个盒子最多只能放两个物品,这两个物品的权值和不能超过盒子的权值。求相同的k个盒子的最小权值是多少
解题思路:
因为最多放两个所以只能是比较 arr[2*(k-1)+1]与 arr[0],类似。。可推一下。。。
上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 1e5+5; const int mod = 1e9+7; const double eps = 1e-8; const int INF = 0x3f3f3f3f; LL gcd(LL a, LL b) { if(b == 0) return a; return gcd(b, a%b); } int arr[maxn]; int dp[maxn]; int main() { int n, k; while(cin>>n>>k) { for(int i=0; i<n; i++) scanf("%d",&arr[i]); k = n - k; if(k < 0) { cout<<arr[n-1]<<endl; continue; } int Max = arr[n-1]; for(int i=0; i<k; i++) Max = max(Max, arr[2*k-1-i]+arr[i]); cout<<Max<<endl; } return 0; }