QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#238681 | #7678. The Game | ucup-team073# | TL | 1158ms | 11964kb | C++14 | 2.8kb | 2023-11-04 17:15:12 | 2023-11-04 17:15:13 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define debug puts("###");
using namespace std;
const int N=3e5+10;
int a[N],b[N],A[N],B[N],n,m;
inline void solve() {
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (int i=1;i<=n;i++) a[i]=A[i];
for (int i=n-m+1,j=1;i<=n;i++,j++) b[i]=B[j];
sort(a+1,a+n+1);
sort(b+n-m+1,b+n+1);
for (int i=n-m+1;i<=n;i++) if (a[i]>b[i]) {puts("-1");return ;}
int suma=0,sumb=0;
for (int i=n-m+1;i<=n;i++) suma+=a[i],sumb+=b[i];
int step=sumb-suma,minb=b[n-m+1];
if (step>n-m) {puts("-1");return ;}
priority_queue<int,vector<int>,greater<int> > q,q2;
for (int i=1;i<=n-m;i++) q.push(a[i]);
vector<int> v;
for (int i=n-m+1;i<=n;i++) if (a[i]!=b[i]) v.push_back(i);
int len=v.size();
for (int i=n-m+1,j=0;j<len;i++,j++) b[i]=b[v[j]],q2.push(a[v[j]]);
vector<int> ans;
for (int i=n-m,cnt=n-m+1;i>=1;i--) {
int x=q.top(),y=-1; q.pop();
if (!q2.empty()) y=q2.top();
if (x>=minb) {puts("-1");return ;}
if (x==y) {
q2.pop(); --step; ans.push_back(y);
if (y+1==b[cnt]) ++cnt;
else q2.push(y+1);
}
else {
q.push(x+1); ans.push_back(x); q.pop();
}
if (i-1==step) {
while (!q2.empty()) {
int y=q2.top(); q2.pop();
ans.push_back(y); if(y+1==b[cnt]) ++cnt;
else q2.push(y+1);
}
break;
}
}
len=ans.size();
printf("%lld\n",len);
for (int i=0;i<len;i++) printf("%lld ",ans[i]);if (len!=0) puts("");
}
void solve2(){
vector<int>ans;
for(int i=1;i<=n;++i) a[i]=A[i];
for(int i=1;i<=m;++i) b[i]=B[i];
sort(a+1,a+n+1),sort(b+1,b+m+1);
int suma=0,sumb=0,minn=b[1],cur=1;
for(int i=1;i<=m;++i)if(a[i+n-m]>b[i]){solve();return;}
for(int i=1;i<=m;++i)sumb+=b[i],suma+=a[n-i+1];
for(int i=1;i<=m;++i){
if(i==1||b[i]==b[i-1])while(a[n-m+i]<b[i])ans.push_back(a[n-m+i]),++a[n-m+i],++cur,++suma;
else break;
}
for(int i=1;i<=m;++i)if(a[n-m+i]<b[1])ans.push_back(a[n-m+i]),++a[n-m+i],++cur,++suma;
/*
int sum=0,flag=0;
for(int i=1;i<=n-m;++i)sum+=(a[n-m+1]-a[i]);
if(sumb-suma>sum&&b[1]!=a[n-m+1]){ans.push_back(a[n-m+1]);a[n-m+1]++;++suma;minn++;flag=1;}
else if(sumb-suma>sum&&b[1]==a[n-m+1]){cout<<"-1\n";return;}
*/
if(suma>sumb){solve();return;}
if(sumb-suma>n-m-cur+1){solve();return;}
priority_queue<int,vector<int>,greater<int> >q;
for(int i=cur;i<=n-m;++i)q.push(a[i]);
for(int i=1;i<=(n-m)-(sumb-suma)-cur+1;++i){
if(q.empty()){solve();return;}
int t=q.top();q.pop();
if(t>=minn){solve();return;}
ans.push_back(t);
q.push(t+1);q.pop();
}
for(int i=1;i<=m;++i)while(a[i+n-m]<b[i])ans.push_back(a[i+n-m]),++a[i+n-m];
cout<<ans.size()<<endl;
for(int i:ans)cout<<i<<' ';cout<<endl;
}
signed main(void) {
int t; cin>>t;
while (t--) {
cin>>n>>m;
for (int i=1;i<=n;i++) cin>>A[i];
for (int i=1;i<=m;i++) cin>>B[i];
solve2();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11168kb
input:
6 5 3 1 2 2 3 3 2 3 4 4 2 1 2 2 4 2 4 5 2 2 3 3 4 4 5 5 6 1 1 1 1 1 1 1 4 4 2 1 1 1 2 2 2 4 1 1 1 1 1 2
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 1 1 1 1 3 7 4 2 1 1...
output:
-1 -1 2 1 2 -1 -1 -1 -1 2 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 2 3 -1 -1 -1 2 1 1 2 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 3 4 -1 -1 -1 2 1 1 2 1 3 -1 -1 -1 2 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok ok (5880 test cases)
Test #4:
score: 0
Accepted
time: 348ms
memory: 11964kb
input:
2640 4 1 1 1 1 1 1 4 1 1 1 1 1 2 4 1 1 1 1 1 3 4 1 1 1 1 1 4 4 1 1 1 1 1 5 4 1 1 1 1 1 6 4 1 1 1 1 1 7 4 1 1 1 1 1 8 4 1 1 1 1 2 1 4 1 1 1 1 2 2 4 1 1 1 1 2 3 4 1 1 1 1 2 4 4 1 1 1 1 2 5 4 1 1 1 1 2 6 4 1 1 1 1 2 7 4 1 1 1 1 2 8 4 1 1 1 1 3 1 4 1 1 1 1 3 2 4 1 1 1 1 3 3 4 1 1 1 1 3 4 4 1 1 1 1 3 5 4...
output:
-1 -1 3 1 2 1 3 1 2 3 -1 -1 -1 -1 -1 -1 3 2 1 2 3 2 3 1 3 2 3 4 -1 -1 -1 -1 -1 3 1 1 2 3 3 1 2 3 3 4 1 3 3 4 5 -1 -1 -1 -1 -1 3 1 1 2 3 4 1 2 3 4 5 1 3 4 5 6 -1 -1 -1 -1 -1 3 1 1 2 3 5 1 2 3 5 6 1 3 5 6 7 -1 -1 -1 -1 -1 3 1 1 2 3 6 1 2 3 6 7 1 -1 -1 -1 -1 -1 -1 3 1 1 2 3 7 1 2 ...
result:
ok ok (2640 test cases)
Test #5:
score: -100
Time Limit Exceeded
input:
14112 5 3 1 1 1 1 1 1 1 1 5 3 1 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