BestCoder Round #47

1001 Senior's Array

题目链接:1001

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

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

code:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int MAXN = 1005;
 5 typedef long long LL;
 6 int a[MAXN];
 7 
 8 LL getSum(int a[], int n)
 9 {
10     LL maxSum = 0, tmpSum = 0;
11     int maxNum = -1000000001;
12     for (int i = 0; i < n; ++i)
13     {
14         tmpSum += a[i];
15         if (maxSum < tmpSum)
16             maxSum = tmpSum;
17         else if (tmpSum < 0)
18             tmpSum = 0;
19         maxNum = max(maxNum, a[i]);
20     }
21     if (maxSum == 0) return (LL)maxNum;
22     return maxSum;
23 }
24 
25 int main()
26 {
27     int nCase;
28     cin >> nCase;
29     while (nCase--)
30     {
31         int n, P;
32         cin >> n >> P;
33         for (int i = 0; i < n; ++i)
34             cin >> a[i];
35         LL ans = -1000000001;
36         for (int i = 0; i < n; ++i)
37         {
38             int t = a[i];
39             a[i] = P;
40             ans = max(ans, getSum(a, n));
41             a[i] = t;
42         }
43         cout << ans << endl;
44     }
45     return 0;
46 }

1002 Senior's Gun

题目链接:1002

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

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

code:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int MAXN = 100005;
 5 typedef long long LL;
 6 int a[MAXN];
 7 int b[MAXN];
 8 
 9 bool cmp(int x, int y)
10 {
11     return x > y;
12 }
13 
14 int main()
15 {
16     int nCase;
17     cin >> nCase;
18     while (nCase--)
19     {
20         int n, m;
21         cin >> n >> m;
22         for (int i = 0; i < n; ++i)
23             cin >> a[i];
24         for (int i = 0; i < m; ++i)
25             cin >> b[i];
26         sort(a, a + n, cmp);
27         sort(b, b + m);
28         LL ans = 0;
29         int p = 0, q = 0;
30         while (p < n && q < m)
31         {
32             if (a[p] > b[q])
33             {
34                 ans += a[p] - b[q];
35                 ++p;
36                 ++q;
37             }
38             else break;
39         }
40         cout << ans << endl;
41     }
42     return 0;
43 }

 

posted @ 2015-07-12 10:09  jasaiq  阅读(149)  评论(0编辑  收藏  举报