核心:从左往右遍历,找递增序列,从右往左遍历,找递增序列,输出两者最大值
本质:熊猫重的喝的奶要比别人至少多100ml
满分
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10010;
int p[N], l[N], r[N];
int main()
{
int m, num = 2, sum = 0;
cin >> m;
for(int i = 0; i < m; i++) cin >> p[i];
l[0] = num;
for(int i = 1; i < m; i++)
{
if (p[i] > p[i - 1]) l[i] = l[i - 1] + 1;
else if (p[i] == p[i - 1]) l[i] = l[i - 1];
else l[i] = 2;
}
num = 2;
r[m - 1] = num;
for (int i = m - 2; i >= 0; i--)
{
if (p[i] > p[i + 1]) r[i] = r[i + 1] + 1;
else if (p[i] == p[i + 1]) r[i] = r[i + 1];
else r[i] = 2;
}
for(int i = 0; i < m; i++)
{
sum += max(l[i], r[i]);
}
printf("%d", sum * 100);
}
17分代码 最后一个测试点错误
/**
题意: 胖胖的熊猫需要多的牛奶
做法:两次循环即可,先从左到右,排升序
然后从右到左,排升序
核心: 用两个数组存储,取其最大值
*/
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10010;
int w[N], milk[N];
int main()
{
int m;
cin >> m;
for(int i = 0; i < m; i++) cin >> w[i];
milk[0] = 200;
for(int i = 0; i < m - 1; i ++)
{
if (w[i + 1] > w[i]) milk[i + 1] = milk[i] + 100;
else
{
if (w[i + 1] == w[i] || milk[i] == 200) milk[i + 1] = milk[i];
else milk[i + 1] = milk[i] - 100;
}
}
for(int i = m - 1; i > 0; i--)
{
if (w[i] < w[i - 1]) milk[i - 1] = milk[i] + 100;
}
int sum = 0;
for(int i = 0; i < m; i++) sum += milk[i];
printf("%d", sum);
}
可以做个解释么?