程序设计思维与实践 Week12 作业

本文详细解析了程序设计思维课程的周作业,包括必做题A、B、C和选做题D、E。A题利用map解决出现次数超过一半的数;B题采用广搜解决三维空间的最短路径问题;C题涉及动态规划求最大和,避免相交区间;D题寻找最长可配对括号子序列;E题运用状压DP和最短路算法解决作业安排问题,以最小扣分和字典序最小的方案输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - 必做题 - 1

题目

  • 给出n个数,zjm想找出出现至少(n+1)/2次的数, 现在需要你帮忙找出这个数是多少?
  • 本题包含多组数据:
    每组数据包含两行。
    第一行一个数字N(1<=N<=999999) ,保证N为奇数。
    第二行为N个用空格隔开的整数。
    数据以EOF结束
  • 对于每一组数据,你需要输出你找到的唯一的数。
  • 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

分析

这道题目如果开1E7的数组,我的就会超内存,由此思考,题目上说有一个出现次数超过一半,肯定是比较集中,所以使用map来进行存储,出现过的次数加一,没出现过的设置为1,遍历map中找到出现超过一半的那个输出

代码

#include<iostream>
#include<map>
using namespace std;
const int nmax=1E7+10;
int n[nmax];
int main(){
	int num,tmp;
	map<int,int> mp;
	while(~scanf("%d",&num)){
		mp.clear();
		for(int i=0;i<num;++i){
			scanf("%d",&tmp);
			if(mp.find(tmp)==mp.end()){
				mp[tmp]=1;
			}
			else{
				mp[tmp]++;
			}
		}
		for(auto iter=mp.begin();iter!=mp.end();iter++){
			if(iter->second>=(num+1)/2){
				cout<<iter->first<<endl;
				continue;
			}
		}
	}
	return 0;
}

B - 必做题 - 2

题目

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

  • 输入第一行是一个数表示空间的数量。
    每个空间的描述的第一行为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

  • Sample Output
    Escaped in 11 minute(s).
    Trapped!

    分析

  • 这道题目是一道地图的路径搜索问题,寻找最短路,与之前的问题不同的是,这个是个三维的,但是解题思路还是一样,使用宽搜进行,最先找到的路径就是答案,使用一个结构体来保存每个状态的点和到这个点的步数就可以了

  • 输入的时候如果我们按照字符来读取,因为各种换行和换行,scanf(%c)很容易读到不应该读的,所以我们把每一行都视为一个字符串,读入再进行字符数组的赋值,可以节省很大的精力.
    (为什么这个编译器不支持push({a,b,c})这样的)/(ㄒoㄒ)/~~)

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int nmax=30+5;
const int inf=1E9;
struct triple{
	int a,b,c,step;
	triple(){}
	triple(int i,int j,int k,int l):a(i),b(j),c(k),step(l){};
};
char graph[nmax][nmax][nmax];
bool vis[nmax][nmax][nmax];//false 没过;
int level,row,col,s1,s2,s3,e1,e2,e3,res;
int myx[]={0,1,0,-1,0,0};
int myy[]={1,0,0,0,-1,0};
int myz[]={0,0,1,0,0,-1};
bool judge(int x,int y,int z){
	if(x>level||x<1||y>row||y<1||z>col||z<1||vis[x][y][z]||graph[x][y][z]=='#') return false;
	else return true;
}

int main(){
	char str[nmax];
	while(~scanf("%d%d%d",&level,&row,&col)){
		if(!level&&!row&&!col) break;
		for(int i=1;i<=level;++i){
			for(int k=1;k<=row;++k){
				scanf("%s",str+1);
				for(int l=1;l<=col;++l){
					vis[i][k][l]=false;
					//printf("%c",str[l]);
					graph[i][k][l]=str[l];
					if(str[l]=='S'){
						s1=i,s2=k,s3=l;
					}
				}
			}
		}
		/*
		for(int i=1;i<=level;++i){
			for(int j=1;j<=row;++j)
			{
				for(int k=1;k<=col;k++){
					printf("%c",graph[i][j][k]);
				}
				cout<<endl;
			}
			cout<<endl;
		}
		printf("%d,%d,%d\n",s1,s2,s3);
		*/
		//search.bfs
		
		queue<triple> que;
		bool flag=false;
		triple start(s1,s2,s3,0);
		que.push(start);
		vis[s1][s2][s3]=true;
		while(!que.empty()){
			triple t=que.front();que.pop();
			int x=t.a,y=t.b,z=t.c;
			for(int i=0;i<6;++i){
				int tmp1=x+myx[i],tmp2=y+myy[i],tmp3=z+myz[i];
				if(judge(tmp1,tmp2,tmp3)){
					if(graph[tmp1][tmp2][tmp3]=='E'){
						res=t.step;
						flag=true;
						break;
					}
					else{
						vis[tmp1][tmp2][tmp3]=true;
						triple tmmp(tmp1,tmp2,tmp3,t.step+1);
						que.push(tmmp);
					}
				}
			}
			if(flag) break;
		}
		if(flag) printf("Escaped in %d minute(s).\n",res+1);
		else cout<<"Trapped!"<<endl; 
	}
	return 0;
}

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)
输入m,输入n。后面跟着输入n个ai
输出最大和

  • Sample Input

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

  • Sample Output

    6
    8

  • Hint

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

    分析


    代码

//暂时没有

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

  • Sample Input

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

  • Sample Output

    6
    6
    4
    0
    6

分析

这道题是要求最长的能配对的字符串的长度,有三种配对

  • 空的是对的(没用)
  • s是对的则(s),[s]是对的
  • a,b是对的,ab是对的
  • 并且这里的是序列,就是不是连续的
    设计的f[i][j]数组是从i到j的最大的长度,最终答案是f[0]length-1
    由此我们就可以知道,一个合理的序列的长度至少为2(你要空的干嘛),并且一个序列的f[i][j]有两种来源,靠两边的堆出来,即f[i]=’(’&&f[j]=’)’||f[i]=’[&&f[j]=’]’,这样的f[i][j]=f[i+1][j-1]+2,还有可能是和出来的所以可能是dp[s][e]=max(dp[s][e],dp[s][k]+dp[k+1][e]);k从s到e遍历,在这些中取最大的就是这个区间上的最大长度了,这样我们就可以从最短的长度为2的序列求起,直到最大的区间
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
const int nmax=1E2+10;
int dp[nmax][nmax];
typedef long long LL;
int main(){
	string str;
	while(1){
		cin>>str;
		memset(dp,0,sizeof(dp));
		if(str=="end") break;
		int leng=str.length();
		int mylen=2;
		for(;mylen<=leng;mylen++){
			for(int s=0;s<leng&&s+mylen-1<leng;++s){
				int e=s+mylen-1;
				if((str[s]=='('&&str[e]==')')||(str[s]=='['&&str[e]==']'))
					dp[s][e]=dp[s+1][e-1]+2;
				for(int k=s;k<e;++k) dp[s][e]=max(dp[s][e],dp[s][k]+dp[k+1][e]);

			}
		}
		cout<<dp[0][leng-1]<<endl;
	}
	return 0;
}

E - 选做题 - 2

题目

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

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

  1. Sample Input

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

  2. Sample Output

    2
    Computer
    Math
    English
    3
    Computer
    English
    Math

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

题目分析

  • 前言
    状压 DP
    • DP 主要难点之一在于状态的表示
    • 尤其对于某些题目来说,状态尤其复杂,如果使用先前的多维数组
    来表示,将会导致数组维度非常大,可操作性非常低
    • 因此我们考虑使用一些编码技术,比如二进制编码,用一个数字来
    表示一个状态,实现状态压缩的目的
    • 在动态规划中,使用特定编码技术来压缩状态即为状压 DP

  • 能否贪心?
    • 作业每超过截止时间一天,需要扣一分,情况复杂,
    难以贪心
    • 数据范围非常小(1 ≤ 𝑛 ≤ 15) 考虑状态压缩(!)注意数据的大小很重要

  1. S 表示当前完成的作业集合,则s的二进制数的每一位表示每一个任务是否完成,如:假设3个任务,001表示第一个完成,011表示第一二个完成…
    这样我们就形成了一种图,因为每种状态都和多一个完成的状态相连,并且可以计算边权如001和011,101状态相连,这样我们就可以更新每个状态这是一个O(2^N)的算法,所以15可以使用
  2. 边权的计算:我们的边权是多扣的分数,由此思考,当我们从一个状态转到另一个状态的时候,原始状态有一个耗费的时间,新的任务有一个耗时和ddl,那么我们如果在ddl之前,那么扣分就是0,如果在ddl之后,扣的分就是原始状态的时间+耗时-ddl,这个就是边权,编程时可以用tmp=cost[i+1]+time[pos]-ddl[i+1];tt=max(tmp,0);来表示
  3. 在这些完成后,我选择使用spfa算法进行最短路的计算,回溯找路径也就是一样的,这个就是板子
  4. 我们在回溯找路径时,找到的是状态量,而不是选择的任务,所以需要转化,我使用异或前后的状态来找出在二进制的哪一位的任务完成,之后使用学长课上普及的函数__builtin_ffs(x),它可以返回末尾最后一个零的位置,从一开始编号,
    5函数说明

代码

#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int nmax=50000;//2^15
const int mmax=20;
const int inf=1E9;
string sub[mmax];
int num,n;
int ddl[mmax],cost[mmax];
int dp[nmax],pre[nmax],time[nmax];
typedef long long LL;
void spfa(int start){
	int inq[nmax];
	int end=(1<<n)-1;
	for(int i=0;i<=end;i++)inq[i]=0,dp[i]=inf;
	time[0]=0;
	queue<int >que;
	inq[start]=1;
	dp[start]=0;
	que.push(start);//renwu,time
	while(!que.empty()){
		int pos=que.front();que.pop();inq[pos]=0;
		//cout<<pos;
		for(int i=0;i<n;++i){
			if((pos&1<<i)==0){
				int next=pos|(1<<i);
				//cout<<next;
				time[next]=time[pos]+cost[i+1];
				int tmp=cost[i+1]+time[pos]-ddl[i+1];
				int tt=max(tmp,0);
				if(dp[pos]+tt<dp[next]) {
					dp[next]=dp[pos]+tt;
					pre[next]=pos;
					if(!inq[next]){
						que.push(next);
						inq[next]=1;
					}
				}
			}
		}
	}
}
int main(){
	scanf("%d",&num);
	while(num--){
		scanf("%d",&n);
		for(int i=1;i<=n;++i){
			cin>>sub[i]>>ddl[i]>>cost[i];
		}
		spfa(0);
		printf("%d\n",dp[(1<<n)-1]);
		vector<int> vec;
		int myall=(1<<n)-1;
		for(;myall;myall=pre[myall]){
			int x=myall^pre[myall];
			vec.push_back(__builtin_ffs(x));
		}
		for(auto it=vec.rbegin();it!=vec.rend();it++){
			cout<<sub[*it]<<endl;
		}
	}	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值