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

1、报数

ACwing 3282

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

int ans[4];

int main() {
    int n; cin >> n;
    int k = 0, i = -1;
    while (n) {
        i++, k++;
        if (k % 7 == 0 || to_string(k).find('7') != -1) ans[i % 4]++;
        else n--;
    }
    for (int i = 0; i < 4; i++) cout << ans[i] << endl;
    return 0;
}

2、回收站选址

ACwing 3283

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

#define x first
#define y second

typedef pair<int, int> PII;

int n;
int ans[5];
set<PII> st;
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
int ix[] = {-1, -1, 1, 1}, iy[] = {-1, 1, 1, -1};

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int x, y; cin >> x >> y;
        st.insert({x, y});
    }
    for (auto it: st) {
        int cnt = 0;
        for (int j = 0; j < 4; j++) {
            int a = it.x + dx[j], b = it.y + dy[j];
            if (st.count({a, b})) cnt++;
        }
        if (cnt != 4) continue;
        int sum = 0;
        for (int j = 0; j < 4; j++) {
            int a = it.x + ix[j], b = it.y + iy[j];
            if (st.count({a, b})) sum++;
        }
        ans[sum]++;
    }
    for (int an : ans) cout << an << endl;
    return 0;
}

3、化学方程式

ACwing 3284

4、区块链

ACwing 3285

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

typedef vector<int> VI;
const int N = 510, M = 20010;

int n, m, w, Q; // w表示传输时延
int h[N], e[M], ne[M], idx;
vector<VI> g; // 链表
int node[N]; // 每个点对应链表的编号
struct Op { // 操作
    int t, id, pid, hid; // 时间、链表id、从哪个结点传过来、过来链表的编号
    bool operator<(const Op &r) const {
        return t > r.t; // 保证堆是小根堆(按时序从小到大模拟)
    }
};
priority_queue<Op> heap;

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

void eval() {
    auto t = heap.top(); heap.pop();
    auto &a = g[node[t.id]], &b = g[t.hid];
    if (b.size() > a.size() || b.size() == a.size() && b.back() < a.back()) {
        node[t.id] = t.hid;
        for (int i = h[t.id]; ~i; i = ne[i]) // 更新当前邻点
            if (e[i] != t.pid && e[i] != t.id) // 防止自环
                heap.push({t.t + w, e[i], t.id, t.hid});
    }
}

int main() {
    scanf("%d%d", &n, &m);
    g.push_back({0}); // 最初所有点都指向链表0(创世块)
    memset(h, -1, sizeof h);
    while (m--) {
        int a, b; scanf("%d%d", &a, &b);
        add(a, b), add(b, a);
    }
    scanf("%d%d", &w, &Q); getchar();
    char str[100];
    while (Q--) {
        fgets(str, 100, stdin); stringstream ssin(str);
        int a[3], cnt = 0;
        while (ssin >> a[cnt]) cnt++;
        if (cnt == 3) { // 添加块
            while (heap.size() && heap.top().t <= a[1]) eval(); // 将之前时刻的所有操作更新
            g.push_back(g[node[a[0]]]); // 因为需要修改,所以先复制
            g.back().push_back(a[2]);
            node[a[0]] = g.size() - 1;
            for (int i = h[a[0]]; ~i; i = ne[i]) // 更新所有邻点
                if (e[i] != a[0]) // 防止自环
                    heap.push({a[1] + w, e[i], a[0], node[a[0]]});
        } else {
            while (heap.size() && heap.top().t <= a[1]) eval();
            printf("%d ", g[node[a[0]]].size());
            for (auto x: g[node[a[0]]]) printf("%d ", x);
            puts("");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值