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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define maxn 505 #define put() putchar('\n') #define Tp template<typename T> #define Ts template<typename T,typename... Ar> using namespace std; Tp void read(T &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,m,k; struct yyy { int id,x; bool operator <(const yyy tmp) const { return x<tmp.x; } }a[maxn],b[maxn],c[maxn],d[maxn]; int tot1=0,tot2=0; bool cmp(yyy x,yyy y) {return x.x<y.x;} bitset<5000005> f[505]; const int base=2.5e6; void insert(int n) { int i,tot=0,fl=0; for (i=1;i<n;i++) { if (!fl&&a[n].x>a[i-1].x&&a[n].x<=a[i].x) b[++tot]=a[n],fl=1; b[++tot]=a[i]; } if (!fl) b[++tot]=a[n]; for (i=1;i<=n;i++) a[i]=b[i]; } void calc(void) { int i; while (m&&n) { printf("%d %d %d %d\n",a[1].id,a[1].x,a[n].id,k-a[1].x); a[n].x-=(k-a[1].x); for (i=1;i<n;i++) a[i]=a[i+1]; n--; if (a[n].x) insert(n); else n--; m--; } } void solve(void) { int i,j,tot1,tot2; read(n);read(m);read(k); for (i=1;i<=n;i++) read(a[i].x),a[i].id=i; sort(a+1,a+1+n,cmp); while (m>=n&&n) { printf("%d %d\n",a[n].id,k); a[n].x-=k; if (a[n].x) insert(n); else n--; m--; } if (!n) return ; if (m==n-1) return calc(),void();
f[0].reset(); f[0][base]=1; for (i=1;i<=n;i++) a[i].x-=k; for (i=1;i<=n;i++) { f[i]=f[i-1]; if (a[i].x>=0) f[i]|=f[i-1]<<a[i].x; else f[i]|=f[i-1]>>(-a[i].x); } if (f[n][base-k]==0) return puts("-1"),void(); i=n,j=base-k,tot1=0,tot2=0; while (i) { if (f[i-1][j-a[i].x]) c[++tot1]=a[i],j-=a[i].x,i--; else d[++tot2]=a[i],i--; } n=tot1;m=tot1-1; reverse(c+1,c+1+tot1); for (i=1;i<=n;i++) a[i]=c[i],a[i].x+=k; calc(); n=tot2;m=tot2-1; reverse(d+1,d+1+tot2); for (i=1;i<=n;i++) a[i]=d[i],a[i].x+=k; calc(); } signed main(void){ int T; read(T); while (T--) solve(); return 0; }
|