1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=(a);i<(int)(n);i++) #define per(i,a,n) for(int i=(n);i-->(int)(a);) int read(){int r;scanf("%d",&r);return r;} const int N=200000; template<class T> using maxPQ = priority_queue<T,vector<T>>; int a[N+10]; int s[N+10]; int d[N+10]; void w(){ int n=read(); int m=read(); rep(i,0,n) a[i]=read(); rep(i,0,m) s[i]=read(); rep(i,0,m) d[i]=read(); vector<vector<int> > ans(m); rep(i,0,m) ans[i].resize(s[i],0); maxPQ< array<int,4> > pq; int maxl = 0; rep(i,0,m) { int S = s[i]; int D = d[i]; maxl = max(maxl, D); rep(t,0,S/D) pq.push({D, i, t, 0}); if(S%D != 0) pq.push({S%D, i, S/D, 0}); } sort(a,a+n); vector<pair<int,int> > cc; rep(i,0,n) { if(i == 0 or cc.back().second != a[i]) { cc.push_back({1, a[i]}); }else{ cc.back().first++; } } sort(begin(cc),end(cc)); per(i,0,size(cc)){ auto [cnt,color] = cc[i]; if((int)pq.size() < cnt) { printf("-1\n"); return ; } vector<array<int,4> > add; rep(j,0,cnt) { auto [len,shelf_id,t,off] = pq.top(); pq.pop(); ans[shelf_id][t * d[shelf_id] + off] = color; if(len > 1) add.push_back({len-1,shelf_id,t,off+1}); } for(auto o:add) pq.push(o); } rep(i,0,m) { map<int,int> v2pos; per(t,0,s[i]/d[i] + !!(s[i]%d[i])) { vector<int> vals; rep(j,0,d[i]) { int idx = t*d[i]+j; if(idx >= s[i]) break; vals.push_back(ans[i][idx]); ans[i][idx] = -1; } per(j,0,size(vals)){ int v = vals[j]; if(v2pos.count(v) and v2pos[v]/d[i] == t+1){ ans[i][v2pos[v] - d[i]] = v; swap(vals[j],vals.back()); vals.pop_back(); } } rep(j,0,d[i]) { int idx = t*d[i]+j; if(idx >= s[i]) break; if(ans[i][idx] == -1) { ans[i][idx] = *vals.begin(); vals.erase(vals.begin()); } v2pos[ans[i][idx]] = idx; } } for(auto v:ans[i]) printf("%d ",v); printf("\n"); } }
int main() { int t=read(); while(t--) w(); return 0; }
|