活跃农民
- 积分
- 450
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-7-3
- 最后登录
- 1970-1-1
|
package wish;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class prorityCache {
public static void main(String[] args) {
List<List<Integer>> id = new ArrayList<>();;
List<Integer> id1 = new ArrayList<Integer>();
id1.add(1); id1.add(5);
List<Integer> id2 = new ArrayList<Integer>();
id2.add(2); id2.add(0);
List<Integer> id3 = new ArrayList<Integer>();
id3.add(3); id3.add(5);
List<Integer> id4 = new ArrayList<Integer>();
id4.add(4); id4.add(0);
List<Integer> id5 = new ArrayList<Integer>();
id5.add(5); id5.add(5);
List<Integer> id6 = new ArrayList<Integer>();
id6.add(6); id6.add(0);
id.add(id1);
id.add(id2);
id.add(id3);
id.add(id4);
id.add(id5);
id.add(id6);
List<List<Integer>> callLogs = new ArrayList<>();;
List<Integer> log1 = new ArrayList<Integer>();
log1.add(1); log1.add(1);
List<Integer> log2 = new ArrayList<Integer>();
log2.add(2); log2.add(2);
List<Integer> log3 = new ArrayList<Integer>();
log3.add(3); log3.add(3);
List<Integer> log4 = new ArrayList<Integer>();
log4.add(4); log4.add(4);
List<Integer> log5 = new ArrayList<Integer>();
log5.add(5); log5.add(5);
List<Integer> log6 = new ArrayList<Integer>();
log6.add(6); log6.add(6);
callLogs.add(log1);
callLogs.add(log2);
callLogs.add(log3);
callLogs.add(log4);
callLogs.add(log5);
callLogs.add(log6);
prorityCache pc = new prorityCache();
System.out.println(pc.getCache(callLogs, id));
}
Map<Integer, Integer> cache = new HashMap<Integer, Integer>();
Map<Integer, Integer> disk = new HashMap<Integer, Integer>();
public List<Integer> getCache(List<List<Integer>> callLogs, List<List<Integer>> id){
List<Integer> result = new LinkedList<Integer>();
initial(id);
for(List<Integer> cl : callLogs) {
//System.out.println("------------------");
process(cl.get(1));
/*
* System.out.println("cache"); for(Map.Entry<Integer, Integer> c:
* cache.entrySet()) { System.out.println(c.getKey() + " "+ c.getValue()); }
* System.out.println("disk"); for(Map.Entry<Integer, Integer> d:
* disk.entrySet()) { System.out.println(d.getKey() + " "+ d.getValue()); }
*/
}
if(cache.size() == 0) {
result.add(-1);
}
for(Map.Entry<Integer, Integer> m : cache.entrySet()) {
result.add(m.getKey());
}
Collections.sort(result);
return result;
}
public void initial(List<List<Integer>> id) {
for(List<Integer> i : id) {
if(i.get(1) > 5) {
cache.put(i.get(0), i.get(1));
} else {
disk.put(i.get(0), i.get(1));
}
}
}
public void process(int id) {
addPrority(id);
reducePrority();
moveTask();
}
public void addPrority(int id) {
if(disk.containsKey(id)) {
disk.put(id, disk.get(id) + 3);
}
if(cache.containsKey(id)) {
cache.put(id, cache.get(id) + 3);
}
}
public void reducePrority() {
for(Map.Entry<Integer, Integer> c: cache.entrySet()) {
cache.put(c.getKey(),c.getValue() - 1 < 0 ? 0 : c.getValue() - 1);
}
for(Map.Entry<Integer, Integer> d: disk.entrySet()) {
disk.put(d.getKey(),d.getValue() - 1 < 0 ? 0 : d.getValue() - 1);
}
}
public void moveTask() {
List<Integer> cRemove = new ArrayList<>();
for(Map.Entry<Integer, Integer> c: cache.entrySet()) {
if(c.getValue() <= 3) {
cRemove.add(c.getKey());
disk.put(c.getKey(), c.getValue());
}
}
for(int c: cRemove) {
cache.remove(c);
}
List<Integer> dRemove = new ArrayList<>();
for(Map.Entry<Integer, Integer> d: disk.entrySet()) {
if(d.getValue() > 5) {
dRemove.add(d.getKey());
cache.put(d.getKey(), d.getValue());
}
}
for(int d: dRemove) {
disk.remove(d);
}
}
}
|
|