第十一次CCF计算机软件能力认证

1、打酱油

ACwing 3247

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n; cin >> n;
    int res = 0;
    if (n >= 50) res += (n / 50) * 7, n %= 50;
    if (n >= 30) res += (n / 30) * 4, n %= 30;
    if (n >= 10) res += (n / 10), n %= 10;
    cout << res << endl;
    return 0;
}

2、公共钥匙盒

ACwing 3248

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, m, q[N];

struct Op {
    int tm, type, id; // tm表示起始时间,type表示起始时间开始钥匙是否存在,id指编号
    bool operator<(const Op &w) const {
        if (tm != w.tm) return tm < w.tm;
        if (type != w.type) return type > w.type; // 还钥匙在取钥匙之前
        return id < w.id; // id较小的先还
    }
} op[N * 2];

int main() {
    cin >> n >> m;
    int k = 0;
    while (m--) {
        int id, start, len; cin >> id >> start >> len;
        op[k++] = {start, 0, id};
        op[k++] = {start + len, 1, id};
    }
    sort(op, op + k);
    for (int i = 1; i <= n; i++) q[i] = i;
    for (int i = 0; i < k; i++) { // 枚举每一次操作
        int id = op[i].id;
        if (!op[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;
}

3、JSON查询

ACwing 3249

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
using namespace std;

struct Obj {
    int type; // 1 str, 2 dict
    string str;
    map<string, Obj> obj;
    Obj() {}
    Obj(string _str) {
        type = 1;
        str = _str;
    }

};

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++; // 过滤掉 '\"'
    return res;
}

Obj work_obj(string &, int &);

void work_kv(Obj &obj, string &str, int &k) { // 解析 key : value 对
    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)); // value 是一个字符串
    else obj.obj[key] = work_obj(str, k); // value 是一个对象
}

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); // 找到一个 key : value 对
        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() {
    int n, m; cin >> n >> m; getchar();
    string str, s;
    while (n--) {
        getline(cin, s); str += s;
    }
    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);
    }
    return 0;
}

4、通信网络

ACwing 3250

开两个布尔数组 s t 1 、 s t 2 st1、st2 st1st2,分别表示能够从当前点能够走到的所有点以及能走到当前点的点(从该点开始沿着反向边遍历即可)。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1010, M = 20010;

int n, m;
int h1[N], h2[N], e[M], ne[M], idx;
bool st1[N], st2[N];

void add(int h[], int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int h[], bool st[]) {
    st[u] = true;
    for (int i = h[u]; i != -1; i = ne[i]) {
        int j = e[i];
        if (!st[j]) dfs(j, h, st);
    }
}

int main() {
    scanf("%d%d", &n, &m);
    memset(h1, -1, sizeof h1); memset(h2, -1, sizeof h2);
    while (m--) {
        int a, b; scanf("%d%d", &a, &b);
        add(h1, a, b), add(h2, b, a);
    }
    int res = 0;
    for (int i = 1; i <= n; i++) {
        memset(st1, 0, sizeof st1); memset(st2, 0, sizeof st2);
        dfs(i, h1, st1); dfs(i, h2, st2);
        int s = 0;
        for (int j = 1; j <= n; j++)
            if (st1[j] || st2[j]) s++;
        if (s == n) res++;
    }
    printf("%d\n", res);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值