AOJ 802.运输宝物

Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB
Total Submission: 53   Submission Accepted: 22
Description
众所周知,“西瓜”是大名鼎鼎的江洋大盗。有一次他偷到了一批宝库。
这批宝物共有n个,他一共有k个箱子。他只能用这些箱子把这些宝物运出去,为了保证运输安全,他不会把两个以上的宝物装入同一个箱子(一个箱子只能装1个或者2个宝物)。这些宝物的大小分别是s(1)、s(2)、s(3)……s(n)。(题目给出的重量保证是非降序,即s(i-1)<=s(i) 对于任何i>1)。
装进宝物后,每个箱子的容量要大于或者等于所装的宝物大小之和。为了规格统一,这些箱子每个的容量要一致。为了降低运费,箱子的容量要尽可能小。“西瓜”想要知道,在能运走的情况下,箱子容量最小是多少。
Input
多组输入
先输入n和k (1≤n≤2·k≤100 000),n是宝物数量,k是箱子数量。
下一行输入空格分隔的n个整数, s1,s2,...,sn (1≤s1≤s2≤...≤sn≤1 000 000),代表这些宝物的重量。
Output
输出一个整数,代表这些箱子容量的最小值。
Sample Input
Original Transformed
4 3
2 3 5 9
Sample Output
Original Transformed
9

 只需将宝物按照从大到小装箱,然后再将剩下的宝物按照从大到小装入从小到大的箱子中

最后求出所有箱子中最大值即可

 1 #include <cstdio>
 2 #include <string>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <memory>
 6 #include <stack>
 7 #include <queue>
 8 #include <set>
 9 #include <algorithm>
10 #include <map>
11 #include <vector>
12 using namespace std;
13  
14 #define debug 0
15  
16 /*
17 By:OhYee
18 Github:OhYee
19 Email:oyohyee@oyohyee.com
20 Blog:http://www.cnblogs.com/ohyee/
21 
22 かしこいかわいい?
23 エリーチカ!
24 要写出来Хорошо的代码哦~
25 */
26 #define REP(n) for(int o=0;o<n;o++)
27 const int maxn=100005;
28 const int maxk=50005;
29  
30 int n,k;
31 int s[maxn];
32  
33 bool Do(){
34     if(scanf("%d%d",&n,&k)==EOF)
35         return false;
36     REP(n)
37         scanf("%d",&s[o]);
38  
39     if(k>=n){
40         printf("%d
",s[n-1]);
41         return true;
42     }
43  
44     int w[maxk];
45     REP(k){
46         w[o]=s[n-k+o];
47     }
48  
49     #if debug
50     REP(n)
51         printf("s[%d]=%d
",o,s[o]);
52     printf("
");
53     REP(k)
54         printf("w[%d]=%d
",o,w[o]);
55     printf("
");
56     #endif // debug
57  
58     for(int i=0;i<n-k;i++){
59         w[i]+=s[n-k-1-i];
60     }
61  
62  
63  
64     int M=w[0];
65     REP(k){
66         M=max(M,w[o]);
67     }
68     #if debug
69     REP(k)
70         printf("w[%d]=%d
",o,w[o]);
71     #endif // debug
72     printf("%d
",M);
73  
74     return true;
75  
76 }
77  
78  
79  
80 int main(){
81     #if debug
82     freopen("in.txt","r",stdin);
83     #endif
84     while(Do());
85     return 0;
86 }
原文地址:https://www.cnblogs.com/ohyee/p/5375126.html