news 2026/5/1 6:28:21

tuple|set

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
tuple|set

lc3811

合法子序列dp

lc560

前缀异或_hash动态统计,计算将数组分割为交替异或和等于 target1 、 target2 的子段的方案数,结果取模 10^9+7

class Solution {
public:
int alternatingXOR(vector<int>& nums, int target1, int target2) {
constexpr int MOD = 1'000'000'007;
unordered_map<int, int> f1;
unordered_map<int, int> f2 = {{0, 1}};
int pre_sum = 0;


for (int i = 0; ; i++) {
pre_sum ^= nums[i];
int last1 = f2[pre_sum ^ target1];

// [0,i] 的最后一段的异或和是 target1 的方案数
int last2 = f1[pre_sum ^ target2];

// [0,i] 的最后一段的异或和是 target2 的方案数
if (i == nums.size() - 1)
return (last1 + last2) % MOD;

f1[pre_sum] = (f1[pre_sum] + last1) % MOD;
f2[pre_sum] = (f2[pre_sum] + last2) % MOD;
}
}
};

不推荐用 状态机dp 不一定具有周期性


lc3810

class Solution {
public:
int minOperations(vector<int>& nums, vector<int>& target) {
unordered_set<int> st;
for (int i = 0; i < nums.size(); i++) {
int x = nums[i];
if (x != target[i]) {
st.insert(x);
}
}
return st.size();
}
};

lc3809

多条件tuple cmp trick

ans = min(ans, tuple{-q, x, y}); // 加个负号,变成求 q 的最大值

class Solution {
public:
vector<int> bestTower(vector<vector<int>>& towers, vector<int>& center, int radius) {
int cx = center[0], cy = center[1];
auto ans = tuple{1, -1, -1};
for (auto& t : towers) {
int x = t[0], y = t[1], q = t[2];
if (abs(x - cx) + abs(y - cy) <= radius) {
ans = min(ans, tuple{-q, x, y}); // 加个负号,变成求 q 的最大值
}
}
return {get<1>(ans), get<2>(ans)};
}
};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 4:13:14

温室气体是指能够吸收和辐射红外辐射的气体,主要包括二氧化碳(CO₂)、甲烷(CH₄)、氧化亚氮(N₂O)和氟化气体

温室气体是指能够吸收和辐射红外辐射的气体&#xff0c;主要包括二氧化碳&#xff08;CO₂&#xff09;、甲烷&#xff08;CH₄&#xff09;、氧化亚氮&#xff08;N₂O&#xff09;和氟化气体等。它们在大气中积累会导致温室效应增强&#xff0c;进而引发全球气候变暖。随着人…

作者头像 李华
网站建设 2026/4/19 15:49:10

Switch控制器PC适配终极指南:BetterJoy全方位配置与优化技巧

Switch控制器PC适配终极指南&#xff1a;BetterJoy全方位配置与优化技巧 【免费下载链接】BetterJoy Allows the Nintendo Switch Pro Controller, Joycons and SNES controller to be used with CEMU, Citra, Dolphin, Yuzu and as generic XInput 项目地址: https://gitcod…

作者头像 李华
网站建设 2026/4/23 14:22:24

GEO核心技术与AI可见度全景洞察下的GEO优化服务商推荐

在AI搜索重构信息分发规则的2026年&#xff0c;企业面临的关键决策已从“是否做GEO”转变为“选择何种GEO伙伴”。生成式引擎优化&#xff08;GEO&#xff09;领域已形成多层次竞争格局&#xff0c;技术路线与服务模式分化显著。在众多服务商中&#xff0c;万数科技凭借全栈自研…

作者头像 李华