QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#600211#5002. Distance and Tree1025497292WA 60ms24432kbC++172.5kb2024-09-29 15:21:322024-09-29 15:21:32

Judging History

你现在查看的是最新测评结果

  • [2024-09-29 15:21:32]
  • 评测
  • 测评结果:WA
  • 用时:60ms
  • 内存:24432kb
  • [2024-09-29 15:21:32]
  • 提交

answer

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define Mirai ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
typedef pair<int,int> pii;
const int N=1e5+10;
void solve()
{
    int n;cin>>n;
    set<int> st;
    vector<pii> res;
    vector<vector<int>> dep(n+1);
    vector<int> a(n+1);
    for(int i=1;i<=n;i++)
    {
        int x;cin>>x;
        a[i]=x;
        if(x>n-1)
        {
            cout<<-1<<endl;
            return ;
        }
        dep[x].push_back(i);
    }
    if(dep[0].size()!=1)
    {
        cout<<-1<<endl;
        return ;
    }
    for(int i=0;i<3;i++)//root
    {
        // now.push_back(dep[0].back()+i*n);
        st.insert(dep[0].back()+i*n);
    }
    for(auto u:dep[1])//1
    {
        res.push_back({dep[0].back(),u});
        for(int i=0;i<3;i++)
        {
            // now.push_back(u+i*n);
            st.insert(u+i*n);
        }
    }
    // for(int i=0;i<=n-1;i++)
    // {
    //     cout<<i<<":";
    //     for(auto it:dep[i])cout<<it<<" ";
        // cout<<endl;
    // }
    // cout<<endl;

    // for(auto it:st)cout<<it<<" ";
    // cout<<endl;

    for(int depth=2;depth<=n-1;depth++)
    {
        for(auto u:dep[depth])
        {
            u+=n;
            auto r=st.upper_bound(u);
            
            // int r=upper_bound(now.begin(),now.end(),u)-now.begin();
            // int l=r-1;
            // cout<<u%n<<" "<<now[l]%n<<" "<<now[r]%n<<endl;
            if(a[(*r)%n]==depth-1)
            {
                res.push_back({(*r)%n,u-n});
            }
            else
            {
                r--;
                if(a[(*r)%n]==depth-1)
                {
                    res.push_back({(*r)%n,u-n});
                }
                else 
                {
                    cout<<-1<<endl;
                    return ;
                }
            } 
            
            // if(a[now[*(r--)]%n]!=depth-1)
            // {
            //     res.push_back({now[r]%n,u-n});
            // }
            // else 
            // {
            //     cout<<-1<<endl;
            //     return ;
            // }
        }
        for(auto u:dep[depth])
        {
            for(int j=0;j<3;j++)
            {
                st.insert(u+j*n);
            }
        }
    }
    // cout<<endl;
    for(auto it:res)cout<<it.first<<" "<<it.second<<endl;
}
signed main()
{
    Mirai;
    int T=1;
    // cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3560kb

input:

5
0 1 2 1 3

output:

-1

result:

ok Accepted

Test #2:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

5
1 1 0 1 1

output:

3 1
3 2
3 4
3 5

result:

ok Accepted

Test #3:

score: -100
Wrong Answer
time: 60ms
memory: 24432kb

input:

100000
96770 96764 96762 96761 96759 96755 96754 96753 96752 96751 96750 96748 96746 96745 96741 96740 96739 96736 96735 96734 96730 96728 96727 96726 96723 96718 96714 96712 96710 96709 96706 96705 96704 96703 96702 96698 96697 96696 96695 96693 96692 96690 96687 96684 96683 96682 96681 96679 96677...

output:

-1

result:

wrong answer There should be a tree