线段树下标的考察
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
cin >> a >> b;
if (b / 2 == a) {
cout << "Yes\n";
} else {
cout << "No\n";
}
return 0;
}
模拟,暴力
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
s = '#' + s;
for (int i = 1; i < n; i++) {
int ans = 0;
for (int j = 1; j <= n; j++) {
if (i + j <= n && s[j] != s[i + j]) {
ans++;
} else {
break;
}
}
cout << ans << '\n';
}
return 0;
}
26进制
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int len = s.size();
LL ans = 0;
for (int i = 0; i < len; i++) {
ans = ans * 26 + (s[i] - 'A' + 1);
}
cout << ans << '\n';
return 0;
}
DFS,判断是否有环
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
map<string, int> mp;
int cnt = 1;
vector<pair<string, string>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
if (mp[a[i].first] == 0) {
mp[a[i].first] = cnt++;
}
if (mp[a[i].second] == 0) {
mp[a[i].second] = cnt++;
}
}
vector<vector<int>> G(cnt + 1);
for (int i = 0; i < n; i++) {
G[mp[a[i].first]].push_back(mp[a[i].second]);
}
bool ok = true;
vector<bool> vis(cnt + 1);
function<void(int, int, int)> dfs = [&](int u, int fa, int cnt) {
if (cnt && u == fa) {
ok = false;
return;
}
for (auto v : G[u]) {
vis[v] = true;
dfs(v, fa, cnt + 1);
}
};
for (int i = 1; i < cnt; i++) {
if (!vis[i] && ok) {
vis[i] = true;
dfs(i, i, 0);
}
}
if (ok) {
cout << "Yes\n";
} else {
cout << "No\n";
}
return 0;
}
DP
题意是每周至少休息一天,工作的那一天距离最近的休息日的天数i即为当日的产量a[i],因为至少必须休息一天,所以假设第一天就是休息日,dp[i][j]状态就是前i天里,到目前为止连续工作了j天,初始状态dp[1][0]=0,其他设置为负无穷,再考虑两个休息日之间的所有工作日产量之和如何得到,通过枚举发现i天工作日的产量最多到a[(i+1)/2],所以只需要求出1到n,a[(i+1)/2]的前缀和即可,状态转移就是如果这一天是工作日,那么dp[i][j]=max(dp[i][j],dp[i-1][j-1]),如果这一天是休息日,那么dp[i][0]=max(dp[i][0],dp[i-1][j-1]+b[j])
AC代码:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<LL> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
b[i] = b[i - 1] + a[(i + 1) / 2];
}
vector<vector<LL>> dp(n + 1, vector<LL> (n + 1, -1e18));
dp[1][0] = 0;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dp[i - 1][j - 1] < 0) {
continue;
}
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
dp[i][0] = max(dp[i][0], dp[i - 1][j - 1] + b[j - 1]);
}
}
LL res = 0;
for (int i = 0; i < n; i++) {
res = max(res, dp[n][i] + b[i]);
}
cout << res << '\n';
return 0;
}