BestCoder Round #47

时间:2022-11-13 06:57:58

1001 Senior's Array

题目链接:1001

题意:给你一个长度为n的序列,你必须修改序列中的某个数为P,求修改后的最大连续子序列和。

思路:数据量比较小,可以直接暴力做, 枚举序列的每个数修改成P,然后更新最大子序列和。

code:

 #include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = ;
typedef long long LL;
int a[MAXN]; LL getSum(int a[], int n)
{
LL maxSum = , tmpSum = ;
int maxNum = -;
for (int i = ; i < n; ++i)
{
tmpSum += a[i];
if (maxSum < tmpSum)
maxSum = tmpSum;
else if (tmpSum < )
tmpSum = ;
maxNum = max(maxNum, a[i]);
}
if (maxSum == ) return (LL)maxNum;
return maxSum;
} int main()
{
int nCase;
cin >> nCase;
while (nCase--)
{
int n, P;
cin >> n >> P;
for (int i = ; i < n; ++i)
cin >> a[i];
LL ans = -;
for (int i = ; i < n; ++i)
{
int t = a[i];
a[i] = P;
ans = max(ans, getSum(a, n));
a[i] = t;
}
cout << ans << endl;
}
return ;
}

1002 Senior's Gun

题目链接:1002

题意:有n把枪每把枪都有一定的攻击值,有m个怪兽每个怪兽都有一定的防御值,当攻击值大于防御值时才能击杀怪兽并获得他们差值的酬劳,求最大酬劳。

思路:容易发现最后的方案一定是攻击力最强的k把枪消灭了防御力最弱的k只怪物。

code:

 #include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = ;
typedef long long LL;
int a[MAXN];
int b[MAXN]; bool cmp(int x, int y)
{
return x > y;
} int main()
{
int nCase;
cin >> nCase;
while (nCase--)
{
int n, m;
cin >> n >> m;
for (int i = ; i < n; ++i)
cin >> a[i];
for (int i = ; i < m; ++i)
cin >> b[i];
sort(a, a + n, cmp);
sort(b, b + m);
LL ans = ;
int p = , q = ;
while (p < n && q < m)
{
if (a[p] > b[q])
{
ans += a[p] - b[q];
++p;
++q;
}
else break;
}
cout << ans << endl;
}
return ;
}