每日两题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;
}
相关推荐
To_OC2 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
炸膛坦客3 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
旖-旎3 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC3 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
Henry Zhu1233 小时前
C++中的特殊成员函数与智能指针
c++
delishcomcn4 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹4 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术5 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
_wyt0016 小时前
多重背包问题详解
c++·背包dp