A题 Math Problem

#include <bits/stdc++.h>
using namespace std;
int t,n,l,r,mi,mx;
int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n;
        mx=0;mi=0x3f3f3f3f;
        for(int i=1;i<=n;i++)
        {
            cin>>l>>r;
            if(l>mx)mx=l;
            if(r<mi)mi=r;
        }
        if(mi>=mx)printf("0\n");
        else printf("%d\n",mx-mi);
    }
    return 0;
}

B题 Box

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
bool vis[N];
int t,n,a[N],ans[N];
vector<int>g;
int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(ans,0,sizeof(ans));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            ans[i]=a[i];
            vis[a[i]]=1;
        }
        g.clear();
        for(int i=1;i<=n;i++)
            if(!vis[i])g.push_back(i);
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==a[i-1])
            {
                if(!g.empty()&&g[0]<=a[i])
                {
                    ans[i]=g[0];
                    g.erase(g.begin());
                }
                else {flag=1;break;}
            }
        }
        if(flag==1)printf("-1\n");
        else
        {
            for(int i=1;i<=n;i++)
                i==n?printf("%d\n",ans[i]):printf("%d ",ans[i]);
        }
    }
    return 0;
}

D1题 Optimal Subsequences (Easy Version)

#include <bits/stdc++.h>
using namespace std;
const int N=110;
bool vis[N];
int n,m,x,k,newpos,ans[N][N];
struct node
{
    int x,pos;
}p[N];
vector<node>g;
bool cmp1(node s1,node s2)
{
    if(s1.x!=s2.x)return s1.x>s2.x;
    else return s1.pos<s2.pos;
}
bool cmp2(node s1,node s2)
{return s1.pos<s2.pos;}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        p[i].x=x;
        p[i].pos=i;
    }
    sort(p+1,p+n+1,cmp1);
    cin>>m;
    while(m--)
    {
        cin>>k>>newpos;
        if(!vis[k])
        {
            vis[k]=1;
            g.clear();
            for(int i=1;i<=k;i++)
                g.push_back(p[i]);
            sort(g.begin(),g.end(),cmp2);
            for(int i=1;i<=k;i++)
                ans[k][i]=g[i-1].x;
        }
        printf("%d\n",ans[k][newpos]);
    }
    return 0;
}