蓝桥杯:C*++ Calculations(不按顺序的加和)贪心解法

问题描述

C*++语言和C++语言非常相似,然而C*++的程序有时会出现意想不到的结果。比如像这样的算术表达式:

  • 表达式=基本式 / 表达式+基本式 / 表达式-基本式
  • 基本式=增量 / 系数*增量
  • 增量=a++ / ++a
  • 系数=0/1/2/……/1000

如“5*a++-3*++a+a++”是合法的C*++表达式。
  
计算这样的表达式的值的方法:

  1. 首先是每个基本式进行计算,然后按照正常的算术运算法则计算。
  2. 如果一个基本式包含“a++”,则先进行乘法运算再使变量a权值+1;如果一个基本式包含“++a”,则先使变量a权值+1再进行乘法运算。
  3. 然而基本式可以按任意顺序计算,这就是为什么计算结果是完全无法预料的。你的任务就是去找到最大的可能结果。

输入

第一行,一个整数n,表示变量a的初始值。第二行,一个合法的C*++表达式。

输出

共一行,一个整数ans,表示最大可能结果。

样例输入

input 1:

1
5*a++-3*++a+a++

output 1:

11

input 2:

3 
a+++++a

output 2:

8

数据规模和约定

对于20%的数据,表达式长度<=20。另有20%的数据,满足n>=0。对于100%的数据,-1000<=n<=1000,表达式长度<=10000。注意表达式开头可能有负号!

思路

题目的意思就是有很多个 a++ 或 ++a,每个a++之前都有一个系数,a++ 或 ++a 进行的顺序可以随意,求最后表达式最大值

其实题目tag不说是贪心我会往DP上面想的

如果是贪心的话,很容易想到:因为a是逐渐增加的,那么让小的数乘上小的a,让大的数乘上大的a,就可以使值最大

证明 1:优先选择大数乘大a

假设a从 a 变成 a+1,有两个数 x, x+1,那么有两种选择:

  1. (x * a) + ((x+1) * (a+1)) = 2ax + x + a + 1
  2. ((x+1) * a) + (x * (a+1)) = 2ax + x + a

显然让 【大的数】 和 【大的a】 相乘比较赚

但是 a++ 和 ++a 的顺序对于结果有没有影响呢?

答案是没有

证明 2: a++ 和 ++a 的顺序对于结果无影响:
  1. 相同的数字x,a++ 和 ++a 的顺序没有影响
    先a++:(x * a) + (x * (a+2)) = 2ax + 2x
    先++a:(x * (a+1)) + (x * (a+1)) = 2ax + 2x

  2. 不同的数字x, y,a++ 和 ++a 的顺序没有影响
    先a++:r1 = (x * a) + (y * (a+2)) = ax + ay + 2y
    先++a:r2 = (x * (a+1)) + (y * (a+1)) = ax + ay + x + y

r1, r2 的大小只和 x,y 的大小有关,x > y,则 r2 > r1,反之
这也再次说明了贪心策略是【大的数】 和 【大的a】 相乘

代码

贪心比较容易实现,复杂度也不会太大
在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

typedef long long ll;
string app = "a++";
string ppa = "++a";

#define MAXLEN 10009
#define char2int(x) ((int)(x - '0'))
// nums[i][0] 存储第i个a++/++a的系数
// nums[i][1]=0表示是a++,nums[i][1]=1表示是++a
int nums[MAXLEN
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
There are a few issues with this code: 1. The `bit_plane_slicing` function takes in a parameter `src`, but the code inside the function uses `img` instead. This will cause an error as `img` is not defined inside the function. 2. The `cv2.normalize` function is being used incorrectly. It should be `cv2.normalize(src, dst, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX)`. 3. The `image1`, `image2`, and `image3` variables are being calculated incorrectly. The correct calculations should be: ``` image1 = img1*(2**7) + img2*(2**6) image2 = img1*(2**7) + img2*(2**6) + img3*(2**5) image3 = img1*(2**7) + img2*(2**6) + img3*(2**5) + img4*(2**4) ``` Here's the corrected code: ``` import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r'E:\\postgraduate\\three\\DIP3E_Original_Images_CH03\\3.14.tif',0) def bit_plane_slicing(src,z): height, width = src.shape dst = np.zeros((height, width), np.uint8) cv2.normalize(src, dst, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX) for i in range(0, height): for j in range(0, width): pixel = format(src[i,j], '08b') if pixel[8-z] == '0': dst[i, j] = 0 else: dst[i, j] = 255 return dst img1 = bit_plane_slicing(img,8) img2 = bit_plane_slicing(img,7) img3 = bit_plane_slicing(img,6) img4 = bit_plane_slicing(img,5) image1 = img1*(2**7) + img2*(2**6) image2 = img1*(2**7) + img2*(2**6) + img3*(2**5) image3 = img1*(2**7) + img2*(2**6) + img3*(2**5) + img4*(2**4) plt.figure(figsize=(100,100)) plt.subplot(131) plt.imshow(image1,cmap='gray') plt.axis('off') plt.subplot(132) plt.imshow(image2,cmap='gray') plt.axis('off') plt.subplot(133) plt.imshow(image3,cmap='gray') plt.axis('off') plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值