力扣第316场
1.力扣
转为整数或直接比较字符串
class Solution {
public:
bool haveConflict(vector<string>& event1, vector<string>& event2) {
if (event1[0] > event2[1] || event2[0] > event1[1]) {
return false;
}
return true;
}
};
2.力扣
枚举区间的左端点,然后枚举区间的右端点,维护右端点的同时维护区间GCD的值,如果等于k则答案+1
class Solution {
public:
int subarrayGCD(vector<int>& nums, int k) {
int res = 0;
int n = nums.size();
for (int i = 0; i < n; i++) {
int j = i + 1;
int cur = nums[i];
if (cur == k) res++;
for (int j = i + 1; j < n; j++) {
cur = gcd(cur, nums[j]);
if (cur == k) res++;
}
}
return res;
}
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
};
Acwing第74场周赛
记 lst 为上一个数,同时特记 x 为第一个数(环形),然后对于每次输入,处理答案并更新 lst,最后输出答案和 |lst−x| 的最小值
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,x,ans=INT_MAX;
cin>>n>>x;
int lst=x;
while(--n)
{
int y;
cin>>y;
ans=min(ans,abs(lst-y));
lst=y;
}
cout<<min(ans,abs(lst-x));
return 0;
}
三维的bfs,
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int k, n, m;
cin >> k >> n >> m;
vector G(k + 1, vector<vector<char>>(n + 1, vector<char>(m + 1)));
for (int a = 1; a <= k; ++a)
for (int b = 1; b <= n; ++b)
for (int c = 1; c <= m; ++c)
cin >> G[a][b][c];
int x, y;
cin >> x >> y;
int ans = 0;
constexpr array<int, 6> dx = {-1, 1, 0, 0, 0, 0};
constexpr array<int, 6> dy = {0, 0, -1, 1, 0, 0};
constexpr array<int, 6> dz = {0, 0, 0, 0, -1, 1};
vector vis(k + 1, vector<vector<bool>>(n + 1, vector<bool>(m + 1, false)));
function<void(int, int, int)> dfs = [&](int x, int y, int z) -> void {
if (x < 1 || x > k || y < 1 || y > n || z < 1 || z > m) return;
if (G[x][y][z] == '#') return;
if (vis[x][y][z]) return;
vis[x][y][z] = true;
++ans;
for (int i = 0; i < 6; ++i) {
int a = x + dx[i], b = y + dy[i], c = z + dz[i];
dfs(a, b, c);
}
};
dfs(1, x, y);
cout << ans << endl;
return 0;
}