poj1193 内存分配

气死我了...这个毒瘤内存分配.....

优先队列 + 链表模拟,看起来搞一搞就好了却WA来WA去...

最后对拍手动找才发现错误:

erase的时候不要急急忙忙插入wait!要把同一时期的erase完再插入wait!

  1 #include <cstdio>
  2 #include <queue>
  3 typedef long long LL;
  4 const LL N = 50010;
  5 const LL INF = 1ll << 62;
  6 
  7 struct Ask {
  8     LL time, len, last;
  9     Ask(LL t, LL l, LL s) {
 10         time = t;
 11         len = l;
 12         last = s;
 13     }
 14 };
 15 
 16 struct Out {
 17     LL time, pos;
 18     Out(LL t, LL p) {
 19         time = t;
 20         pos = p;
 21     }
 22     inline bool operator <(const Out &x) const {
 23         return time > x.time;
 24     }
 25 };
 26 
 27 struct ListNode {
 28     LL pre, nex, l, r;
 29     bool vis;
 30 }li[N]; LL head = N - 1, tail = N - 2, top = 1;
 31 
 32 LL n;
 33 std::queue<Ask> Wait, Come;
 34 std::priority_queue<Out> Erase;
 35 
 36 inline void init() {
 37     li[head].nex = 1;
 38     li[1].pre = head;
 39     li[1].nex = tail;
 40     li[1].l = 0;
 41     li[1].r = n - 1;
 42     li[tail].pre = 1;
 43     return;
 44 }
 45 
 46 inline LL getpos(LL k) {
 47     LL p = li[head].nex;
 48     while(p != tail) {
 49         while(li[p].vis) {
 50             p = li[p].nex;
 51         }
 52         if(p == tail) {
 53             return 0;
 54         }
 55         if(li[p].r - li[p].l + 1 >= k) {
 56             return p;
 57         }
 58         p = li[p].nex;
 59     }
 60     return 0;
 61 }
 62 
 63 inline LL insert(Ask x) {
 64     LL p = getpos(x.len);
 65     if(!p) {
 66         return 0;
 67     }
 68     if(li[p].r - li[p].l + 1 == x.len) {
 69         li[p].vis = 1;
 70         return p;
 71     }
 72     ++top;
 73     li[top].r = li[p].r;
 74     li[top].l = li[p].l + x.len;
 75     li[p].r = li[top].l - 1;
 76     li[p].vis = 1;
 77     li[top].nex = li[p].nex;
 78     li[top].pre = p;
 79     li[p].nex = top;
 80     li[li[top].nex].pre = top;
 81     return p;
 82 }
 83 
 84 inline void erase(Out x) {
 85     LL p = x.pos;
 86     li[p].vis = 0; ///   b p c
 87     LL b = li[p].pre, c = li[p].nex;
 88     if((li[b].vis || b == head) && (li[c].vis || c == tail)) {
 89         return;
 90     }
 91     if(li[b].vis || b == head) {
 92         li[p].nex = li[c].nex;
 93         li[li[c].nex].pre = p;
 94         li[p].r = li[c].r;
 95         return;
 96     }
 97     if(li[c].vis || c == tail) {
 98         li[p].pre = li[b].pre;
 99         li[li[b].pre].nex = p;
100         li[p].l = li[b].l;
101         return;
102     }
103     li[p].nex = li[c].nex;
104     li[p].pre = li[b].pre;
105     li[li[c].nex].pre = p;
106     li[li[b].pre].nex = p;
107     li[p].l = li[b].l;
108     li[p].r = li[c].r;
109     return;
110 }
111 
112 int main() {
113     LL t, len, last;
114     int c = 0, w = 0;
115     scanf("%lld", &n);
116     init();
117     while(1) {
118         scanf("%lld%lld%lld", &t, &len, &last);
119         if(!t && !len && !last) {
120             break;
121         }
122         Come.push(Ask(t, len, last));
123     }
124     LL sum = 0, T = 0;
125     while(!Wait.empty() || !Come.empty() || !Erase.empty()) {
126         LL tc = INF, te = INF;
127         if(!Come.empty()) {
128             tc = Come.front().time;
129         }
130         if(!Erase.empty()) {
131             te = Erase.top().time;
132         }
133         if(te <= tc) {
134             while(Erase.top().time == te) {
135                 erase(Erase.top());
136                 Erase.pop();
137                 if(Erase.empty()) {
138                     break;
139                 }
140             }
141             T = te;
142             LL in = 1;
143             while(!Wait.empty()) {
144                 in = insert(Wait.front());
145                 if(!in) {
146                     break;
147                 }
148                 Erase.push(Out(te + Wait.front().last, in));
149                 Wait.pop();
150             }
151         }
152         else {
153             LL in = insert(Come.front());
154             if(in) {
155                 Erase.push(Out(tc + Come.front().last, in));
156             }
157             else {
158                 Wait.push(Come.front());
159                 sum++;
160             }
161             Come.pop();
162         }
163     }
164 
165     printf("%lld\n%lld", T, sum);
166     return 0;
167 }
AC代码

 

posted @ 2018-08-07 20:08  garage  阅读(247)  评论(0编辑  收藏  举报