Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我哭啊,这道题我做了一年了,WAde实在不行了。是不是有什么肮脏的数据阿,携代码求救!//java de import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.TreeSet; public final class Main{ final static int nextInt(){ int v = 0; char t = 0; boolean neg=false; try { while (!Character.isDigit(t = (char) System.in.read()) && t!='-') ; if(t=='-'){ neg=true; t='0'; } do { v *= 10; v += t - '0'; } while (Character.isDigit(t = (char) System.in.read())); } catch (IOException e) { } return neg?-v:v; } static final long EMPTY = Long.MIN_VALUE; static int N; static long TIME; static final class Block implements Comparable<Block>{ Block next,last; int start,size; long life; Block(int start,int size,long life){this.start=start;this.size=size;this.life=life;} boolean near(Block b){return start+size==b.start||b.start+b.size==start;} boolean expire(){return life==TIME;} boolean empty(){return life==EMPTY;} public int compareTo(Block o) {return start>o.start?1:(start==o.start?0:-1);} @Override public boolean equals(Object obj) { return ((Block)obj).start == start; } @Override public int hashCode() { return start; } @Override public String toString(){ String c = ""; if(this.empty()) c = " "; else if(this.life>0){ c = " "+Long.toString(life); c = c.substring(c.length()-3); } String result = ""; for(int i=0;i<this.size;i++) result += c; if(next!=Running.end()) result += next.toString(); return result; } } static final class Request{ int size,life; long time; static final ArrayList<Request> requestList = new ArrayList<Request>(); Request(long time,int size,int life){this.time=time;this.size=size;this.life=life;} static final void append(long time,int size,int life){requestList.add(new Request(time,size,life));} static final ArrayList<Request> list(){return requestList;} } static final class Waiting{ static final ArrayList<Request> questList = new ArrayList<Request>(); static int questIndex = 0; static Request top(){return size()>0?questList.get(questIndex):null;} static void push(Request r){questList.add(r);} static int all(){return questList.size();} static Request pop(){return questList.get(questIndex++);} static int size(){return questList.size()-questIndex;} } static final class Running{ static final Block head=new Block(-1,-1,EMPTY+1); static final Block tail=new Block(-1,-1,EMPTY+1); static final void init(int n){head.next=tail;tail.last=head;insert(new Block(0,n,EMPTY));} static final void insert(Block b){b.next = head.next;b.last = head;head.next.last = b;head.next = b;} static final void append(Block b){b.next=tail;b.last=tail.last;tail.last.next=b;tail.last=b;} static final Block begin(){return head.next;} static final Block end(){return tail;} static final Block find(int size){ for(Block b=begin();b!=end();b=b.next) if(b.size>=size&&b.empty()) return b; return null; } static final void free(Block b){ b.life = EMPTY; if(b.last.empty()){ b.size+=b.last.size; Block lb=b.last.last; lb.next = b; b.last = lb; } if(b.next.empty()){ b.size += b.next.size; Block nb=b.next.next; b.next = nb; nb.last = b; } } static final Block make(Block b,Request r){ if(b.size<r.size || !b.empty()) return null; Block result = new Block(b.start+r.size,b.size-r.size,EMPTY); result.last = b; result.next = b.next; b.life = TIME+r.life; b.size = r.size; if(result.size==0) return null; b.next.last = result; b.next = result; return result; } } static final class Event{ final static TreeSet<Long> eventSet = new TreeSet<Long>(); final static void add(long time){eventSet.add(time);} final static long next(){ ArrayList<Long> toRemove = new ArrayList<Long>(); Long result = Long.MIN_VALUE; for(Long i:eventSet){ if(i>TIME){ result = i; break; }else if(i<TIME) toRemove.add(i); } for(Long i:toRemove) eventSet.remove(i); eventSet.remove(TIME); return result; } } static final HashSet<Block> Taken=new HashSet<Block>(); static final boolean process(Request r, boolean alreadyWaiting){ if(r==null) return false; Block b=Running.find(r.size); if(b==null){ if(!alreadyWaiting) Waiting.push(r); return false; } Block nb = Running.make(b, r); if(!b.empty()){ Taken.add(b); Event.add(b.life); } return true; } public static final void main(String[] args) throws Exception{ N=nextInt(); Running.init(N); while(true){ int t=nextInt(),s=nextInt(),l=nextInt(); if(t==0&&s==0&&l==0) break; Request.append(t, s, l); Event.add(t); } if(Request.list().size()==0){ System.out.println(0); System.out.println(0); return; }else{ TIME=Request.list().get(0).time - 1; } int requestIndex=0; while(true){ long newTIME = Event.next(); if(newTIME == Long.MIN_VALUE) break; TIME = newTIME; ArrayList<Block> toBeFreed = new ArrayList<Block>(); for(Block b:Taken) if(b.life==TIME){ Running.free(b); toBeFreed.add(b); }else if(b.life<TIME){ //if(Event.eventSet.contains(b.life)) throw new Exception("cao "); } for(Block b:toBeFreed) Taken.remove(b); while(true){ if(Waiting.size()<=0) break; if(!process(Waiting.top(), true)) break; Waiting.pop(); } for(;requestIndex<Request.list().size();){ if(Request.list().get(requestIndex).time == TIME){ process(Request.list().get(requestIndex),false); requestIndex++; }else if(Request.list().get(requestIndex).time < TIME){ throw new Exception("cao "); }else{ break; } } //System.out.println(Running.begin()+ " "+TIME); } System.out.println(TIME); System.out.println(Waiting.all()); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator