360笔试4.2
第一题是主播打赏,只按照7天内的总打赏价值最高的为榜首,要求维持榜首n天。求最低需要总共打赏多少才行。
第一行 需要维持的天数,参与人数
后面n行为每个人数每天打赏的钱。
例如
8 1
1
2
3
4
5
6
7
8
输出 38
但是不知道为啥只过了27,有大佬可以看看我的思路和代码嘛,
思路是前缀和,dp[j][i],保存第j个人 第i天的打赏总价值。使用一个变量holdValue 来记录当前已经刷的金额(7天内的)
dp[j][i],超过7天也要每次减去7天前累积的金额。
最后只有出现holdValue 比其他人累积金额少的情况才补充金额。累加所有补充的金额为输出结果。
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
int people = sc.nextInt();
int[][] dp = new int[people][day];
sc.nextLine();
int i = 0;
int[] dpValue = new int[day];
int count = 0;
int holdValue = 0;
while(i < day){
int j = 0;
int max = Integer.MIN_VALUE;
while(j < people){
//第j个人 第i天
if(i >= 7){
dp[j][i] = dp[j][i-1]+sc.nextInt()-dp[j][i-7];
}
else if(i > 0 && i < 7)dp[j][i] = dp[j][i-1]+sc.nextInt();
else dp[j][i] = sc.nextInt();
max = Math.max(dp[j][i],max);
j++;
}
if(i >= 7)holdValue = holdValue - dpValue[i-7];
sc.nextLine();
dpValue[i] = max+1;
if(holdValue < dpValue[i]){
int s = dpValue[i]-holdValue;
count += s;
holdValue = dpValue[i];
}
i++;
}
System.out.println(count);
}
}
第二题,给你四个数A,B,C,D 。表示 A个单人队伍,B个2人队伍,C个三人队伍,D个四人队伍,队伍不能拆分只能合并,要求输出最大能组成4人队伍的个数。贪心,优先1 3组合,然后2 2 组合 ,然后2 1 组合,最后是 1自己组。#360笔试##春招##360公司#