[acm]动态规划-Robberies

本文介绍了一种算法,用于计算在给定被抓概率限制下,从一系列银行中能抢夺的最大金额。该算法考虑了不同银行的安全级别和现金持有量,通过动态规划求解最大期望收益。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university. 


For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible. 


His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
 

Input

The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . 
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
 

Output

For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set. 

Notes and Constraints 
0 < T <= 100 
0.0 <= P <= 1.0 
0 < N <= 100 
0 < Mj <= 100 
0.0 <= Pj <= 1.0 
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
 

Sample Input

3 0.04 3 1 0.02 2 0.03 3 0.05 0.06 3 2 0.03 2 0.03 3 0.05 0.10 3 1 0.03 2 0.02 3 0.05
 

Sample Output

2 4 6
 


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int mj[101];
    int t,n,i,j,k,sum;
    double p,pj[101],a[10001];//a[i]指的是抢劫i数目钱时逃脱的概率
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        sum=0;
        scanf("%lf %d",&p,&n);
        for(j=1;j<=n;j++)
        {
            scanf("%d %lf",&mj[j],&pj[j]);
            sum+=mj[j];
        }
        memset(a,0.0,sizeof(a));
        a[0]=1.0;
        for(j=1;j<=n;j++)
        {
            for(k=sum;k>=mj[j];k--)
            {
                if(a[k]<a[k-mj[j]]*(1-pj[j]))
                {
                    a[k]=a[k-mj[j]]*(1-pj[j]);
                }
            }
        }//通过这个运算将抢劫的数目所对应的概率求出来
        int m;
        for(j=sum;j>=0;j--)
        {
            if(a[j]>=(1-p))
            {
                m=j;
                break;
            }
        }
        printf("%d\n",m);
    }
    return 0;
}



<think>嗯,用户想了解CRC32校验算法的实现和使用方法。我需要先回顾一下CRC32的基本原理,然后考虑如何用简单易懂的中文解释清楚。首先,CRC32是基于循环冗余校验的一种算法,主要用于检测数据传输或存储中的错误。用户可能希望知道它的具体步骤,比如如何初始化、处理数据、异或操作以及最终得到校验值。 根据提供的引用资料,CRC32的核心是多项式除法,但实际中通过查表法优化速度。需要解释生成多项式,比如常见的0xEDB88320,以及初始值、输入输出是否反转这些参数。用户提供的代码片段中有两个版本,一个是逐位计算,另一个是查表法,这可能需要在回答中对比说明,突出查表法的效率优势。 接下来,用户可能关心具体怎么使用这个算法,比如在C语言中的实现步骤,或者如何应用到实际项目中,比如文件校验或网络传输。要分步骤讲解,从初始化到处理每个字节,再到最终计算,可能需要示例代码来辅助说明。同时,注意用户要求行内数学表达式用$...$,比如多项式应该写成$0xEDB88320$这样的格式。 另外,用户的问题里提到生成相关问题,我需要考虑相关的问题可能涉及性能优化、不同生成多项式的影响,或者CRC32与其他校验算法的比较。这些可以作为相关问题放在最后,帮助用户进一步探索。 需要确保回答结构清晰,先讲原理,再讲实现步骤,然后使用方法,最后附上相关问题。同时,引用提供的资料,比如引用[^1]和[^2]来支持解释。要避免技术术语过于复杂,保持语言通俗易懂,同时满足LaTeX格式要求,比如多项式用$符号包裹。检查是否有遗漏的步骤,比如输入反转和输出反转的处理,这在代码示例中有所体现,需要解释清楚这些参数的作用。</think>### CRC32校验算法实现及使用方法 #### 一、CRC32基本原理 CRC32通过多项式除法生成32位校验值,用于检测数据传输或存储中的错误。其核心是生成多项式$G(x)$,常用标准为$0xEDB88320$(反向多项式形式)[^1]。算法流程如下: 1. **初始化**:设置32位寄存器初值为$0xFFFFFFFF$ 2. **数据输入**:逐字节与寄存器异或运算 3. **位移处理**:根据多项式进行循环移位和异或操作 4. **结果取反**:最终值与$0xFFFFFFFF$异或 #### 二、算法实现(C语言) ```c // 查表法实现(高效) uint32_t crc32_table[256]; void init_crc32_table() { for (int i = 0; i < 256; i++) { uint32_t crc = i; for (int j = 0; j < 8; j++) { crc = (crc >> 1) ^ ((crc & 1) ? 0xEDB88320 : 0); } crc32_table[i] = crc; } } uint32_t calc_crc32(uint8_t *data, size_t len) { uint32_t crc = 0xFFFFFFFF; while (len--) { crc = (crc >> 8) ^ crc32_table[(crc ^ *data++) & 0xFF]; } return crc ^ 0xFFFFFFFF; } ``` 此实现通过预先生成的256元素查找表,将时间复杂度从$O(n \cdot 8)$优化到$O(n)$。 #### 三、使用场景及方法 1. **文件校验**:计算文件CRC32值验证完整性 ```bash $ crc32 filename.txt ``` 2. **网络传输**:在数据帧尾部附加4字节校验码 3. **存储系统**:RAID、ZFS等文件系统使用CRC32校验数据块 #### 四、参数配置 | 参数 | 说明 | 标准值 | |---------------|---------------------------|-----------------| | 初始值 | 寄存器初始状态 | 0xFFFFFFFF | | 多项式 | 生成多项式 | 0xEDB88320 | | 输入/输出反转 | 字节处理顺序 | 通常需要反转 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值