回溯+位运算|前缀和优化背包

lc2402

双堆模拟

用两个优先队列分别管理空闲会议室和使用中会议室的结束时间与编号

按会议开始时间排序后依次安排会议

统计各会议室使用次数并返回使用最多的那个会议室编号

class Solution {

public:

int mostBooked(int n, vector<vector<int>>& meetings) {

ranges::sort(meetings, {}, \[\](auto& m) { return m0; });

priority_queue<int, vector<int>, greater<>> idle; // 会议室编号

for (int i = 0; i < n; i++) {

idle.push(i);

}

priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> using_; // (结束时间,会议室编号)

vector<int> cnt(n); // 会议室的开会次数

for (auto& m : meetings) {

long long start = m0, end = m1;

// 在 start 时刻空出来的会议室

while (!using_.empty() && using_.top().first <= start) {

idle.push(using_.top().second);

using_.pop();

}

int i;

if (!idle.empty()) { // 有空闲的会议室

i = idle.top();

idle.pop();

} else { // 没有空闲的会议室

// 弹出一个最早结束的会议室(若有多个同时结束,弹出编号最小的会议室)

auto e, room = using_.top();

i = room;

using_.pop();

end += e - start; // 更新当前会议的结束时间

}

using_.emplace(end, i); // 使用一个会议室

cnti++;

}

return ranges::max_element(cnt) - cnt.begin();

}

};

lc756

状态压缩+记忆化DFS

将字符映射为数字并压缩存储金字塔每一层状态,利用允许++组合表递归尝试构建上一层++,判断能否从底部生成合法金字塔

class Solution {

public:

bool pyramidTransition(string bottom, vector<string>& allowed) {

vector<int> groups77;

for (auto& s : allowed) {

// A~F -> 1~6

groupss\[0 & 31]s\[1 & 31].push_back(s2 & 31);

}

int n = bottom.size();

vector<int> pyramid(n);

for (int i = 0; i < n; i++) {

pyramidn - 1 |= (bottomi & 31) << (i * 3); // 等价于 pyramidn-1i = bottomi&31

}

vector<uint8_t> vis(1 << ((n - 1) * 3));

auto dfs = \&(this auto&& dfs, int i, int j) -> bool {

if (i < 0) {

return true;

}

if (vispyramid\[i]) {

return false;

}

if (j == i + 1) {

vispyramid\[i] = true;

return dfs(i - 1, 0);

}

for (int top : groupspyramid\[i + 1 >> (j * 3) & 7]pyramid\[i + 1 >> ((j + 1) * 3) & 7]) {

pyramidi &= ~(7 << (j * 3)); // 清除之前填的字母,等价于 pyramidij = 0

pyramidi |= top << (j * 3); // 等价于 pyramidij = top

if (dfs(i, j + 1)) {

return true;

}

}

return false;

};

return dfs(n - 2, 0);

}

};

lc3333

前缀和优化多重背包

class Solution {

public:

int possibleStringCount(string word, int k) {

int n = word.size();

if (n < k) { // 无法满足要求

return 0;

}

const int MOD = 1'000'000'007;

vector<int> cnts;

long long ans = 1;

int cnt = 0;

for (int i = 0; i < n; i++) {

cnt++;

if (i == n - 1 || wordi != wordi + 1) {

// 如果 cnt = 1,这组字符串必选,无需参与计算

if (cnt > 1) {

if (k > 0) {

cnts.push_back(cnt - 1);

}

ans = ans * cnt % MOD;

}

k--; // 注意这里把 k 减小了

cnt = 0;

}

}

if (k <= 0) {

return ans;

}

int m = cnts.size();

vector f(m + 1, vector<int>(k));

ranges::fill(f0, 1);

vector<int> s(k + 1);

for (int i = 0; i < m; i++) {

// 计算 fi 的前缀和数组 s

for (int j = 0; j < k; j++) {

sj + 1 = (sj + fij) % MOD;

}

// 计算子数组和

for (int j = 0; j < k; j++) {

fi + 1j = (sj + 1 - smax(j - cnts\[i, 0)]) % MOD;

}

}

return (ans - fmk - 1 + MOD) % MOD; // 保证结果非负

}

};

相关推荐
c2385621 小时前
vector(下)
数据结构·算法
z落落21 小时前
C# 冒泡排序+选择排序 + Array.Sort 自定义排序
数据结构·算法
wyy1851007372821 小时前
双路并行:一套匹配算法如何解决中文制单的两大核心难题
算法·ai·crm·crm系统
s_w.h21 小时前
【 linux 】文件系统
linux·运维·服务器·算法·bash
无限进步_1 天前
【C++】weak_ptr、循环引用与线程安全
开发语言·数据结构·c++·算法·安全
罗超驿1 天前
9.LeetCode 209. 长度最小的子数组 | 滑动窗口专题详解
java·算法·leetcode·面试
水蓝烟雨1 天前
0135. 分发糖果
算法·leetcode
IronMurphy1 天前
【算法五十二】5. 最长回文子串
算法
Lewiis1 天前
白话选择排序
数据结构·算法·排序算法
计算机安禾1 天前
【算法分析与设计】第19篇:二分图匹配与指派问题
大数据·人工智能·算法