邢臺網站制作報價多少錢seo研究中心倒閉
藍橋杯小白賽?
???????3.小藍的金牌夢【算法賽】 - 藍橋云課 (lanqiao.cn)
數組長度為質數,最大的子數組和
素數 + 前綴和
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define N 100010
int ans[N];int s[N];vector<int> prime;
bool is_prime[N];void Era(int n) {is_prime[0] = is_prime[1] = true;for (int i = 2; i <= n; ++i) {if (!is_prime[i]) {prime.push_back(i);if ((int)i * i > n) continue;for (int j = i * i; j <= n; j += i) is_prime[j] = true;}}
}
void Eratosthenes(int n) {is_prime[0] = is_prime[1] = false;for (int i = 2; i <= n; ++i) is_prime[i] = true;for (int i = 2; i <= n; ++i) {if (is_prime[i]) {prime.push_back(i);if ((long long)i * i > n) continue;for (int j = i * i; j <= n; j += i)// 因為從 2 到 i - 1 的倍數我們之前篩過了,這里直接從 i// 的倍數開始,提高了運行速度is_prime[j] = false; // 是 i 的倍數的均不是素數}}
}
signed main() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);int maxn = -1e18;;int n, t;cin >> n;for (int i = 1; i <= n; ++i) {cin >> t;s[i] = s[i-1] + t;}Era(n);for (int i = 0; i < prime.size(); ++i) {for (int j = prime[i]; j <= n; ++j) {maxn = max(maxn, s[j]-s[j-prime[i]]);}}// for (auto x:prime) {// cout << x << ' ';// }cout << maxn << endl;return 0;
}
4.合并石子加強版【算法賽】 - 藍橋云課 (lanqiao.cn)
數學
a*b + (a+b)*c == a*b + a* c + b*c == b*c + (b+c)*a
#include "bits/stdc++.h"
using namespace std;
#define int unsigned long long
signed main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);int n, t, sum=0, free=0;cin>>n;// cin >> sum;for(int i=0;i<n;++i){cin >> t;free += t*sum;sum += t;// free += ;}cout << free << endl;return 0;
}
力扣雙周賽
100130. 找到兩個數組中的公共元素 - 力扣(LeetCode)
看錯題意
class Solution {
public:vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {unordered_set<int> set1(nums1.begin(), nums1.end());unordered_set<int> set2(nums2.begin(), nums2.end());vector<int> ans(2);for (int x: nums1) ans[0] += set2.count(x);for (int x: nums2) ans[1] += set1.count(x);return ans;}
};
?總結
set1(nums1.begin(), nums2.end())? 容器可以直接根據另一個容器初始化
set.count(x)? ? ? ? ?如果找到數值為x的元素,返回1;找不到,返回0
100152. 消除相鄰近似相等字符 - 力扣(LeetCode)
?看成首位也需要判斷(環(huán))
貪心,貪第二個
class Solution {
public:int removeAlmostEqualCharacters(string word) {int cnt = 0;for (int i = 0; i < word.size() - 1; ++i){if (abs((word[i]-word[i+1])) <= 1) {// word[i+1] = 'A';++i;++cnt;}}return cnt;}
};
2958. 最多 K 個重復元素的最長子數組 - 力扣(LeetCode)
暴力超時
子數組:1 哈希表 2 滑動窗口
class Solution {
public:int maxSubarrayLength(vector<int>& nums, int k) {int ans = 0, left = 0;unordered_map<int, int> cnt;for (int right = 0; right < nums.size(); ++right) {++cnt[nums[right]];while (cnt[nums[right]] > k) {--cnt[nums[left++]];}ans = max(ans, right - left + 1);}return ans;}
};
本周計劃:
盛最多水的容器 接雨水_嗶哩嗶哩_bilibili
同向雙指針 滑動窗口【基礎算法精講 03】_嗶哩嗶哩_bilibili
2958. 最多 K 個重復元素的最長子數組 - 力扣(LeetCode)?
2560. 打家劫舍 IV - 力扣(LeetCode)
?