[LightOJ] [1048] Conquering Keokradong

Problem Description

Loading...

Problem Analysis

This is the same problem as 【Light】[1076 ]Get the Containers

However, after finding the minimum possible maximum value,
you also need to output the grouping
and prioritize earlier numbers when grouping.

Note that
the grouping may be completed before all elements are processed,
so the remaining elements do not need to be grouped.
For example

1
2
3
4
5
6
7
8
9
1
6 4
10
5
5
5
5
5
5

should output

1
2
3
4
5
6
Case 1: 10
10
10
10
5
5

If you simply check the condition, all outputs will be 10.

Here we keep track of the number of outputs.
If the remaining number of outputs equals the remaining number of elements,
then output the number directly
and there is no need to check whether to group it.

cpp
 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
#include<stdio.h>
int a[1200];
int n,m;
bool judge(int t) {
	int cnt=1;
	int sum=0;
	for(int i=0; i<n; i++) {
		if(sum+a[i]>t) {
			cnt++;
			sum=0;
		}
		sum+=a[i];
	}
	if(cnt<=m)
		return true;
	else
		return false;
}
int main() {
	int T,kase=0;
	scanf("%d",&T);
	while(T--) {
		scanf("%d %d",&n,&m);
		n++;
		m++;
		int max=0,sum=0;
		for(int i=0; i<n; i++) {
			scanf("%d",&a[i]);
			sum+=a[i];
			if(max<a[i])
				max=a[i];
		}
		int l=max,r=sum;
		int res=max;
		while(r>=l) {
			int mid=(l+r)/2;
			if(judge(mid)) {
				res=mid;
				r=mid-1;
			} else
				l=mid+1;
		}
		printf("Case %d: %d\n",++kase,res);
		sum=0;
		int cnt=m;
		for(int i=0; i<n; i++) {
			if(sum+a[i]>res||cnt==n-i+1) {
				printf("%d\n",sum);
				cnt--;
				sum=0;
			}
			sum+=a[i];
		}
		printf("%d\n",sum);
	}
	return 0;
}

Problem address: 【Light】[1048]Conquering Keokradong

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...