"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 G.Great Atm【贪心】

本文介绍了一道编程题,挑战者需要帮助战士Atm通过执行一系列比特运算最大化其战斗力。文章提供了问题描述、输入输出示例及解析,并附带了AC代码实现。

Great Atm
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 99(19 users)Total Accepted: 32(16 users)Rating: Special Judge: No
Description
An old story said the evil dragon wasn’t evil at all, only bewitched, and now that the riddles were solved it was proving to be as kind as its new master.
A powerful warrior Atm is going to solve the riddles. First, he should beat the evil wizard. The road from Atm’s castle to wizard’s lab is filled with magic traps. The magic trap will affect Atm’s combat effectiveness.
Atm’s combat effectiveness can be considered as an integer. Effect of magic trap can be considered as mathematical operation. The three kinds of magic traps correspond to three kind of bit operation. (AND, OR and XOR)
Atm can adjust his equipment to change his initial combat effectiveness from 0 to m (include 0 and m). He wants when he arrives the wizard’s lab, his combat effectiveness can be maximum.
Input
There are multiple test cases.
For each test cases:
The first line contains two integers n(1<=n<=10^5) and m(1<=m<=10^9), indicating the number of magic traps and the maximum of initial combat effectiveness.
Each of the next n lines contains a string and an integer, indicating the bit operation. The string will be “AND”, “OR” or “XOR” correspond to AND operation (&), OR operation (|) or XOR operation (^). The integer t(1<=t<=10^9) is second operand in the operation.

Output
For each test cases, a line contains an integer, indicating the maximum combat effectiveness when he arrives the wizard's lab.
Sample Input
3 10
AND 5
OR 6
XOR 7
Sample Output
1
Source
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛

题目大意:

给你N个操作,一开始可以选择【0~m】中的一个数作为起点,然后进行N个操作之后,得到结果,现在问你最大的结果。


思路:


听说数据很水?


1、首先,对于一个结果,我们如果转化成二进制,那么对应第i位的结果,只受到初始的那个数第i位是什么,(0或者1);

那么我们可以预处理出来,对应每一位,初始的时候是0和1的结果。


2、那么接下来进行贪心,如果一个位子上,原来初始的时候是0就能得到1,那么肯定我们这一位初始的数直接设定为0即可。

那么如果原来初始的时候是0并不能得到1,那么再看看原来初始的时候是1是否能够得到1.如果可以,那么我们可以考虑这个位子是否取1.

那么如何考虑取1的方案呢?

很明显,我们从高位开始贪心即可。

那么我们过程讨论从高位开始,如果这一位:

①这一位初始是0结果是1.那么取0.否则转②

②这一位初始是1结果是1.那么取1.否则取0.因为此时取0取1都一样,那么我们肯定希望此时数小更好,因为之后还有可能取1的地方很多,留给那些位子取1是最优的。

③过程维护好数字不要超过上限m即可。


Ac代码:



#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long int
ll n,m;
char a[105000][10];
ll non[105000];
ll num[105000];
ll poww[105000];
ll use[10500];
void init()
{
    for(int i=0; i<=35; i++)
    {
        if(i==0)poww[i]=1;
        else
            poww[i]=poww[i-1]*2;
    }
}
int main()
{
    init();
    while(~scanf("%lld%lld",&n,&m))
    {
        memset(non,0,sizeof(non));
        memset(use,0,sizeof(use));
        for(int i=0; i<n; i++)
        {
            scanf("%s%d",a[i],&num[i]);
        }
        for(int i=35; i>=0; i--)
        {
            if(poww[i]<=m)
            {
                ll ceshi2=0;
                ll ceshi=poww[i];
                for(int j=0; j<n; j++)
                {
                    if(a[j][0]=='A')
                    {
                        ceshi=(ceshi&num[j]);
                        ceshi2=(ceshi2&num[j]);
                    }
                    if(a[j][0]=='O')
                    {
                        ceshi=(ceshi|num[j]);
                        ceshi2=(ceshi2|num[j]);
                    }
                    if(a[j][0]=='X')
                    {
                        ceshi=(ceshi^num[j]);
                        ceshi2=(ceshi2^num[j]);
                    }
                }
                if((ceshi2&poww[i])>0)non[i]=1;
                if((ceshi&poww[i])>0)use[i]=1;
            }
        }
        ll yu=m;
        ll outputnum=0;
        for(int i=35; i>=0; i--)
        {
            if(non[i]==1)continue;
            if(use[i]==1)
            {
                if(yu>=poww[i])
                {
                    yu-=poww[i];
                    outputnum+=poww[i];
                }
            }
        }
        for(int j=0; j<n; j++)
        {
            if(a[j][0]=='A')
            {
                outputnum=(outputnum&num[j]);
            }
            if(a[j][0]=='O')
            {
                outputnum=(outputnum|num[j]);
            }
            if(a[j][0]=='X')
            {
                outputnum=(outputnum^num[j]);
            }
        }
        printf("%lld\n",outputnum);
    }
}







内容概要:本文介绍了一个基于MATLAB实现的无人机三维路径规划项目,采用蚁群算法(ACO)与多层感知机(MLP)相结合的混合模型(ACO-MLP)。该模型通过三维环境离散化建模,利用ACO进行全局路径搜索,并引入MLP对环境特征进行自适应学习与启发因子优化,实现路径的动态调整与多目标优化。项目解决了高维空间建模、动态障碍规避、局部最优陷阱、算法实时性及多目标权衡等关键技术难题,结合并行计算与参数自适应机制,提升了路径规划的智能性、安全性和工程适用性。文中提供了详细的模型架构、核心算法流程及MATLAB代码示例,涵盖空间建模、信息素更新、MLP训练与融合优化等关键步骤。; 适合人群:具备一定MATLAB编程基础,熟悉智能优化算法与神经网络的高校学生、科研人员及从事无人机路径规划相关工作的工程师;适合从事智能无人系统、自动驾驶、机器人导航等领域的研究人员; 使用场景及目标:①应用于复杂三维环境下的无人机路径规划,如城市物流、灾害救援、军事侦察等场景;②实现飞行安全、能耗优化、路径平滑与实时避障等多目标协同优化;③为智能无人系统的自主决策与环境适应能力提供算法支持; 阅读建议:此资源结合理论模型与MATLAB实践,建议读者在理解ACO与MLP基本原理的基础上,结合代码示例进行仿真调试,重点关注ACO-MLP融合机制、多目标优化函数设计及参数自适应策略的实现,以深入掌握混合智能算法在工程中的应用方法。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值