1 条题解
-
0
#include<bits/stdc++.h> using namespace std; const int maxn = 20100; int n,q; int rem; struct Fmem{ int l,r; }fmem[maxn]; int num; struct Used{ int l,r; int id; bool operator <(const Used &beta) const{ return l < beta.l; } }; set <Used> s; int fnum; int bestfit(int p,int len){ int near = 0; for(int i=1;i<=fnum;i++){ int flen = fmem[i].r - fmem[i].l+1; if(flen >= len){ if(near == 0 || (fmem[near].r-fmem[near].l+1) > flen || ((fmem[near].r-fmem[near].l+1) == flen && fmem[near].l > fmem[i].l)){ near = i; } } } if(near == 0) return 0; else { rem -= len; cout<<fmem[near].l<<'\n'; s.insert((Used){fmem[near].l,fmem[near].l+len-1,p}); fmem[near].l = fmem[near].l+len; return 1; } } void compact(int p,int lp){ int now = 1; for(set<Used>::iterator it = s.begin();it != s.end();it++){ int len = (*it).r-(*it).l + 1; Used nw = (Used){now,now+len-1,(*it).id}; s.erase(it);s.insert(nw); it = s.find(nw); now += len; } rem -= lp; fnum = 0; cout<<now<<'\n'; s.insert((Used){now,now+lp-1,p}); now += lp; fmem[++fnum] = (Fmem){now,n}; } int cmp(Fmem alpha,Fmem beta){ return alpha.l < beta.l; } void release(int p){ for(set<Used>::iterator it = s.begin();it != s.end();it++){ if((*it).id == p){ rem += (*it).r - (*it).l+1; fmem[++fnum] = (Fmem){(*it).l,(*it).r}; cout<<(*it).l<<'\n'; s.erase(it); break; } } sort(fmem+1,fmem+fnum+1,cmp); int fp = 0; for(int i=1;i<=fnum;i++){ if(fmem[i].l > fmem[i].r) continue; if(fmem[i].l == fmem[fp].r+1 && fp != 0){ fmem[fp].r = fmem[i].r; }else{ fp++; fmem[fp] = fmem[i]; } } fnum = fp; } int main(){ cin >> n >> q; rem = n; fmem[++fnum] = (Fmem){1,n}; for(int i=1;i<=q;i++){ int x,y; cin >> x >> y; if(x == 1){ num++; if(bestfit(num,y))continue; if(rem < y){ cout<<"Fail\n"; }else compact(num,y); }else{ release(y); } } }
- 1
信息
- ID
- 277
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者