|
| 1 | +package jtest.com.baidu; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import static java.lang.System.out; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by YAO on 2017/2/27. |
| 12 | + * C市现在要转移一批罪犯到D市,C市有n名罪犯,按照入狱时间有顺序,另外每个罪犯有一个罪行值,值越大罪越重。 |
| 13 | + * 现在为了方便管理,市长决定转移入狱时间连续的c名犯人,同时要求转移犯人的罪行值之和不超过t,问有多少种选择的方式? |
| 14 | + */ |
| 15 | +public class MoveCriminal { |
| 16 | + |
| 17 | + public static Set<List<Criminal>> move(Prison prison, int count, int limit) { |
| 18 | + Set<List<Criminal>> ways = new HashSet<>(); |
| 19 | + List<Criminal> CriminalsInPrison = prison.getCriminals(); |
| 20 | + List<Criminal> toMove = new ArrayList<>(); |
| 21 | + |
| 22 | + int totalValue = 0; |
| 23 | + // 第一批C-1名犯人 |
| 24 | + for (int i = 0; i < count - 1; i++) { |
| 25 | + Criminal moveOne = CriminalsInPrison.get(i); |
| 26 | + toMove.add(moveOne); |
| 27 | + totalValue += moveOne.getValue(); |
| 28 | + } |
| 29 | + |
| 30 | + //滑动窗口模式 |
| 31 | + try { |
| 32 | + for (int i = count - 1, j = 0; i < CriminalsInPrison.size(); i++, j++) { |
| 33 | + Criminal moveOne = CriminalsInPrison.get(i); |
| 34 | + toMove.add(moveOne); |
| 35 | + totalValue += moveOne.getValue(); |
| 36 | + if (totalValue <= limit) { |
| 37 | + ways.add(toMove); |
| 38 | +// showTheCriminals(toMove, totalValue); |
| 39 | + } |
| 40 | + Criminal next = CriminalsInPrison.get(j); |
| 41 | + totalValue -= next.getValue(); |
| 42 | + toMove.remove(0); |
| 43 | + } |
| 44 | + } catch (IndexOutOfBoundsException e) { |
| 45 | + out.println(toMove); |
| 46 | + } |
| 47 | + return ways; |
| 48 | + } |
| 49 | + |
| 50 | + private static void showTheCriminals(List<Criminal> toMove, int totalValue) { |
| 51 | + out.println("==============================="); |
| 52 | + for (Criminal c : toMove) { |
| 53 | + out.println("id:" + c.getId()); |
| 54 | + out.println("value:" + c.getValue()); |
| 55 | + } |
| 56 | + out.println("totalValue:" + totalValue); |
| 57 | + out.println("==============================="); |
| 58 | + } |
| 59 | + |
| 60 | + public static void main(String[] args) { |
| 61 | + int criminals = 2000; |
| 62 | + int moveNum = 100; // move criminal numbers should be more then 2 |
| 63 | + int limit = 90000; // should small then criminals*moveNum,should more then min value |
| 64 | + |
| 65 | + Prison C = new Prison(criminals); |
| 66 | + Set<List<Criminal>> a = move(C, moveNum, limit); |
| 67 | + System.out.println("ways:" + a.size()); |
| 68 | + } |
| 69 | + |
| 70 | +// public static void main(String[] args){ |
| 71 | +// |
| 72 | +// Scanner scanner = new Scanner(System.in); |
| 73 | +// while (scanner.hasNext()){ |
| 74 | +// int number = scanner.nextInt(); // 罪犯总数 |
| 75 | +// int limit = scanner.nextInt(); // 犯罪值总数 |
| 76 | +// int count = scanner.nextInt(); // 移动的罪犯个数 |
| 77 | +// |
| 78 | +// int[] arr = new int[number]; |
| 79 | +// |
| 80 | +// for (int i =0;i<number;i++){ |
| 81 | +// arr[i] = scanner.nextInt(); // 每个罪犯值? |
| 82 | +// } |
| 83 | +// int total = 0; |
| 84 | +// for (int i =0; i < count -1 && i < arr.length; ++i) { |
| 85 | +// total += arr[i]; |
| 86 | +// } |
| 87 | +// int result =0; |
| 88 | +// for (int i = count -1,j=0; i < arr.length; ++i,++j){ |
| 89 | +// total += arr[i]; |
| 90 | +// if (total <= limit){ |
| 91 | +// ++result; |
| 92 | +// } |
| 93 | +// total -=arr[j]; |
| 94 | +// } |
| 95 | +// System.out.println(result); |
| 96 | +// } |
| 97 | +// scanner.close(); |
| 98 | +// } |
| 99 | +} |
0 commit comments