每日两题day67

今天你AC了吗?

每日两题day67


一、基础题

题目:P1567 统计天数 - 洛谷

思路:

代码:

cpp 复制代码
#include <bits/stdc++.h>

int main() {
    int n, days = 1, r = 1;
    std::cin >> n;
    std::vector<int> a(n);
    std::cin >> a[0];
    for (int i = 1; i < n; i++) {
        std::cin >> a[i];
        if (a[i] > a[i - 1]) {
            days++;
            r = std::max(r, days);
        } else {
            days = 1;
        }
    }
    std::cout << r;
    return 0;
}

二、提高题

题目:P1135 奇怪的电梯 - 洛谷

思路:

bfs,从A按层序搜到B,注意特判A==B。

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, a, b;
    cin >> n >> a >> b;
    if (a == b) {
        cout << 0 << "\n";
        return 0;
    }
    vector<int> t(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> t[i];
    }
    queue<int> q;
    vector<int> u(n + 1);
    u[a] = 1;
    q.push(a);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        int l = x - t[x], r = x + t[x];
        if (l > 0 && u[l] == 0) {
            if (b == l) {
                cout << u[x] << "\n";
                return 0;
            }
            q.push(l);
            u[l] = u[x] + 1;
        }
        if (r <= n && u[r] == 0) {
            if (b == r) {
                cout << u[x] << "\n";
                return 0;
            }
            q.push(r);
            u[r] = u[x] + 1;
        }
    }
    cout << "-1\n";
    return 0;
}
相关推荐
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
见过夏天1 天前
C++ 基础入门完全指南
c++
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考2 天前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队2 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法