modify code

pull/6/head
左程云 5 years ago
parent c2fd0e4e3a
commit 6c206c6ebf

@ -10,12 +10,12 @@ public class Code02_EveryStepShowBoss {
public static class Customer { public static class Customer {
public int value; public int value;
public int buy; public int buy;
public int order; public int enterTime;
public Customer(int v, int b, int o) { public Customer(int v, int b, int o) {
value = v; value = v;
buy = b; buy = b;
order = 0; enterTime = 0;
} }
} }
@ -23,7 +23,7 @@ public class Code02_EveryStepShowBoss {
@Override @Override
public int compare(Customer o1, Customer o2) { public int compare(Customer o1, Customer o2) {
return o1.buy != o2.buy ? (o2.buy - o1.buy) : (o1.order - o2.order); return o1.buy != o2.buy ? (o2.buy - o1.buy) : (o1.enterTime - o2.enterTime);
} }
} }
@ -32,7 +32,7 @@ public class Code02_EveryStepShowBoss {
@Override @Override
public int compare(Customer o1, Customer o2) { public int compare(Customer o1, Customer o2) {
return o1.buy != o2.buy ? (o1.buy - o2.buy) : (o1.order - o2.order); return o1.buy != o2.buy ? (o1.buy - o2.buy) : (o1.enterTime - o2.enterTime);
} }
} }
@ -42,19 +42,15 @@ public class Code02_EveryStepShowBoss {
private HeapGreater<Customer> candHeap; private HeapGreater<Customer> candHeap;
private HeapGreater<Customer> daddyHeap; private HeapGreater<Customer> daddyHeap;
private final int daddyLimit; private final int daddyLimit;
public int ctime;
public int dtime;
public WhosYourDaddy(int limit) { public WhosYourDaddy(int limit) {
customers = new HashMap<Integer, Customer>(); customers = new HashMap<Integer, Customer>();
candHeap = new HeapGreater<>(new CandidateComparator()); candHeap = new HeapGreater<>(new CandidateComparator());
daddyHeap = new HeapGreater<>(new DaddyComparator()); daddyHeap = new HeapGreater<>(new DaddyComparator());
daddyLimit = limit; daddyLimit = limit;
ctime = 0;
dtime = 0;
} }
public void operate(int number, boolean buyOrRefund) { public void operate(int time, int number, boolean buyOrRefund) {
if (!buyOrRefund && !customers.containsKey(number)) { if (!buyOrRefund && !customers.containsKey(number)) {
return; return;
} }
@ -72,10 +68,10 @@ public class Code02_EveryStepShowBoss {
} }
if (!candHeap.contains(c) && !daddyHeap.contains(c)) { if (!candHeap.contains(c) && !daddyHeap.contains(c)) {
if (daddyHeap.size() < daddyLimit) { if (daddyHeap.size() < daddyLimit) {
c.order = dTime++; c.enterTime = time;
daddyHeap.push(c); daddyHeap.push(c);
} else { } else {
c.order = cTime++; c.enterTime = time;
candHeap.push(c); candHeap.push(c);
} }
} else if (candHeap.contains(c)) { } else if (candHeap.contains(c)) {
@ -91,7 +87,7 @@ public class Code02_EveryStepShowBoss {
daddyHeap.resign(c); daddyHeap.resign(c);
} }
} }
daddyMove(); daddyMove(time);
} }
public List<Integer> getDaddies() { public List<Integer> getDaddies() {
@ -103,20 +99,20 @@ public class Code02_EveryStepShowBoss {
return ans; return ans;
} }
private void daddyMove() { private void daddyMove(int time) {
if (candHeap.isEmpty()) { if (candHeap.isEmpty()) {
return; return;
} }
if (daddyHeap.size() < daddyLimit) { if (daddyHeap.size() < daddyLimit) {
Customer p = candHeap.pop(); Customer p = candHeap.pop();
p.order = dTime++; p.enterTime = time;
daddyHeap.push(p); daddyHeap.push(p);
} else { } else {
if (candHeap.peek().buy > daddyHeap.peek().buy) { if (candHeap.peek().buy > daddyHeap.peek().buy) {
Customer oldDaddy = daddyHeap.pop(); Customer oldDaddy = daddyHeap.pop();
Customer newDaddy = candHeap.pop(); Customer newDaddy = candHeap.pop();
oldDaddy.order = cTime++; oldDaddy.enterTime = time;
newDaddy.order = dTime++; newDaddy.enterTime = time;
daddyHeap.push(newDaddy); daddyHeap.push(newDaddy);
candHeap.push(oldDaddy); candHeap.push(oldDaddy);
} }
@ -129,19 +125,14 @@ public class Code02_EveryStepShowBoss {
List<List<Integer>> ans = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>();
WhosYourDaddy whoDaddies = new WhosYourDaddy(k); WhosYourDaddy whoDaddies = new WhosYourDaddy(k);
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
whoDaddies.operate(arr[i], op[i]); whoDaddies.operate(i, arr[i], op[i]);
ans.add(whoDaddies.getDaddies()); ans.add(whoDaddies.getDaddies());
} }
return ans; return ans;
} }
public static int cTime = 0;
public static int dTime = 0;
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<>();
cTime = 0;
dTime = 0;
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<>();
@ -166,10 +157,10 @@ public class Code02_EveryStepShowBoss {
} }
if (!cands.contains(c) && !daddy.contains(c)) { if (!cands.contains(c) && !daddy.contains(c)) {
if (daddy.size() < k) { if (daddy.size() < k) {
c.order = dTime++; c.enterTime = i;
daddy.add(c); daddy.add(c);
} else { } else {
c.order = cTime++; c.enterTime = i;
cands.add(c); cands.add(c);
} }
} }
@ -177,20 +168,20 @@ public class Code02_EveryStepShowBoss {
cleanZeroBuy(daddy); cleanZeroBuy(daddy);
cands.sort(new CandidateComparator()); cands.sort(new CandidateComparator());
daddy.sort(new DaddyComparator()); daddy.sort(new DaddyComparator());
move(cands, daddy, k); move(cands, daddy, k, i);
ans.add(getCurAns(daddy)); ans.add(getCurAns(daddy));
} }
return ans; return ans;
} }
public static void move(ArrayList<Customer> cands, ArrayList<Customer> daddy, int k) { public static void move(ArrayList<Customer> cands, ArrayList<Customer> daddy, int k, int time) {
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.order = dTime++; c.enterTime = time;
daddy.add(c); daddy.add(c);
cands.remove(0); cands.remove(0);
} else { } else {
@ -199,8 +190,8 @@ public class Code02_EveryStepShowBoss {
daddy.remove(0); daddy.remove(0);
Customer newDaddy = cands.get(0); Customer newDaddy = cands.get(0);
cands.remove(0); cands.remove(0);
newDaddy.order = dTime++; newDaddy.enterTime = time;
oldDaddy.order = cTime++; oldDaddy.enterTime = time;
daddy.add(newDaddy); daddy.add(newDaddy);
cands.add(oldDaddy); cands.add(oldDaddy);
} }
@ -274,9 +265,9 @@ public class Code02_EveryStepShowBoss {
} }
public static void main(String[] args) { public static void main(String[] args) {
int maxValue = 20; int maxValue = 10;
int maxLen = 200; int maxLen = 100;
int maxK = 10; int maxK = 6;
int testTimes = 100000; int testTimes = 100000;
System.out.println("测试开始"); System.out.println("测试开始");
for (int i = 0; i < testTimes; i++) { for (int i = 0; i < testTimes; i++) {

Loading…
Cancel
Save