2019牛客暑期多校训练营(第六场)D Move(multiset+枚举)

题目链接:https://ac.nowcoder.com/acm/contest/886/D

题目大意:

  将N个数放入K个相同大小的箱子中,每一个箱子装的数的和都不能超过它的大小,求满足这个要求的最小容积。

AC代码:

 1 #include<bits/stdc++.h>
 2 #define numm ch-48
 3 #define pd putchar(' ')
 4 #define pn putchar('
')
 5 #define pb push_back
 6 #define fi first
 7 #define se second
 8 #define fre1 freopen("1.txt","r",stdin)
 9 #define fre2 freopen("2.txt","w",stdout)
10 using namespace std;
11 template <typename T>
12 void read(T &res) {
13     bool flag=false;char ch;
14     while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
15     for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
16     flag&&(res=-res);
17 }
18 template <typename T>
19 void write(T x) {
20     if(x<0) putchar('-'),x=-x;
21     if(x>9) write(x/10);
22     putchar(x%10+'0');
23 }
24 const int maxn=1e5+7;
25 const int N=60;
26 const int inf=0x3f3f3f3f;
27 const int INF=0x7fffffff;
28 const int mod=998244353;
29 typedef long long ll;
30 multiset<int>st;
31 int a[1010],n,k;
32 bool check(int x) {
33     st.clear();
34     for(int i=1;i<=n;i++)
35         st.insert(a[i]);
36     for(int i=1;i<=k;i++) {
37         int r=x;
38         while(!st.empty()) {
39             multiset<int>::iterator it=st.upper_bound(r);
40             if(it!=st.begin()) {
41                 it--;
42                 r-=*it;
43                 st.erase(it);
44             }else
45                 break;
46         }
47     }
48     if(!st.size())return true;
49     else return false;
50 }
51 int main()
52 {
53     int _,t=0;
54     read(_);
55     while(_--) {
56         read(n),read(k);
57         int l=0,sum=0;
58         for(int i=1;i<=n;i++) {
59             read(a[i]);
60             sum+=a[i];
61             l=max(a[i],l);
62         }
63         l=max(l,sum/k);
64         while(!check(l)) l++;
65         printf("Case #%d: ",++t);write(l);pn;
66     }
67     return 0;
68 }
代码在这里!
原文地址:https://www.cnblogs.com/wuliking/p/11303702.html