0,1揹包問題:html
定義V(i,j):當前揹包容量 j,前 i 個物品最佳組合對應的價值;java
遞推關係式:編程
1) j<w(i) V(i,j)=V(i-1,j)數組
2) j>=w(i) V(i,j)=max{ V(i-1,j),V(i-1,j-w(i))+v(i) }ide
參考:動態規劃-01揹包問題spa
網易2017春招筆試編程題集合:3d
雙核處理:code
兩個CPU,多個任務;求最小時間;htm
輸入:blog
5 3072 3072 7168 3072 1024
輸出:
9216
解法:
動態規劃問題;以總時長的一半做爲容量,則對小於等於容量一半進行動態規劃,sum減去動態規劃的結果即爲cpu中長的時間,即爲結果;
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] val = new int[n + 1]; int sum = 0; for (int i = 1; i <= n; i++) { val[i] = sc.nextInt() / 1024; sum += val[i]; } int[][] rst = new int[n + 1][sum / 2 + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= sum/2; j++) { if (j < val[i]) rst[i][j] = rst[i - 1][j]; else rst[i][j] = Math.max(rst[i - 1][j], rst[i - 1][j - val[i]] + val[i]); } } System.out.println((sum - rst[n][sum / 2]) * 1024); } } }
趕去公司:
主要注意絕對值的處理;
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int[] tX = new int[num]; int[] tY = new int[num]; for (int i = 0; i < num; i++) { tX[i] = sc.nextInt(); } for (int i = 0; i < num; i++) { tY[i] = sc.nextInt(); } int gx = sc.nextInt(); int gy = sc.nextInt(); int walkTime = sc.nextInt(); int taxiTime = sc.nextInt(); int rst = walkTime * (Math.abs(gx) + Math.abs(gy)); for (int i = 0; i < num; i++) { rst = Math.min(rst, walkTime * (Math.abs(tX[i]) + Math.abs(tY[i])) + taxiTime * (Math.abs(gx - tX[i]) + Math.abs(gy - tY[i]))); } System.out.println(rst); } }
調整隊形:
B和G組成的字符串,調整爲B和G分開,只能相鄰調整,求最小次數;
解法:兩種狀況:B都在左邊和B都在右邊;遍歷,計算每一個字符移動到最左邊須要的次數,比較兩種狀況最小值。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int bSum = 0; int gSum = 0; int bIndex = 0; int gIndex = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'B') { bSum += i - bIndex++; } else gSum += i - gIndex++; } System.out.print(Math.min(bSum, gSum)); } }
消除重複元素:
重複元素,保留後出現的;
輸入輸出描述與示例:
輸入描述: 輸入包括兩行: 第一行爲序列長度n(1 ≤ n ≤ 50) 第二行爲n個數sequence[i](1 ≤ sequence[i] ≤ 1000),以空格分隔 輸出描述: 輸出消除重複元素以後的序列,以空格分隔,行末無空格 輸入例子1: 9 100 100 100 99 99 99 100 100 100 輸出例子1: 99 100
解法:
首先數組讀取數字,而後遍歷數組使用HashSet保存全部結果;
再倒序遍歷數組,判斷set是否包含array[i],包含的話就list.add(0, arr[i]);最後遍歷list輸出str.trim();
import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int len = sc.nextInt(); int[] array = new int[len]; for (int i = 0; i < len; i++) { array[i] = sc.nextInt(); } HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < len; i++) { set.add(array[i]); } ArrayList<Integer> list = new ArrayList<>(); for (int i = len - 1; i >= 0; i--) { if (set.contains(array[i])) { list.add(0, array[i]); set.remove(array[i]); } } String str = ""; for (int i = 0; i < list.size(); i++) { str += list.get(i) + " "; } System.out.print(str.trim()); } }
魔力手環:
k次變換,每次變換爲a[i] = a[i] + a[i + 1];其中a[n - 1] = a[n - 1] + a[0];
常規O(nk)解法,只能經過40%:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = sc.nextInt(); } for (int cnt = 0; cnt < k; cnt++) { int tmp = array[0] % 100; for (int i = 0; i < n; i++) { if (i != n - 1) array[i] = array[i] + array[i + 1]; else array[i] = array[i] + tmp; if (array[i] > 100) array[i] = array[i] % 100; } } String str = ""; for (int i = 0; i < n; i++) { str += array[i] + " "; } System.out.print(str.trim()); } }
解法:快速冪;不太會;
工做安排: