4.5总结

1.

Given a set of n integers: A={a1, a2,…, an}, we define a function d(A) as below:
Your task is to calculate d(A).
在这里插入图片描述

input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, …, an. (|ai| <= 10000).There is an empty line after each case.

output

Print exactly one line for each test case. The line should contain the integer d(A).

sample input

1
10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

题意:就是求两个不相交的最大子段和
超时了很多次。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 50010
int a[maxn],left1[maxn],right1[maxn];
int main()
{
	int t,n,i;
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;++i)
			scanf("%d",&a[i]);
		left1[0]=a[0];
		for(i=1;i<n;++i)
		{
			if(left1[i-1]<0)
				left1[i]=a[i];
			else
				left1[i]=left1[i-1]+a[i];
		}
		for(i=1;i<n;++i)
			left1[i]=max(left1[i],left1[i-1]);
		right1[n-1]=a[n-1];
		for(i=n-2;i>=0;--i)
		{
			if(right1[i+1]<0)
				right1[i]=a[i];
			else
				right1[i]=right1[i+1]+a[i];
		}
		for(i=n-2;i>=0;--i)
			right1[i]=max(right1[i],right1[i+1]);
		int ans=-10000*maxn;
		for(i=1;i<n;++i)
			ans=max(ans,left1[i-1]+right1[i]);
		printf("%d\n",ans);
	}
	return 0;
}

2.

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0…N-1) so that she produces as much milk as possible.
Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.
Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.

input

  • Line 1: Three space-separated integers: N, M, and R
  • Lines 2…M+1: Line i+1 describes FJ’s ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi

output

  • Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

sample input

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31

Sample Output

43

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=1001;
const int inf = 1e9;
int n,m,r;
struct node
{
    int s,e,w;
    bool operator < (const node &a)const
    {
        if(e != a.e)
            return e < a.e;
        else
            return s < a.s;
    }
} p[MAXN];
int dp[MAXN];
int main()
{
    cin>>n>>m>>r;
    for(int i = 0 ; i < m ; i++)
        cin>>p[i].s>>p[i].e>>p[i].w;
    sort(p,p+m);
    for(int i = 0 ; i < m ; ++i)
    {
        dp[i] = p[i].w;
        for(int j = 0 ; j < i ; j++)
            if(p[i].s - p[j].e >= r)
                dp[i] = max(dp[i],dp[j] + p[i].w);
    }
    int ans = 0 ;
    for(int i = 0 ; i < m ; ++i)
        ans = max(ans, dp[i]);
    cout<<ans<<endl;
    return 0;
}

3.

当今国际反恐形势很严峻,特别是美国“9.11事件”以后,国际恐怖势力更是有恃无恐,制造了多起骇人听闻的恐怖事件。基于此,各国都十分担心恐怖势力会对本国社会造成的不稳定,于是纷纷在本国的军队、警察队伍中开展了反恐训练。作为反恐立场坚定的大国,中国也十分重视在人民解放军、武装警察部队、人民警察队伍中反恐训练,还专门成立了反恐特警队。

炜炜是反恐特警队的一名新队员,现在正在接受培训。这几天刚好是射击训练第二阶段——实弹应变训练的日子,此前的第一阶段里,炜炜经过努力,已经将自己训练成为一个百发百中的神抢手了!这次,他将背着国产最新型12.7mm重型狙击枪进行训练比赛。

这次训练比赛的规则是这样的:

1、每个队员从出发点开始,沿着一条唯一的笔直道路跑直到终点,途中不允许往回跑,否则将被取消比赛资格。
2、出发前,每个队员的枪膛内都被装了顺序一样的、用小写英文字母标明类型的子弹序列,每位队员被告知这一序列的信息;同时,每位队员也被告知恐怖分子即将出现的序列和类型(同样用小写英文字母标明类型)。
3、在跑动的过程中,若发现“恐怖分子”,特警队员可以选择用枪击毙他,来得到写在“恐怖分子”胸前的得分,但是前提是他使用的子弹类型必须和“恐怖分子”类型相同,否则,即使击毙了“恐怖分子”,也得不到分数;当然选择不击毙他也是可以的,这样他不会从那个“恐怖分子”身上得到分数。
4、允许特警队员放空枪,这样可以消耗掉型号不对的子弹而不至于杀死“恐怖分子”(当然每个特警队员都不会愚蠢到不装消音装置就放空枪,以至于吓跑“恐怖分子”),等待枪口出现正确型号的子弹击毙他得分。

这里,我们假定: 1、对于每个队员,途中出现恐怖分子的地点、时间、类型也是完全一样的。 2、每颗子弹都是质量合格的,都可以发挥杀伤效力
3、由于队员各个都是神枪手,一旦他选择了正确的子弹,向目标射击,目标100%被爆头
4、每个队员的记忆力超强,能记住所有子弹序列信息和恐怖分子序列信息。 5、每个队员体力足够好,能跑完全程,并做他想要做的
6、“恐怖分子”是不动的,小范围内不存在多于一个的恐怖分子;

炜炜需要你的帮助,告诉他如何做,才能得到最高的分数。现在如果告诉你出发时枪膛内子弹的序号和型号、恐怖分子出现的序号和类型,你能告诉炜炜他最多能得到多少分数吗?

input

输入数据的第一行有一个整数N表示子弹和恐怖分子的类型数。随后的一行是各种恐怖分子类型的一行字母,两个字母之间没有任何字符。接下来的一行是击毙上一行对应位置恐怖分子类型的得分数,每个分数之间恰有一个空格。第三第四行分别表示开始时枪膛内子弹的序列(左边的先打出)和恐怖分子出现的序列(左边的先出现),字母之间都没有任何字符。
每个测试数据之间没有空格和空行。你的程序必须通过全部测试数据,才能被判为AC。

output

对于每一个测试数据,输出炜炜最多能得到的分数。

sample input

3
abc
1 1 1
abc
ccc
3
abc
1 1 1
ccc
aba

sample output

1
0

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 10002
using namespace std;
char a[MAX];
char b[MAX];
int s[MAX];
int n;
int dp[10][MAX];
int max(int a,int b)
{
    return a>b?a:b;
}
int main()
{
    int l1,l2,maxn;
    while(cin>>n&&n)
    {
        getchar();
        cin>>a;
        l1=strlen(a);
        memset(s,0,sizeof(s));
        for(int i=0; i<l1; i++)
            cin>>s[a[i]-'a'];
        getchar();
        scanf("%s",a+1);
        scanf("%s",b+1);
        l1=strlen(a+1);
        l2=strlen(b+1);
        memset(dp,0,sizeof(dp));
        maxn=0;
        for(int i=1; i<=l1; i++)
            for(int j=1; j<=l2; j++)
            {
                if(a[i]==b[j])
                    dp[i%10][j]=dp[(i-1+10)%10][j-1]+s[a[i]-'a'];
                else
                    dp[i%10][j]=max(dp[(i-1+10)%10][j],dp[i%10][j-1]);
                maxn=max(dp[i%10][j],maxn);
            }
        cout<<maxn<<endl;
    }
    return 0;
}

4.

Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible.
A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time.
Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full of appreciation for your help.

input

There are N(1<=N<=10) different scenarios, each scenario consists of 3 lines:1) An integer K(1<=K<=2000) representing the total number of people;
2) K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person;
3) (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together.

output

For every scenario, please tell Joe at what time could he go back home as early as possible. Every day Joe started his work at 08:00:00 am. The format of time is HH:MM:SS am|pm.

sample input

2
2
20 25
40
1
8

sample output

08:00:40 am
08:00:08 am

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int maxn = 2000 + 50;
int n,k;
int dp[maxn],a[maxn],b[maxn];
int h,m,s;
int main()
{
    cin>>n;
    while( n-- )
    {
        memset(dp, 0, sizeof(dp));
        cin>>k;
        for(int i=1; i<=k; i++)
            cin>>a[i];
        for(int i=2; i<=k; i++)
            cin>>b[i];
        dp[0] = 0;
        dp[1] = a[1];
        for(int i=2; i<=k; i++)
            dp[i] = min( dp[i-1]+a[i], dp[i-2]+b[i] );
        h = dp[k] / 3600 + 8;
        m = dp[k] / 60 % 60;
        s = dp[k] % 60;
        printf("%02d:%02d:%02d am\n", h, m, s);
    }
    return 0;
}

最近的dp学习成果很差,看见dp的题还是怕,不敢思考,同学们的实力都在不断提高,我却没能成功的跟上学习。应该改变一下自己了,学习实在是太怠惰了,荒废了一周又一周。真的应该认真起来学点东西了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值