天梯赛练习题L2(031-035)

L2-031 深入虎穴

这个题目我疑惑了好久,因为它说只有一个入口,但是却没有告诉我们哪个是入口,并且需要我们找出距离入口最远的那扇门
所以我们在做题时要一一判断哪个是入口,然后再进行寻找

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=5e5+10,M=4002;
#define endl '\n'
LL n,maxn=0,ans=0,last[N];
vector<LL> v[N];
void dfs(LL idx,LL fa,LL dep)
{
    if(dep>maxn)
    {
        maxn=max(maxn,dep);
        ans=idx;
    }
    for(int i=0;i<v[idx].size();i++)
    {
        if(v[idx][i]==fa) continue;
        else dfs(v[idx][i],idx,dep+1);
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        memset(last,-1,sizeof last);
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            LL op;
            cin>>op;
            for(int j=1;j<=op;j++)
            {
                LL x;
                cin>>x;
                v[i].push_back(x);
                last[x]=i;
            }
       }
       for(int i=1;i<=n;i++)
       {
           if(last[i]==-1)
           {
               dfs(i,-1,1);
           }
       }
       cout<<ans<<endl;
    }
    return 0;
}

L2-032 彩虹瓶

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=5e5+10,M=4002;
#define endl '\n'
LL n,m,k,a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m>>k;
        while(k--)
        {
            for(int i=1;i<=n;i++)
                cin>>a[i];
            stack<int> s;
            LL idx=1;
            bool flag=true;
            for(int i=1;i<=n;i++)
            {
                if(a[i]==idx)
                {
                    idx++;
                    while(!s.empty())
                    {
                        if(s.top()==idx)
                        {
                            s.pop();
                            idx++;
                        }
                        else break;
                    }
                }
                else
                {
                    s.push(a[i]);
                    if(s.size()>m)
                    {
                        flag=false;
                        break;
                    }
                }
            }
            //cout<<idx<<endl;
            if(flag==false||idx!=n+1) cout<<"NO"<<endl;
            else cout<<"YES"<<endl;
        }
    }
    return 0;
}

L2-033 简单计算器

仔细看懂题目解释的运算顺序即可

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1000200,M=4002;
const double PI=3.1415926535;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        stack<LL> s;
        for(int i=1;i<=n;i++)
        {
            LL x;
            cin>>x;
            s.push(x);
        }
        stack<char> st;
        for(int i=1;i<=n-1;i++)
        {
            char c;
            cin>>c;
            st.push(c);
        }
        while(s.size())
        {
            LL a=s.top(); s.pop();
            LL b=s.top(); s.pop();
            char op=st.top(); st.pop();
            if(op=='+') s.push(b+a);
            else if(op=='-') s.push(b-a);
            else if(op=='*') s.push(b*a);
            else if(op=='/')
            {
                if(a==0)
                {
                    cout<<"ERROR: "<<b<<"/"<<a;
                    break;
                }
                else s.push(b/a);
            }
            if(s.size()==1)
            {
                cout<<s.top();
                break;
            }
        }
    }
    return 0;
}

L2-034 口罩发放

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1e4+10,M=4002;
#define endl '\n'
LL d,p,t,s,anscnt;
map<string,LL> mp,vis;
struct node
{
	string name,id;
	LL flag;
	LL hh,mm,t;
	LL idx;
}a[N],ans[N];
bool cmp(node x,node y)
{
	if(x.t!=y.t) return x.t<y.t;
	return x.idx<y.idx;
}
bool check(string s)
{
	LL len=s.length();
	if(len!=18) return false;
	for(int i=0;i<len;i++)
    {
		if(!isdigit(s[i])) return false;
	}
	return true;
}
int main()
{
    //cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>d>>p;
	    for(int i=1;i<=d;i++)//d天的数据
        {
            cin>>t>>s;//第i天:t条申请,s个发放名额
		    for(int j=1;j<=t;j++)
		    {
		        //姓名、身体状况、身体情况、提交时间
		        cin>>a[j].name>>a[j].id>>a[j].flag;
			    scanf("%d:%d",&a[j].hh,&a[j].mm);
			    a[j].t=a[j].hh*60+a[j].mm;
			    a[j].idx=j;//标记排名
			    if(mp.find(a[j].id)==mp.end()) mp[a[j].id]=0;
			    if(a[j].flag==1&&check(a[j].id)&&vis.find(a[j].id)==vis.end())
                {
                    vis[a[j].id]=0;
				    ans[anscnt++]=a[j];
			    }
            }
		    sort(a+1,a+t+1,cmp);
		    LL cnt=0;
		    for(int j=1;j<=t&&cnt<s;j++)
            {
                if(check(a[j].id)&&(!mp[a[j].id]||(i-mp[a[j].id])>p))
                {
                    cout<<a[j].name<<" "<<a[j].id<<endl;
				    cnt++;
				    mp[a[j].id]=i;
                }
            }
	    }
	    for(int i=0;i<anscnt;i++)
            cout<<ans[i].name<<" "<<ans[i].id<<endl;
    }
    return 0;
}

L2-035 完全二叉树的层序遍历

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=5e5+10,M=4002;
#define endl '\n'
LL n,a[N];
void check(int i)
{
     if(i>n) return;
     check(2*i);
     check(2*i+1);
     cin>>a[i];
 }
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n;
        check(1);
        for(int i=1;i<=n;i++)
        {
            if(i!=n) cout<<a[i]<<" ";
            else cout<<a[i];
        }
    }
    return 0;
}
posted @ 2023-01-22 19:29  高尔赛凡尔娟  阅读(21)  评论(0编辑  收藏  举报