不太清楚哪些边界条件没有判断,比如它题目中给出 fee > 0 ,我还需要在程序里判断一下嘛?
(好像测试的时候,判断 fee <= 0,过的案例更少了,不知道是什么问题
求大佬们指教一下!!感谢感谢!!!
1. 股票 0.83
#include<bits/stdc++.h> using namespace std; int main(){ string str; cin >> str; if (str.size() <= 2) return 0; vector<int> price; for (int i = 1; i < str.size() - 1; i += 2){ if (str[i] <= '9' && str[i] >= '0'){ int tmp = str[i] - '0'; price.push_back(tmp); } } int fee; cin >> fee; int n = price.size(); vector<vector<int>> dp(n, vector<int>(2, 0)); dp[0][0] -= price[0]; for (int i = 1; i < n; ++i){ dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - price[i]); dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + price[i] - fee); } cout << max(dp[n - 1][0], dp[n - 1][1]); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int A[12]; for (int i = 1; i <= 10; i++) cin >> A[i]; A[0] = A[10]; A[11] = A[1]; int temp[12]; int m; cin >> m; while (m--){ for (int i = 1; i <= 10; i++){ temp[i] = A[i - 1] ^ A[i + 1]; } for (int j = 1; j <= 10; j++){ A[j] = temp[j]; } A[0] = temp[10]; A[11] = temp[1]; } for (int i = 1; i <= 10; i++) cout << A[i] << ' '; return 0; }
3. score 0.5
#include<bits/stdc++.h> using namespace std; #define ll long long int A[300010]; ll sumA[300010]; // 前 i 个数的和 int n, k; ll res = LLONG_MIN; void backtracking(int K, int Sum, int startIndex){ if (K == k){ if (startIndex < n){ Sum += K * (sumA[n] - sumA[startIndex] + A[startIndex]); res = res > Sum ? res : Sum; } return; } for (int i = startIndex; i <= n - 1 -(k - K); i++){ if (i == 0) continue; Sum += K * (sumA[i] - sumA[startIndex] + A[startIndex]); K++; backtracking(K, Sum, i + 1); K--; Sum -= K * (sumA[i] - sumA[startIndex] + A[startIndex]); } } int main(){ cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> A[i]; for (int i = 1; i <= n; ++i) sumA[i] += sumA[i - 1] + A[i]; // sumA[0] = 0; backtracking(1, 0, 0); cout << res; return 0; }
全部评论
(0) 回帖