每日两题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 天前
算法竞赛C++常用的STL
c++·算法
weilx12341 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛2 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光2 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark2 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·2 天前
C++ 萌新语法入门篇
c++·算法比赛
Because_of_Her12 天前
并查集-听课笔记
笔记·算法·并查集