week12-作业-动态规划(三)

D-选做题-1

题目:

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 [([])].

Input:

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.

Output:

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

Sample Input:

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output:

6
6
4
0
6

题目分析:

该题要找出最长满足题意的括号长度,利用动态规划(区间DP)的思想。

dp[i][j]表示子序列[i,j]中合法序列的最大长度,从长度为2的区间开始遍历,找到合理的括号,即 (s[i]=’(’&&s[j]=’)’ || s[i]=’[’&&s[j]=’]’) 时,区间满足题意的长度加2, dp[i][j]=dp[i+1][j-1]+2 。对区间i-j,利用转移方程 dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]),更新区间内的dp值。最后输出dp[0][n-1]即为在整个区间内的括号长度最大值。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e2+5; 
int dp[maxn][maxn];

int main()
{
	string s;
	while(cin>>s)
	{
		if(s=="end")
		{
			return 0;	
		}
		memset(dp,0,sizeof(dp));
		int len=s.size();
		int cnt=2;//初始区间长度
		int j=0; 
		while(cnt<=len)
		{
			for(int i=0;i<len;i++)
			{
				j=i+cnt-1;
				if(j>=len)
				{
					break;
				}
				if(s[i]=='('&&s[j]==')' || s[i]=='['&&s[j]==']')
				{
					dp[i][j]=dp[i+1][j-1]+2;
					//cout<<dp[i][j]<<endl;
				}
				for(int k=i;k<j;k++)
	            {
	                dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
	                //cout<<dp[i][j]<<endl;
	            }
			}
			cnt++;	
		}
		cout<<dp[0][len-1]<<endl;	
	}
    return 0;
}

E-选做题-2

题目:

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

Input:

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

Output:

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

Sample Input:

2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3

Sample Output:

2
Computer
Math
English
3
Computer
English
Math

Hint:

在第二个样例中,按照 Computer->English->Math 和 Computer->Math->English 的顺序完成作业,所扣的分数都是 3,由于 English 的字典序比 Math 小,故输出前一种方案。

题目分析:

因为作业每超过截止时间一天,需要扣一分,情况复杂, 难以贪心且数据范围非常小(1 ≤ 𝑛 ≤ 15), 所以考虑状态压缩。 (采用二分的思想对应于每一种状态,然后对每种状态从小到大进行更新 , 用1表示该作业已完成,0表示该作业尚未完成)

dp[i] 表示完成 i 作业集合后被扣的最少分数 , pre[i] 表示对每个状态i记录其对应的最后一个作业。sum[i]作业i集合对应的总时间 。

  • 对于n门课,枚举每一个状态,从0到1<<n -1,从没有一个完成到所有都完成 , 对每种状态枚举每一门课完成的情况(从第1门到第n门)
  • 然后+当前课程所扣的分数(因为扣的分数不能为负,所以 int temp=max((sum[k]+a[j].day-a[j].ddl),0)
  • 用i&(1<<j)表示第j门是否在i中。找到最小值后,把它记录到路径当中。更新记录i状态时,最新完成的作业以及当前作业集合的总时间。

如何保证字典序最小 , 首先对作业按字典序排序 , 状态 S 越小则其对应的字典序越小 ,因此 S 从小到大枚举,x 从小到大枚举即可保证字典序最小 。(注意为了保证字典序输出(dp[i]>=dp[k]+temp)需要有等于号)

最后对每个状态 S 记录的 pre[S] (其对应的最后一个作业 )递归输出即可。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1<<15;
#define inf 0x3f3f3f3f;
struct job
{
	string name;
	int ddl;
	int day;
}a[16];
int dp[maxn];//表示完成s作业后被扣的最少分数 
int pre[maxn];//对每个状态S记录 pre[S] 表示其对应的最后一个作业 
int sum[maxn];// 作业集合对应的总时间
void output(int x)
{
	if(x==0)
	{
		return ;
	}
	output(x-(1<<pre[x]));
	cout<<a[pre[x]].name<<endl;
}
int main()
{
	int m;
	cin>>m;
	while(m--)
	{
		int n;
		cin>>n;
		memset(dp,0,sizeof(dp));
		memset(pre,0,sizeof(pre));
		memset(sum,0,sizeof(sum));
		for(int i=0;i<n;i++)
		{
			cin>>a[i].name>>a[i].ddl>>a[i].day;
		}
		for(int i=1;i<(1<<n);i++)//枚举每一个状态,从没有一个完成到所有都完成 
		{
			dp[i]=inf;
			for(int j=0;j<n;j++)//枚举只有一门课完成的情况(从第1门到第n门) 
			{
				if(i & (1<<j) )
				{
					int k=i-(1<<j);
					int temp=max((sum[k]+a[j].day-a[j].ddl),0);
					dp[i]=min(dp[i],dp[k]+temp);
					if(dp[i]>=dp[k]+temp)
					{
						pre[i]=j;
						sum[i]=sum[k]+a[j].day;
					}
				} 
			}
		}
		cout<<dp[(1<<n)-1]<<endl;
		output((1<<n)-1);
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值