CSDN编程竞赛报名地址:https://edu.csdn.net/contest/detail/45
1. 题目名称:最优利润值
你在读的经营课程上,老师布置了一道作业。在一家公司的日常运营中,是会对一些商品的价格走势根据一些经验和数据 进行预估,并据此进行决策。例如,假设某商品每天的价格都有可能变动,我们要做的就是低买高卖获得最高利润。比如假设我们预估该商品接下来七天内的价格走势如下: 4 1 2 3 6 4 8 那我们采取的最佳策略是在价格1块钱的时候买入,在价格8块钱的时候卖出。为了简化整个过程,我们限定在此周期内只能有一次买入一次卖出,且商品在没有购入前是无法卖出的,即该商品不是期货而是现货。现要求你用程序来实现自动决策。输入一定天数的商品预估价格,自动计算出最优利润值。例如,上面的例子中,最优利润值为8-1=7。(简单起见,只考虑0-100000之间的整数价格)
由题意知只能做一次买入一次卖出,因此可以枚举在哪天买入,然后在之后价格最高的一天卖出。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int a[maxn];
int main()
{
int ans = 0, x, n = 0;
while (~scanf("%d", &x))
{
a[n++] = x;
int mx = 0;
for (int i = n - 1; i >= 0; --i)
{
mx = max(mx, a[i]);
ans = max(ans, mx - a[i]);
}
}
printf("%d\n", ans);
return 0;
}
2. 题目名称:开学趣闻之美食诱惑
小艺酱又开学了,可是在上学的路上总会又各种意想不到的美食诱惑让小艺酱迟到。假设小艺酱家到学校是一个n*n的矩 阵。每个格子包含一个诱惑值p,诱惑着小艺,让她迟到。 小艺位于矩阵的左上角,学校在矩阵的右下角落。小艺想知道自己到达学校所要经历的最小诱惑值是?
经典DP,dp[i][j]表示从起点到行 i 列 j 的最小诱惑值,由于 p >= 0,一定是从上方或者左方进入行 i 列 j 最优。那么: 。最后dp[n-1][n-1]为答案。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
int a[maxn][maxn], dp[maxn][maxn];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
scanf("%d", &a[i][j]);
dp[i][j] = INF;
}
}
dp[0][0] = a[0][0];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[i][j]);
if (j)
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i][j]);
}
}
printf("%d\n", dp[n - 1][n - 1]);
return 0;
}
3. 题目名称:小艺照镜子
回文串是一个正读和反读都一样的字符串,已知字符串str。 输出字符串str中最长回文串的长度。
Manacher算法计算回文子串。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
char Ma[maxn * 2];
int Mp[maxn * 2];
void Manacher(char *s, int len)
{
int l = 0;
Ma[l++] = '$';
Ma[l++] = '#';
for (int i = 0; i < len; ++i)
{
Ma[l++] = s[i];
Ma[l++] = '#';
}
Ma[l] = 0;
int mx = 0, id = 0;
for (int i = 0; i < l; ++i)
{
Mp[i] = mx > i ? min(Mp[2 * id - i], mx - i) : 1;
while (Ma[i + Mp[i]] == Ma[i - Mp[i]])
++Mp[i];
if (i + Mp[i] > mx)
{
mx = i + Mp[i];
id = i;
}
}
}
char s[maxn];
int main()
{
scanf("%s", s);
int len = strlen(s);
Manacher(s, len);
int ans = 0;
for (int i = 0; i < 2 * len + 2; ++i)
{
ans = max(ans, Mp[i] - 1);
}
printf("%d\n", ans);
return 0;
}
4. 题目名称:爱吃鬼
小艺酱每天都在吃和睡中浑浑噩噩的度过。 可是小肚子是有空间上限v的。 小艺酱有n包零食,每包零食占据小肚子空间 #*#a_i#*#并会给小艺酱一个甜蜜值#*#b_i#*#。 小艺酱想知道自己在小肚子空间上限允许范围内最大能获得的甜蜜 值是多少?
感觉题目的意思是01背包,但是样例看起来又是多重背包。结果多重背包只能过10%。疑惑。