今天你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;
}