為了充分發揮Gpu算力,需要盡可能多的將任務交給GPU執行,現在有一個任務數組,
數組元素表示在這1s內新增的任務個數,且每秒都有新增任務,
假設GPU最多一次執行n個任務,一次執行耗時1s,在保證Gpu不空閑的情況下,最少需要多長時間執行完成。
輸入描述
第一個參數為gpu最多執行的任務個數, 取值范圍1~10000
第二個參數為任務數組的長度,取值范圍1~10000
第三個參數為任務數組,數字范圍1~10000
輸出描述
執行完所有任務需要多少秒
例子
輸入
3
5
1 2 3 4 5
輸出
6
說明,一次最多執行3個任務 最少耗時6s
例子2
輸入
4
5
5 4 1 1 1
輸出
5
說明,一次最多執行4個任務 最少耗時5s
查看代碼
import java.util.*;
public class Demo4 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int k = Integer.parseInt(sc.nextLine());
int len = Integer.parseInt(sc.nextLine());
String[] split = sc.nextLine().split(" ");
int[] ints = new int[len];
for(int i = 0; i < len; i++){
ints[i] = Integer.parseInt(split[i]);
}
int more = 0;
int time = 0;
for(int i : ints){ //關鍵對變量more的處理
if(more + i > k) more = more + i - k;
else more = 0;
time++;
}
while(more > 0){
more -= k;
time++;
}
System.out.println(time);
}
}
}
問題的關鍵在於,用more來記錄上一次操作對下一次的影響。
