1001:Senior's Array
题目大意:
在数组中改一个合适的数(必须修改)使得区间和最大的那个区间的和尽量大,问最大区间和是多大?
解题思路:
数据范围比较小,水题,可以暴力,枚举修改的数字,然后求区间和最大的,最后比较得出最大。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 1010; 7 const int INF = 0x3f3f3f3f; 8 typedef __int64 LL; 9 int main () 10 { 11 LL t, a[maxn]; 12 scanf ("%I64d", &t); 13 while (t --) 14 { 15 LL n, m; 16 scanf ("%I64d %I64d", &n, &m); 17 for (int i=0; i<n; i++) 18 scanf ("%I64d", &a[i]); 19 LL Max = -INF; 20 for (int i=0; i<n; i++) 21 { 22 swap (m, a[i]); 23 LL sum, num; 24 sum = num = -INF; 25 for (int j=0; j<n; j++) 26 { 27 if (num < 0) 28 num = a[j]; 29 else 30 num += a[j]; 31 if (num > sum) 32 sum = num; 33 } 34 if (sum > Max) 35 Max = sum; 36 swap (m, a[i]); 37 } 38 printf ("%I64d ", Max); 39 } 40 return 0; 41 }
1002:Senior's Gun
题目大意:
有n个技能,m个怪兽,当技能的伤害力x大于怪兽的防卫能力y,这个技能就可以消灭这个怪兽,并且得到x-y的分数,怪兽可以不被消灭完,问最大得分是多少?
解题思路:
简单贪心,把技能值和怪兽能力值排序,用最大的技能值减去最小的怪兽防御能力,直到最后没有技能可以消灭怪兽。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 100010; 7 const int INF = 0x3f3f3f3f; 8 typedef __int64 LL; 9 LL a[maxn], b[maxn]; 10 int main () 11 { 12 int t; 13 scanf ("%d", &t); 14 while (t --) 15 { 16 int n, m; 17 scanf ("%d %d", &n, &m); 18 for (int i=0; i<n; i++) 19 scanf ("%I64d", &a[i]); 20 for (int i=0; i<m; i++) 21 scanf ("%I64d", &b[i]); 22 sort (a, a+n); 23 sort (b, b+m); 24 LL sum = 0; 25 for (int i=n-1,j=0;i>=0&&j<m; i--,j++) 26 { 27 if (a[i] < b[j]) 28 break; 29 sum += a[i] - b[j];; 30 } 31 printf ("%I64d ", sum); 32 } 33 return 0; 34 }
1003:Senior's String
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5282
题目描述:
给出两个字符串x,y。假设L为两个字符串的最长公共子序列,问把x拆成若干个长度为L的子序列中有几个是y的子序列?
解题思路:
首先用O(n^2)的方法求出x,y的最长公共子序列数组dp[i][j],然后再用一个数组dp1[i][j]来表示x前i个字符与y的前j个字符最长公共子序列等于dp[i][j]的方案数,对于dp1[i][j]的计算也是对dp[i][j]数组转移策略的讨论。只需要考虑选还是不选x的第i个字符即可。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 1010; 7 const int mod = 1000000007; 8 int dp[maxn][maxn], dp1[maxn][maxn], a[maxn][30]; 9 char x[maxn], y[maxn]; 10 int main () 11 { 12 int t; 13 scanf ("%d", &t); 14 while (t --) 15 { 16 scanf ("%s %s", x, y); 17 int nx = strlen (x); 18 int ny = strlen (y); 19 int temp[30]; 20 memset (a, 0, sizeof(a)); 21 memset (dp, 0, sizeof(dp)); 22 memset (dp1, 0, sizeof(dp1)); 23 memset (temp, 0, sizeof(temp)); 24 for (int i=1; i<=ny; i++) 25 { 26 temp[y[i-1]-'a'] = i; 27 for (int j=0; j<26; j++) 28 a[i][j] = temp[j]; 29 } 30 for (int i=1; i<=nx; i++) 31 for (int j=1; j<=ny; j++) 32 if (x[i-1] == y[j-1]) 33 dp[i][j] = dp[i-1][j-1] + 1; 34 else 35 dp[i][j] = max(dp[i][j-1], dp[i-1][j]); 36 for (int i=0; i<=nx; i++) 37 for (int j=0; j<=ny; j++) 38 { 39 if (dp[i][j] == 0) 40 { 41 dp1[i][j] = 1; 42 continue; 43 } 44 if (dp[i][j] == dp[i-1][j]) 45 dp1[i][j] = (dp1[i][j] + dp1[i-1][j]) % mod; 46 int p = a[j][x[i-1]-'a']; 47 if (p && dp[i-1][p-1] + 1 == dp[i][j]) 48 dp1[i][j] = (dp1[i][j] + dp1[i-1][p-1]) % mod; 49 } 50 printf ("%d ", dp1[nx][ny]); 51 } 52 return 0; 53 }