modify on class

pull/6/head
左程云 4 years ago
parent d3542266f9
commit 8acd2c6485

@ -3,9 +3,7 @@ package class04_06;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet;
public class Code01_Comparator { public class Code01_Comparator {

@ -32,8 +32,7 @@ public class Code01_CoverMax {
lines[i] = new Line(m[i][0], m[i][1]); lines[i] = new Line(m[i][0], m[i][1]);
} }
Arrays.sort(lines, new StartComparator()); Arrays.sort(lines, new StartComparator());
// lines // 小根堆,每一条线段的结尾数值,使用默认的
//
PriorityQueue<Integer> heap = new PriorityQueue<>(); PriorityQueue<Integer> heap = new PriorityQueue<>();
int max = 0; int max = 0;
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {

@ -8,12 +8,12 @@ import java.util.List;
public class Code02_EveryStepShowBoss { public class Code02_EveryStepShowBoss {
public static class Customer { public static class Customer {
public int value; public int id;
public int buy; public int buy;
public int enterTime; public int enterTime;
public Customer(int v, int b, int o) { public Customer(int v, int b, int o) {
value = v; id = v;
buy = b; buy = b;
enterTime = 0; enterTime = 0;
} }
@ -50,21 +50,22 @@ public class Code02_EveryStepShowBoss {
daddyLimit = limit; daddyLimit = limit;
} }
public void operate(int time, int number, boolean buyOrRefund) { // 当前处理i号事件arr[i] -> id, buyOrRefund
if (!buyOrRefund && !customers.containsKey(number)) { public void operate(int time, int id, boolean buyOrRefund) {
if (!buyOrRefund && !customers.containsKey(id)) {
return; return;
} }
if (!customers.containsKey(number)) { if (!customers.containsKey(id)) {
customers.put(number, new Customer(number, 0, 0)); customers.put(id, new Customer(id, 0, 0));
} }
Customer c = customers.get(number); Customer c = customers.get(id);
if (buyOrRefund) { if (buyOrRefund) {
c.buy++; c.buy++;
} else { } else {
c.buy--; c.buy--;
} }
if (c.buy == 0) { if (c.buy == 0) {
customers.remove(number); customers.remove(id);
} }
if (!candHeap.contains(c) && !daddyHeap.contains(c)) { if (!candHeap.contains(c) && !daddyHeap.contains(c)) {
if (daddyHeap.size() < daddyLimit) { if (daddyHeap.size() < daddyLimit) {
@ -94,7 +95,7 @@ public class Code02_EveryStepShowBoss {
List<Customer> customers = daddyHeap.getAllElements(); List<Customer> customers = daddyHeap.getAllElements();
List<Integer> ans = new ArrayList<>(); List<Integer> ans = new ArrayList<>();
for (Customer c : customers) { for (Customer c : customers) {
ans.add(c.value); ans.add(c.id);
} }
return ans; return ans;
} }
@ -131,30 +132,38 @@ public class Code02_EveryStepShowBoss {
return ans; return ans;
} }
// 干完所有的事,模拟,不优化
public static List<List<Integer>> compare(int[] arr, boolean[] op, int k) { public static List<List<Integer>> compare(int[] arr, boolean[] op, int k) {
HashMap<Integer, Customer> map = new HashMap<>(); HashMap<Integer, Customer> map = new HashMap<>();
ArrayList<Customer> cands = new ArrayList<>(); ArrayList<Customer> cands = new ArrayList<>();
ArrayList<Customer> daddy = new ArrayList<>(); ArrayList<Customer> daddy = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
int num = arr[i]; int id = arr[i];
boolean buyOrRefund = op[i]; boolean buyOrRefund = op[i];
if (!buyOrRefund && !map.containsKey(num)) { if (!buyOrRefund && !map.containsKey(id)) {
ans.add(getCurAns(daddy)); ans.add(getCurAns(daddy));
continue; continue;
} }
if (!map.containsKey(num)) { // 没有发生用户购买数为0并且又退货了
map.put(num, new Customer(num, 0, 0)); // 用户之前购买数是0此时买货事件
// 用户之前购买数>0 此时买货
// 用户之前购买数>0, 此时退货
if (!map.containsKey(id)) {
map.put(id, new Customer(id, 0, 0));
} }
Customer c = map.get(num); // 买、卖
Customer c = map.get(id);
if (buyOrRefund) { if (buyOrRefund) {
c.buy++; c.buy++;
} else { } else {
c.buy--; c.buy--;
} }
if (c.buy == 0) { if (c.buy == 0) {
map.remove(num); map.remove(id);
} }
// c
// 下面做
if (!cands.contains(c) && !daddy.contains(c)) { if (!cands.contains(c) && !daddy.contains(c)) {
if (daddy.size() < k) { if (daddy.size() < k) {
c.enterTime = i; c.enterTime = i;
@ -171,7 +180,6 @@ public class Code02_EveryStepShowBoss {
move(cands, daddy, k, i); move(cands, daddy, k, i);
ans.add(getCurAns(daddy)); ans.add(getCurAns(daddy));
} }
return ans; return ans;
} }
@ -179,12 +187,13 @@ public class Code02_EveryStepShowBoss {
if (cands.isEmpty()) { if (cands.isEmpty()) {
return; return;
} }
// 候选区不为空
if (daddy.size() < k) { if (daddy.size() < k) {
Customer c = cands.get(0); Customer c = cands.get(0);
c.enterTime = time; c.enterTime = time;
daddy.add(c); daddy.add(c);
cands.remove(0); cands.remove(0);
} else { } else { // 等奖区满了,候选区有东西
if (cands.get(0).buy > daddy.get(0).buy) { if (cands.get(0).buy > daddy.get(0).buy) {
Customer oldDaddy = daddy.get(0); Customer oldDaddy = daddy.get(0);
daddy.remove(0); daddy.remove(0);
@ -214,7 +223,7 @@ public class Code02_EveryStepShowBoss {
public static List<Integer> getCurAns(ArrayList<Customer> daddy) { public static List<Integer> getCurAns(ArrayList<Customer> daddy) {
List<Integer> ans = new ArrayList<>(); List<Integer> ans = new ArrayList<>();
for (Customer c : daddy) { for (Customer c : daddy) {
ans.add(c.value); ans.add(c.id);
} }
return ans; return ans;
} }

@ -5,6 +5,9 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
/*
* T
*/
public class HeapGreater<T> { public class HeapGreater<T> {
private ArrayList<T> heap; private ArrayList<T> heap;
@ -67,6 +70,7 @@ public class HeapGreater<T> {
heapify(indexMap.get(obj)); heapify(indexMap.get(obj));
} }
// 请返回堆上的所有元素
public List<T> getAllElements() { public List<T> getAllElements() {
List<T> ans = new ArrayList<>(); List<T> ans = new ArrayList<>();
for (T c : heap) { for (T c : heap) {

@ -0,0 +1,9 @@
package class04_07;
public class Inner<T> {
public T value;
public Inner(T v) {
value = v;
}
}
Loading…
Cancel
Save