题目链接
有 3030 个篮子,每个篮子里有若干个苹果,篮子里的苹果数序列已经给出。
现在要把苹果分给小朋友们,每个小朋友要么从一个篮子里拿三个苹果,要么从相邻的三个篮子里各拿一个苹果。
苹果可以剩余,而且不同的拿法会导致不同的答案。比如对于序列3 1 3 ,可以分给两个小朋友变成0 1 0;也可以分给一个小朋友变成2 0 2,此时不能继续再分了。所以答案是 22 。
求问对于以下序列,最多分给几个小朋友?
样例
7 2 12 5 9 9 8 10 7 10 5 4 5 8 4 4 10 11 3 8 7 8 3 2 1 6 3 9 7 1
思路
贪心,尽可能在一个篮子里拿3,从左到右遍历,能拿就拿,不能拿就拿后面的人的来凑。
import java.util.Scanner;
public class Main {
static int[] w = {1,5,10,50,100,500};
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int[] a = new int[32];
for(int i = 0; i < 30; i++) {
a[i] = sc.nextInt();
}
int res = 0;
for(int i = 0; i < 30; i++) {
int t = a[i]/3;
a[i] -= t*3;
res += t;
while(a[i]>0 && a[i+1]>0 && a[i+2]>0) {
res++;
a[i]--;
a[i+1]--;
a[i+2]--;
}
}
System.out.println(res);
}
}