week 12作业 dp 问题

C

题目

东东每个学期都会去寝室接受扫楼的任务,并清点每个寝室的人数。
每个寝室里面有ai个人(1<=i<=n)。从第i到第j个宿舍一共有sum(i,j)=a[i]+…+a[j]个人
这让宿管阿姨非常开心,并且让东东扫楼m次,每一次数第i到第j个宿舍sum(i,j)
问题是要找到sum(i1, j1) + … + sum(im,jm)的最大值。且ix <= iy <=jx和ix <= jy <=jx的情况是不被允许的。也就是说m段都不能相交。
注:1 ≤ i ≤ n ≤ 1e6 , -32768 ≤ ai ≤ 32767 人数可以为负数。。。。(1<=n<=1000000)

输入

输入m,输入n。后面跟着输入n个ai 处理到 EOF

输出

输出最大和

样例

样例输入
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
样例输出
6
8

分析:

区间dp问题可以利用m段最大连续区间和的思路解决。其状态f[i][j]表示取第j个数,并且前j个数共分成i个区间的最大和,状态转移方程为:f[i][j]=max(f[i][j-1]+a[j], f[i-1][k]+a[j]) k从(i-1)到(j-1)。其中,f[i][j-1]+a[j]表示在取第j-1个数的基础上,加上第j个数,所以并没有增加新的区间。f[i-1][k]+a[j]表示已经有了i-1个区间,第j个数的加入产生了一个新的区间,所以根据这个思路,k应该从i-1到j-1取值。
根据f数组的定义,可以存在i==j的情况,因此会同时出现f[i][j-1]即:f[i][i-1]的情况,i-1个数却分出了i个区间,这显然不成立。因此将f[i][i-1]设置为极小值即可。
考虑空间的优化:第一层循环从1到m遍历i,在状态转移方程中,f[i][j-1]+a[j]并没有用到第i-1层的内容,而f[i-1][k]+a[j]可以再开一个变量,在i-1层更新的过程中,将其记录下来。

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define inf 1e9
int a[1000010];
int b[1000010];
int dp[1000010]; 
int main()
{
	int m,n;
	while(scanf("%d %d",&m,&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		int ans; 
		//f[i][j]表示前j个数成i个区间的最大和
		//f[i][j]=max(f[i][j-1]+a[j], f[i-1][k]+a[j]) 
		for(int i=1;i<=m;i++) //m次
		{
			ans=-inf;    //dp[i][j]    b =dp[i-1][j]
			for(int j=i;j<=n;j++)  // -1 4 -2 3 -2 3        
			{
				dp[j]=max(dp[j-1]+a[j],b[j-1]+a[j]);//合并 和另起一串
				b[j-1]=ans;
				ans=max(ans,dp[j]);   //  dp[m][j]
			} 
		} 
	
		printf("%d\n",ans);
	}
	return 0;
}

D

题目

We give the following inductive definition of a “regular brackets” sequence:

the empty sequence is a regular brackets sequence,
if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
if a and b are regular brackets sequences, then ab is a regular brackets sequence.
no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

输入

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

输出

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

样例

样例输入:
((()))
()()()
([]])
)[)(
([][][)
end
样例输出:
6
6
4
0
6

分析

区间dp 问题,定义状态f[i][j]为i-j 的符合条件的最大长度。大区间从小区间得来,因此首先枚举的是区间长度,然后从头开始枚举起点,因为长度已知,所以两端点都可以确定。然后如果恰好两端点的位置可以匹配,那么dp[i][j]=dp[i+1][j-1]+2,然后就需要考虑区间内部的问题,枚举k ,转移方程为f[i][j]=max(f[i][j],f[i][k]+f[k][j]);

代码

#include<stdio.h>
#include<string>
#include<stack>
#include<iostream> 
#include<string.h>
using namespace std;
// 括号序列
// f[i,j]    前i个元素,需要添加括号 j  (0,i)  
int dp[1010][1010]; 
int main()
{
	string s;
	while(cin>>s)
	{
		if(s=="end")	return 0;
		//满足条件 dp[i][j]=dp[i+1][j-1]+2;   //i j是最大的区间
		//对于区间[i,j]          dp[i,j]=max(dp[i,k]+dp[k,j]) 
		//较长的区间需要从较短的区间转移
		memset(dp,0,sizeof(dp));
		for(int len=1;len<s.size();len++)
		{
			for(int j=len;j<s.size();j++)
			{
				int i=j-len;
				if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
				{
					if(len==1) dp[i][j]=2; 
					dp[i][j]=dp[i+1][j-1]+2;
				}
				for(int k=i;k<j;k++)  // 内部 
				{
					dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
				}
			}
		} 
		printf("%d\n",dp[0][s.size()-1]);
	} 
	return 0;
}

E

题目

马上假期就要结束了,zjm还有 n 个作业,完成某个作业需要一定的时间,而且每个作业有一个截止时间,若超过截止时间,一天就要扣一分。
zjm想知道如何安排做作业,使得扣的分数最少。
Tips: 如果开始做某个作业,就必须把这个作业做完了,才能做下一个作业。

输入

有多组测试数据。第一行一个整数表示测试数据的组数
第一行一个整数 n(1<=n<=15)
接下来n行,每行一个字符串(长度不超过100) S 表示任务的名称和两个整数 D 和 C,分别表示任务的截止时间和完成任务需要的天数。
这 n 个任务是按照字符串的字典序从小到大给出。

输出

每组测试数据,输出最少扣的分数,并输出完成作业的方案,如果有多个方案,输出字典序最小的一个

样例

样例输入:
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
样例输出:
2
Computer
Math
English
3
Computer
English
Math

代码

#include<stdio.h>
#include<string> 
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100000 
#define N 20
string tname[N];
int d[N];
int c[N];
int pre[maxn];
int sum[maxn];
int f[maxn];
void print(int s)
{
	if(s==0)
		return;
	print(s^(1<<pre[s]));
	cout<<tname[pre[s]]<<endl;
}
int main()
{
	int m,n,nn;
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d",&n);
		memset(c,0,sizeof(c));
		memset(d,0,sizeof(d));
		for(int i=0;i<maxn;i++)
		{
			f[i]=1e9;pre[i]=-1;sum[i]=0;
		}
		for(int i=0;i<n;i++)
		{
			cin>>tname[i]>>d[i]>>c[i];	
		}
		nn=(1<<n)-1; 
		f[0]=0;  //初始状态 
		//输入数据完毕 
		for(int i=0;i<=nn;i++)  //总的状态数
		{
			for(int j=0;j<n;j++)
			{
				if(!(i&(1<<j)))  //没有当前作业 
				{
					int tmp=max(sum[i]+c[j]-d[j],0);//作业j被扣的分数
					int t=i|(1<<j);  //加上 j  
					if(f[t]>f[i]+tmp)  //加上j之后的比不加j扣的分数多
					{
						f[t]=min(f[t],f[i]+tmp);  //扣分 
						sum[t]=sum[i]+c[j];    //总时间 
						pre[t]=j;            //回溯 
					}
				}
			 } 
		}
		cout<<f[nn]<<endl;
		print(nn); 
	}
	return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值