当前位置:首页 > 技术 > 正文内容

Codeforces位运算压缩、路径规划与背包问题题解

访客 技术 2026年6月27日 1

位运算指令压缩(Codeforces 879C)

给定n个位运算指令(与、或、异或),需压缩为不超过5个等效指令。核心思路:通过全0和全1二进制数(0和1023)模拟运算,推断最终操作组合。

#include <iostream>
using namespace std;

int main() {
    int n, op_val;
    char op_type[3];
    cin >> n;
    int zero_state = 0, all_one_state = 1023;
    
    while(n--) {
        scanf("%s%d", op_type, &op_val);
        if(op_type[0] == '^') {
            zero_state ^= op_val;
            all_one_state ^= op_val;
        } 
        else if(op_type[0] == '&') {
            zero_state &= op_val;
            all_one_state &= op_val;
        } 
        else {
            zero_state |= op_val;
            all_one_state |= op_val;
        }
    }
    
    int AND = 0, OR = 0, XOR = 0;
    int bit_mask = 1;
    for(int i = 0; i < 10; ++i) {
        int low_bit = zero_state & 1;
        int high_bit = all_one_state & 1;
        
        if(low_bit == 0) {
            if(high_bit == 1) AND |= bit_mask;
        } 
        else {
            if(high_bit == 0) {
                XOR |= bit_mask;
                AND |= bit_mask;
            } 
            else {
                OR |= bit_mask;
                AND |= bit_mask;
            }
        }
        zero_state >>= 1;
        all_one_state >>= 1;
        bit_mask <<= 1;
    }
    printf("3\n& %d\n| %d\n^ %d\n", AND, OR, XOR);
    return 0;
}

加油路径规划(Codeforces 864C)

车辆在长a的道路往返k次,油箱容量b,距起点f处有加油站。求最少加油次数。模拟行驶过程并检测油量临界点。

#include <iostream>
using namespace std;

int main() {
    long total_dist, capacity, station_pos, trips;
    cin >> total_dist >> capacity >> station_pos >> trips;
    long fuel = capacity, refuels = 0;
    
    for(int i = 1; i <= trips; ++i) {
        if(i % 2 == 1) { // 去程
            if(fuel < station_pos) { 
                cout << -1 << endl; 
                return 0; 
            }
            fuel -= station_pos;
            
            if(i == trips) { 
                if(fuel < total_dist - station_pos) {
                    if(capacity < total_dist - station_pos) {
                        cout << -1 << endl;
                        return 0;
                    }
                    refuels++;
                }
            } 
            else {
                if(fuel < 2 * (total_dist - station_pos)) {
                    refuels++;
                    fuel = capacity;
                }
                fuel -= (total_dist - station_pos);
            }
        } 
        else { // 返程
            if(fuel < total_dist - station_pos) {
                cout << -1 << endl;
                return 0;
            }
            fuel -= (total_dist - station_pos);
            
            if(i == trips) {
                if(fuel < station_pos) refuels++;
            } 
            else {
                if(fuel < 2 * station_pos) {
                    refuels++;
                    fuel = capacity;
                }
                fuel -= station_pos;
            }
        }
    }
    cout << refuels << endl;
    return 0;
}

火灾救援背包问题(Codeforces 864E)

抢救n件物品,每件有耗时t、销毁时间d、价值p。求最大可救价值及物品序列。采用动态规划记录决策路径。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Item {
    int time, deadline, value, id;
};

bool cmp(Item x, Item y) { 
    return x.deadline < y.deadline; 
}

int main() {
    int n, max_time = 0;
    cin >> n;
    Item items[105];
    for(int i = 1; i <= n; ++i) {
        cin >> items[i].time >> items[i].deadline >> items[i].value;
        items[i].id = i;
        max_time = max(max_time, items[i].deadline);
    }
    sort(items + 1, items + n + 1, cmp);
    
    int dp[105][2005] = {0};
    bool selected[105][2005] = {0};
    
    for(int i = 1; i <= n; ++i) {
        for(int j = 0; j <= max_time; ++j) {
            dp[i][j] = dp[i-1][j];
            if(j >= items[i].time && j < items[i].deadline) {
                int new_val = dp[i-1][j - items[i].time] + items[i].value;
                if(new_val > dp[i][j]) {
                    dp[i][j] = new_val;
                    selected[i][j] = true;
                }
            }
        }
    }
    
    int max_val = 0, end_time = 0;
    for(int t = 0; t <= max_time; ++t) {
        if(dp[n][t] > max_val) {
            max_val = dp[n][t];
            end_time = t;
        }
    }
    
    vector<int> path;
    for(int i = n; i >= 1; --i) {
        if(selected[i][end_time]) {
            path.push_back(items[i].id);
            end_time -= items[i].time;
        }
    }
    reverse(path.begin(), path.end());
    
    cout << max_val << "\n" << path.size() << "\n";
    for(int id : path) cout << id << " ";
    return 0;
}
标签: 位运算压缩

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

linux screen 用法详情 (nohup 的替代方案)

一、screen 是什么?能干嘛?screen 是一个终端复用器,可以:在一个 SSH 会话中开多个“虚拟终端”SSH 断线后,程序仍然在后台运行随时重新连接到原来的会话特别适合:nohup 的替代方案跑脚本 / 爬虫 / 训练模型运维、远程开发二、安装 screen# CentOS / Rocky / Almayum install -y screen# Debian / Ubuntuapt i...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。