week12作业题

A必做题1

问题描述:

给出n个数,zjm想找出出现至少(n+1)/2次的数, 现在需要你帮忙找出这个数是多少?

Input

本题包含多组数据:
每组数据包含两行。
第一行一个数字N(1<=N<=999999) ,保证N为奇数。
第二行为N个用空格隔开的整数。
数据以EOF结束。

Output

对于每一组数据,你需要输出你找到的唯一的数。

Sample Input

5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample Output

3
5
1

思路:
直接判断每个数出现的次数,若是满足直接输出即可

#include<iostream>
#include<string.h>
using namespace std;
int n=0;
void cs()
{
	cin.clear();
//	cin.ignore();
	while(scanf("%d",&n)!=EOF)
	{
		int n1=(n+1)/2;
		int *A=new int[n+10],*B=new int[1000000];
		for(int i=0;i<n;i++)
		{
			cin>>A[i];
			//B[i]=0;
		}
		memset(B,0,sizeof(B));
		for(int i=0;i<n;i++)
		{
			B[A[i]]++;
			if(B[A[i]]>=n1)
			{
				cout<<A[i]<<endl;
				break;
			}
		}
	}	
}
int main()
{
	cs();
	return 0;
}

B必做题2

问题描述:
zjm被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成。
zjm每次向上下前后左右移动一个单位需要一分钟,且zjm不能对角线移动。
空间的四周封闭。zjm的目标是走到空间的出口。
是否存在逃出生天的可能性?如果存在,则需要多少时间?
Input

输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度,R和C分别表示每层空间的行与列的大小。
随后L层,每层R行,每行C个字符。
每个字符表示空间的一个单元。'#'表示不可通过单元,'.'表示空白单元。
zjm的起始位置在'S',出口为'E'。每层空间后都有一个空行。
L,R和C均为0时输入结束。

Output

每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。

如果无法逃生,则输出如下
Trapped!

Sample Input

3 4 5
S….
.###.
.##..
###.#

#####
#####
##.##
##…

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

思路:
这道题就是平时的多了一维,使用dfs,bfs都可以做出这道题

#include<iostream>
#include<queue>
using namespace std;
int L,R,C,A[40][40][40];
struct node
{
	int x,y,z;
	node(int w,int e,int r)
	{
		x=w;y=e;z=r;
	}
};
int up[]={0,0,0,0,1,-1};
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int main()
{
	char c;
	while(cin>>L>>R>>C&&!(L==0&&R==0&&C==0))
	{
		int begin0,begin1,begin2,end0,end1,end2;
		for(int i=1;i<=L;i++)
		{
			for(int k=1;k<=R;k++)
			{
				for(int j=1;j<=C;j++)
				{
					cin>>c;
					if(c=='S')
					{
						begin0=i;begin1=k;begin2=j;
						A[i][k][j]=0;
					}
					else if(c=='E'||c=='.')
					{
						A[i][k][j]=-1;
						if(c=='E')
						{
							end0=i;end1=k;end2=j;
						}
					}
					else if(c=='#')A[i][k][j]=-2;
				}
			}
		}
		queue<node> q;
		q.push({begin2,begin1,begin0});
		while(!q.empty())
		{
			node now=q.front();
			q.pop();
			if(now.x==end2&&now.y==end1&&now.z==end0)
			{
				break;
			}
			int ff=A[now.z][now.y][now.x];
			for(int i=0;i<6;i++)
			{
				if(A[now.z+up[i]][now.y+dy[i]][now.x+dx[i]]==-1&&now.x+dx[i]<=C&&now.x+dx[i]>=1&&now.y+dy[i]<=R&&now.y+dy[i]>=1&&now.z+up[i]<=L&&now.z+up[i]>=1)
				{
					q.push({now.x+dx[i],now.y+dy[i],now.z+up[i]});
					A[now.z+up[i]][now.y+dy[i]][now.x+dx[i]]=ff+1;
				}
			}
		}
		if(A[end0][end1][end2]!=-1)cout<<"Escaped in "<<A[end0][end1][end2]<<" minute(s)."<<endl;
		else cout<<"Trapped!"<<endl;
	}
}

C必做题3

问题描述:

东东每个学期都会去寝室接受扫楼的任务,并清点每个寝室的人数。
每个寝室里面有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) 

Input
输入m,输入n。后面跟着输入n个ai 处理到 EOF
Output
输出最大和
Sample Input

1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Output

6
8 

Hint

数据量很大,需要scanf读入和dp处理。

思路:
首先要思考出状态转移方程:
dp[i][j]=max{dp[i][j-1]+A[j],dp[i-1][k]+A[j]}
dp表示清洁i次时,清洁到j的最大人数,所以第j个就是dii段的延伸,但是为优化我们也可以把状态转移方程优化为如下;
B[k]=max(B[k-1]+A[k],C[k-1]+A[k]);

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<limits>
using namespace std;
int A[1000010],B[1000010],C[1000010];
int main()
{
	int n,m;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(int i=1;i<=n;i++)scanf("%d",&A[i]);
		B[0]=0;
		memset(C,0,sizeof(C));
		int vis=std::numeric_limits<int>::min();
	//	cout<<vis<<endl;
		for(int i=1;i<=m;i++)
		{
			vis=std::numeric_limits<int>::min();
			for(int k=i;k<=n;k++)
			{
				B[k]=max(B[k-1]+A[k],C[k-1]+A[k]);
				C[k-1]=vis;
				vis=max(vis,B[k]);
			}
		}
		printf("%d\n",vis);
	}
}

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[i][j]也就是区间[i,j]变成合法括号序列所需添加的最少括号的数量
i,j若是匹配的括号对则dp[i][ans]=dp[i+1][ans-1]+2;
dp[i][ans]=max(dp[i][ans],dp[i][j]+dp[j][ans]);

#include<iostream>
#include<string.h>
using namespace std;
int dp[110][110];
string s;
int main()
{
	while(cin>>s)
	{
		memset(dp,0,sizeof(dp));
		if(s=="end")break;
		int n=s.length();
		int ans=0;
		for(int k=2;k<=n;k++)
		{
			for(int i=0;i<n;i++)
			{
				ans=i+k-1;
				if(ans>=n)break;
				if(s[i]=='('&&s[ans]==')'||s[i]=='['&&s[ans]==']')
				{
					dp[i][ans]=dp[i+1][ans-1]+2;
				}
				for(int j=i;j<ans;j++)
				{
					dp[i][ans]=max(dp[i][ans],dp[i][j]+dp[j][ans]);
				}
			}
		}
		cout<<dp[0][n-1]<<endl;
	}
}

E:选做题2

问题描述:
Description
马上假期就要结束了,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 小,故输出前一种方案。

思路:
这道题结合了位运算以及Dp
状态转移方程:
int tmp=sum[i1-x]+A[k].b-A[k].a;
tmp=max(0,tmp);

f[]代表写了多少作业是要扣掉的最少的分数,sum[]则是写了多少作业的时候用掉的时间,pre是用来回溯的。

#include<iostream>
#include<string.h>
#include<limits>
using namespace std;
int n,f[1<<16],pre[1<<16],sum[1<<16],n1;
struct node
{
	string s;
	int a;
	int b;
};
node A[20];
void cs(int x)
{
	if(x==0)return;
	else 
	{
		cs(x-(1<<pre[x]));
		cout<<A[pre[x]].s<<endl;
	}
}
int main()
{
	cin>>n1;
	for(int i=0;i<n1;i++)
	{
		cin>>n;
		memset(f,0,sizeof(f));memset(pre,0,sizeof(pre));memset(sum,0,sizeof(sum));
		for(int i=0;i<n;i++)
		{
			cin>>A[i].s>>A[i].a>>A[i].b;
		}
		int ff=1<<n;
		for(int i1=1;i1<=ff;i1++)
		{
			f[i1]=std::numeric_limits<int>::max();
			for(int k=n-1;k>=0;k--)
			{
				int x=1<<k;
				if(i1&x)
				{
					int tmp=sum[i1-x]+A[k].b-A[k].a;
					tmp=max(0,tmp);
					if(f[i1]>f[i1-x]+tmp)
					{
						sum[i1]=sum[i1-x]+A[k].b;
						f[i1]=f[i1-x]+tmp;
						pre[i1]=k;
					}
				}
			}
		}
		cout<<f[ff-1]<<endl;
		cs(ff-1);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值