QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620905#7678. The Gamety09WA 1ms3556kbC++172.1kb2024-10-07 22:14:012024-10-07 22:14:01

Judging History

This is the latest submission verdict.

  • [2024-10-07 22:14:01]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3556kb
  • [2024-10-07 22:14:01]
  • Submitted

answer

#include"bits/stdc++.h"
#define endl '\n'
using ll=long long;
using namespace std;
constexpr int N=1e5+10,inf=0X3F3F3F3F;
constexpr ll INF=0X3F3F3F3F3F3F3F3F;
#define int ll
void solve(){
    int n,m;
    cin>>n>>m;
    ll sb=0,sa1=0,sa2=0;
    vector<int>a(n+1),b(m+1);
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=m;i++){
        cin>>b[i];
        sb+=b[i];
    }

    sort(a.begin()+1,a.end());
    sort(b.begin()+1,b.end());

    for(int i=1;i<=m;i++){
        sa2+=a[n-m+i];
        if(a[n-m+i]>b[i]){
            cout<<-1<<endl;
            return;
        }
    }
    if(sb-sa2>n-m){
        cout<<-1<<endl;
        return;
    }

    multiset<int>a1,a2;
    for(int i=1;i<=n-m;i++){
        a1.insert(a[i]);
        sa1+=a[i];
    }
    for(int i=1;i<=m;i++)
        a2.insert(a[i+n-m]);

    vector<int>ans;
    while(sb-sa2<n-m){
        if(a1.size()){
            int x=*a1.begin();
            ans.push_back(x);

            a1.erase(a1.begin());
            a1.insert(x+1);
            while((*a1.rbegin())>(*a2.begin())){
                int x=*a1.rbegin(),y=*a2.begin();
                a1.extract(x);
                a2.erase(a2.begin());
                a1.insert(y);
                a2.insert(x);
                sa2=sa2-y+x;
            }
            a1.erase(a1.begin());

        }else{
            int x=*a2.begin();
            ans.push_back(x);
            a2.erase(a2.begin());
            sa2-=x;
            a2.insert(x+1);
            sa2+=x+1;
            int y=*a2.begin();
            a2.erase(a2.begin());
            sa2-=y;
        }
        n--;
        if(sb-sa2>n-m||a2.empty()){
            cout<<-1<<endl;
            return;
        }
    }

    int i=1;
    for(auto x:a2){
        while(x<b[i]){
            ans.push_back(x);
            x++;
        }
        i++;
    }

    cout<<ans.size()<<endl;
    for(auto x:ans)
        cout<<x<<' ';
    cout<<endl;
}
signed main(){
    cin.tie(nullptr)->sync_with_stdio(0);
	int t;cin>>t;while(t--)
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3556kb

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

output:

2
1 3 
-1
3
2 4 4 
5
1 1 1 2 3 
2
1 1 
-1

result:

ok ok (6 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3540kb

input:

7056
4 3
1 1 1 1
1 1 1
4 3
1 1 1 1
1 1 2
4 3
1 1 1 1
1 1 3
4 3
1 1 1 1
1 1 4
4 3
1 1 1 1
1 1 5
4 3
1 1 1 1
1 1 6
4 3
1 1 1 1
1 2 2
4 3
1 1 1 1
1 2 3
4 3
1 1 1 1
1 2 4
4 3
1 1 1 1
1 2 5
4 3
1 1 1 1
1 2 6
4 3
1 1 1 1
1 3 3
4 3
1 1 1 1
1 3 4
4 3
1 1 1 1
1 3 5
4 3
1 1 1 1
1 3 6
4 3
1 1 1 1
1 4 4
4 3
1 1...

output:

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
-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
-1
-1
-1
-1
-1
-1
-1
-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
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1...

result:

wrong answer Wrong answer, number of operation is not correct (test case 1)