BestCoder Round #47 (ABC)

比赛链接:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=608

Senior's Array

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 888    Accepted Submission(s): 324

Problem Description
One day, Xuejiejie gets an array A . Among all non-empty intervals of A , she wants to find the most beautiful one. She defines the beauty as the sum of the interval. The beauty of the interval--- [L,R] is calculated by this formula : beauty(L,R) = A[L]+A[L+1]++A[R] . The most beautiful interval is the one with maximum beauty.
But as is known to all, Xuejiejie is used to pursuing perfection. She wants to get a more beautiful interval. So she asks Mini-Sun for help. Mini-Sun is a magician, but he is busy reviewing calculus. So he tells Xuejiejie that he can just help her change one value of the element of A to P . Xuejiejie plans to come to see him in tomorrow morning.
Unluckily, Xuejiejie oversleeps. Now up to you to help her make the decision which one should be changed(You must change one element).
 
Input
In the first line there is an integer T , indicates the number of test cases.
In each case, the first line contains two integers n and P . n means the number of elements of the array. P means the value Mini-Sun can change to.
The next line contains the original array.
1n1000 , 109A[i],P109
 
Output
For each test case, output one integer which means the most beautiful interval's beauty after your change.
 
Sample Input
  
  
2 3 5 1 -1 2 3 -2 1 -1 2
 
Sample Output
  
  
8 2
 
题目大意:给一组数和一个数,要求用这个数字更换数组中的某个数值必须且仅一次,问更换后的最大连续和

题目分析:数据不大,O(n^2)直接搞,枚举更换位置

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1005;
ll const INF = 1e16;
ll a[MAX];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, p;
        scanf("%d %d", &n, &p);
        for(int i = 1; i <= n; i++)
            scanf("%I64d", &a[i]);
        ll ans = -INF;
        for(int i = 1; i <= n; i++)
        {
            ll t = a[i];
            a[i] = p;
            ll cur = 0, ma = -INF;
            for(int j = 1; j <= n; j++)
            {
                cur += a[j];
                if(cur > ma)
                    ma = cur;
                if(cur < 0)
                    cur = 0;
            }
            a[i] = t;
            ans = max(ans, ma);
        }
        printf("%I64d\n", ans);
    }
}


Senior's Gun

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 859    Accepted Submission(s): 313


Problem Description
Xuejiejie is a beautiful and charming sharpshooter.
She often carries n guns, and every gun has an attack power a[i] .
One day, Xuejiejie goes outside and comes across m monsters, and every monster has a defensive power b[j] .
Xuejiejie can use the gun i to kill the monster j , which satisfies b[j]a[i] , and then she will get a[i]b[j] bonus .
Remember that every gun can be used to kill at most one monster, and obviously every monster can be killed at most once.
Xuejiejie wants to gain most of the bonus. It's no need for her to kill all monsters.
 

Input
In the first line there is an integer T , indicates the number of test cases.
In each case:
The first line contains two integers n , m .
The second line contains n integers, which means every gun's attack power.
The third line contains m integers, which mean every monster's defensive power.
1n,m100000 , 109a[i],b[j]109
 
Output
For each test case, output one integer which means the maximum of the bonus Xuejiejie could gain.
 
Sample Input
   
   
1 2 2 2 3 2 2
 

Sample Output
   
   
1
 
题目大意:n把枪,每把攻击力ai,m个怪,每个怪防御力bi,每把枪只能用一次,每用ai枪杀bi怪可得到ai-bi的利润,求最大利润

题目分析:显然我们要用攻击力最大的几把枪去杀掉防御力最低的几个怪,排个序模拟一下


#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int a[MAX], b[MAX];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int m, n;
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for(int i = 0; i < m; i++)
            scanf("%d", &b[i]);
        sort(a, a + n);
        sort(b, b + m);
        ll ans = 0;
        int j = n - 1;
        for(int i = 0; i < min(n, m) && j != -1; i++)
        {
            if(a[j] > b[i])
            {
                ans += a[j] - b[i];
                j --;
            }
            else 
                break;
        }
        printf("%I64d\n", ans);
    }
}


Senior's String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 355    Accepted Submission(s): 133


Problem Description
Xuejiejie loves strings most. In order to win the favor of her, a young man has two strings X , Y to Xuejiejie. Xuejiejie has never seen such beautiful strings! These days, she is very happy. But Xuejiejie is missish so much, in order to cover up her happiness, she asks the young man a question. In face of Xuejiejie, the young man is flustered. So he asks you for help.
The question is that :
Define the L as the length of the longest common subsequence of X and Y .( The subsequence does not need to be continuous
in the string, and a string of length L has 2L subsequences containing the empty string ). Now Xuejiejie comes up with all subsequences of length L of string X , she wants to know the number of subsequences which is also the subsequence of string Y .
 
Input
In the first line there is an integer T , indicates the number of test cases.
In each case:
The first line contains string X , a non-empty string consists of lowercase English letters.
The second line contains string Y , a non-empty string consists of lowercase English letters.
1|X|,|Y|1000 , |X| means the length of X .
 
Output
For each test case, output one integer which means the number of subsequences of length L of X which also is the subsequence of string Y modulo 109+7 .
 
Sample Input
   
   
2 a b aa ab
 
Sample Output
   
   
1 2
 
题目大意:给两个字符串x,y,从字符串x中取出所有LCS(x,y)长度的子串,问有多少个也是y的子串

题目分析:很好的一道题,二次dp,第一次求LCS,这个不多说,第二次记cnt[i][j]为到x的i位置和y的j位置时长度为dp[i][j]的公共子串的个数。因为是从x中选,那么就考虑x的第i个字符选不选
如果dp[i][j] == dp[i - 1][j]即长度相等,则不选cnt[i][j] += cnt[i][j - 1]

选的情况下,x的第i个字符必然和y的某个字符相等,那肯定选y中在j之前最后一个与之相等的
dp[i][j] == dp[i - 1][p - 1] + 1
cnt[i][j] += cnt[i - 1][p - 1]


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MOD = 1e9 + 7;
int const MAX = 1005;
char x[MAX], y[MAX];
int dp[MAX][MAX], cnt[MAX][MAX];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(cnt, 0, sizeof(cnt));
        scanf("%s %s", x + 1, y + 1);
        int l1 = strlen(x + 1);
        int l2 = strlen(y + 1);
        for(int i = 1; i <= l1; i++)
            for(int j = 1; j <= l2; j++)
                dp[i][j] = (x[i] == y[j] ? dp[i - 1][j - 1] + 1 : max(dp[i - 1][j], dp[i][j - 1]));
        cnt[0][0] = 1;
        for(int i = 1; i <= l1; i++)
            cnt[i][0] = 1;
        for(int j = 1; j <= l2; j++)
            cnt[0][j] = 1;
        for(int i = 1; i <= l1; i++)
        {
            int p = 0;
            for(int j = 1; j <= l2; j++)
            {
                if(x[i] == y[j])
                    p = j;
                if(dp[i][j] == dp[i - 1][j])
                    cnt[i][j] = (cnt[i][j] % MOD + cnt[i - 1][j] % MOD) % MOD;
                if(p && dp[i][j] == dp[i - 1][p - 1] + 1)
                    cnt[i][j] = (cnt[i][j] % MOD + cnt[i - 1][p - 1] % MOD) % MOD;
            }
        }
        printf("%d\n", cnt[l1][l2] % MOD);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值