csp201709

打酱油

实现

枚举即可

#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
    
    
    cin>>n;
    int t = n/10;
    int res =t;
    // cout<<res<<endl;
    for(int i=0;i<=t/3;i++)
    {
    
    
        res = max(res,t+i+((t-3*i)/5)*2);
        // cout<<res-t<<endl;
    }
    cout<<res;
}

公共钥匙盒

思路

来自acwing-yxc
用一个结构体存储存取钥匙的请求
重载<

实现

#include<bits/stdc++.h>
using namespace std;
int n,k;
const int N = 1010;
int q[N];
struct op
{
    
    
    int tm,type,id;
    bool operator< (const op&t) const
    {
    
    
        if(tm!=t.tm) return tm<t.tm;
        if(type!=t.type) return type>t.type;
        return id < t.id;
    }
}ops[N*2];
int main()
{
    
    
    cin>>n>>k;
    int cnt = 0;
    while(k--)
    {
    
    
        int id,start,len;
        cin>>id>>start>>len;
        ops[cnt++] = {
    
    start,0,id};
        ops[cnt++] = {
    
    start+len,1,id};
    }
    sort(ops,ops+cnt);
    
    for(int i=1;i<=n;i++) q[i] = i;
    for(int i=0;i<cnt;i++)
    {
    
    
        int id = ops[i].id;
        if(!ops[i].type)
        {
    
    
            for(int j=1;j<=n;j++)
            {
    
    
                if(q[j]==id)
                {
    
    
                    q[j] = 0;
                    break;
                }
            }
        }
        else
        {
    
    
            for(int j=1;j<=n;j++)
            {
    
    
                if(!q[j])
                {
    
    
                    q[j] = id;
                    break;
                }
            }
        }
    }
    for(int i=1;i<=n;i++) cout<<q[i]<<' ';
    return 0;
}

JSON

思路

用一个结构体来表示JSON中的基本单位,考虑到基本单位可能是字符串或者对象,结构体如下:

struct Obj
{
    
    
    int type;  // 1: str, 2: dict
    string str;
    map<string, Obj> obj;
};

当type为2时,map表示了键值对,值可以是字符串也可以是对象

实现

来自acwing-yxc

#include<bits/stdc++.h>
using namespace std;
struct Obj
{
    
    
    int type;    //1:str 2:dict
    string str;
    Obj(){
    
    };
    Obj(string _str)
    {
    
    
        type = 1;
        str = _str;
    }
    map<string,Obj> obj;
};
int n,m;
Obj work_obj(string& str,int& k);
string work_str(string& str,int& k)
{
    
    
    k++;
    string res;
    while(str[k]!='\"') 
    {
    
    
        if(str[k]=='\\') res += str[k+1],k+=2;
        else res+=str[k++];
    }
    k++;
    // cout<<res<<endl;
    return res;
}
void work_kv(Obj& obj,string& str,int& k)
{
    
    
    auto key = work_str(str,k);
    while(str[k]!=':') k++;
    k++;
    while(str[k]!='\"' && str[k]!='{') k++;
    if(str[k]=='\"') obj.obj[key] = Obj(work_str(str,k));
    else obj.obj[key] = work_obj(str,k);
}
Obj work_obj(string& str,int& k)
{
    
    
    Obj obj;
    obj.type = 2;
    while(str[k]!='{') k++;
    k++;
    while(str[k]!='}')
    {
    
    
        if(str[k]=='\"') work_kv(obj,str,k);
        else k++;
    }
    k++;
    return obj;
}

void query(Obj o, vector<string>& qs)
{
    
    
    for (auto& s: qs)
    {
    
    
        if (o.type == 1 || !o.obj.count(s))
        {
    
    
            puts("NOTEXIST");
            return;
        }
        auto t = o.obj[s];
        o = t;
    }

    if (o.type == 1) printf("STRING %s\n", o.str.c_str());
    else puts("OBJECT");
}

int main()
{
    
    
    cin>>n>>m;
    getchar();
    string str,s;
    while(n--)
    {
    
    
        getline(cin,s);
        str+=s;
    }
    // cout<<str<<endl;
    int k = 0;
    auto obj = work_obj(str,k);
    while(m--)
    {
    
    
        getline(cin,s);
        vector<string> qs;
        for(int i=0;i<s.size();i++)
        {
    
    
            int j = i+1;
            while(j<s.size() && s[j]!='.') j++;
            qs.push_back(s.substr(i,j-i));
            i = j;
        }
        query(obj,qs);
    }
}

通信网络

思路

  • 建立两个图,分别是正向边和反向边。
  • 枚举每一个点,通过dfs求出st数组
  • 统计可以到达的点个数,为n则ans++

猜你喜欢

转载自blog.csdn.net/Tracy_yi/article/details/129177033