• 数据结构与算法复习:第三十二弹


    1. 知识点总结

    耗时:2h45min

    得分:80/100

    知识点:英语阅读理解、结构体排序、众数、基于树的DFS

    题目难度知识点
    1056 Mice and Rice🎯🎯🎯😅主要是理解题意(排序/queue)
    1055 The World’s Richest🎯结构体排序
    1054 The Dominant Color🎯众数
    1053 Path of Equal Weight🎯基于树的DFS

    2. 分题题解

    2.1 1056 Mice and Rice

    骗分5/25

    难点主要在于理解题意

    • 首先每个人的排名是来自于当前轮分组个数+1,比如这一轮有11人,3个人一组,一共划分成4组,那么这一轮淘汰的同学名次就是第4+1=5名
    • 其次关于第二路的order其实是指定了对应的weight,这个超级坑,就是说,如果你第三行第i个数字是j那么对应的你的第i个老鼠信息应该是他的weight=w[j]index=j不需要重新排序
    • 最后输出的时候按照index排序

    最后……这个题目翻译成中文也蛮有歧义的……(个人感觉

    下面是参考柳神的代码把自己的给改了一下:

    #include
    using namespace std;
    //这题可能还是英文理解的问题,AC代码参考柳神
    int Np,Ng;
    struct Node{
    	int id;
    	int w;
    	int order;
    	int rank;
    }; 
    vector<Node>turn,nturn,ans;//当前轮,下一轮 ,答案 
    vector<int>w;
    bool cmp(Node a,Node b){
    	return a.order<b.order;
    }
    bool cmp1(Node a,Node b){
    	return a.w>b.w;
    }
    int rk=0;
    int main(){
    	scanf("%d%d",&Np,&Ng);
    	turn.resize(Np);
    	w.resize(Np);
    	for(int i=0;i<Np;i++){
    		scanf("%d",&w[i]);
    	} 
    	for(int i=0;i<Np;i++){
    		turn[i].id=i;
    		scanf("%d",&turn[i].order);
    		turn[i].w=w[turn[i].order];
    	} 
    	for(int i=1;;i++){
    		int len=turn.size();
    		rk=len/Ng;
    		if(len%Ng){
    			rk+=1;
    		}
    		if(len==1){
    			turn[0].rank=1;
    			ans.push_back(turn[0]);
    			break;
    		}
    		for(int j=0;j<len/Ng;j++){
    			if((j+1)*Ng<=len){
    				sort(turn.begin()+j*Ng,turn.begin()+(j+1)*Ng,cmp1);
    				nturn.push_back(turn[j*Ng]);
    				for(int k=1+j*Ng;k<(j+1)*Ng;k++){
    					turn[k].rank=rk+1;
    					ans.push_back(turn[k]);
    				}
    			}
    		}
    		if(len%Ng){
    			sort(turn.begin()+len/Ng*Ng,turn.end(),cmp1);
    			nturn.push_back(turn[len/Ng*Ng]);
    			for(int k=len/Ng*Ng+1;k<len/Ng*Ng+len%Ng;k++){
    				turn[k].rank=rk+1;
    				ans.push_back(turn[k]);
    			}
    		}
    		turn=nturn;
    		nturn.clear();
    	}
    	sort(ans.begin(),ans.end(),cmp);
    	for(int i=0;i<Np;i++){
    		if(i){
    			printf(" ");
    		}
    		printf("%d",ans[i].rank);
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    2.2 1055 The World’s Richest

    STL排序即可,没有技巧,全是感情~

    #include
    using namespace std;
    struct Node{
    	string name;
    	int age;
    	int nw;
    };
    int N,K,cnt,num,amin,amax;
    vector<Node>nodes;
    bool cmp(Node a,Node b){
    	if(a.nw!=b.nw){
    		return a.nw>b.nw;
    	}else if(a.age!=b.age){
    		return a.age<b.age;
    	}else{
    		return a.name<b.name;
    	}
    }
    int main(){
    	cin>>N>>K;
    	nodes.resize(N);
    	for(int i=0;i<N;i++){
    		cin>>nodes[i].name>>nodes[i].age>>nodes[i].nw;
    	}
    	sort(nodes.begin(),nodes.end(),cmp);
    	for(int i=1;i<=K;i++){
    		scanf("%d%d%d",&num,&amin,&amax);
    		cnt=0;
    		printf("Case #%d:\n",i);
    		for(int j=0;j<N;j++){
    			if(cnt==num){
    				break;
    			}
    			if(nodes[j].age<=amax&&nodes[j].age>=amin){
    				cnt++;
    				printf("%s %d %d\n",nodes[j].name.c_str(),nodes[j].age,nodes[j].nw);
    			}
    		}
    		if(cnt==0){
    			printf("None\n");
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    2.3 1054 The Dominant Color

    翻译题意其实就是找众数,且题意说众数唯一,那么就是先排序,然后计算每个连续最大的个数,求出众数:

    #include
    using namespace std;
    int N,M;
    typedef unsigned long long ull;
    vector<ull>v; 
    int max_cnt=0;
    int cnt;
    ull temp;
    ull ans=-1;
    int main(){
    	cin>>N>>M;
    	v.resize(N*M);
    	for(int i=0;i<N*M;i++){
    		cin>>v[i];
    	}
    	sort(v.begin(),v.end());
    	cnt=1;temp=v[0];
    	for(int i=1;i<N*M;i++){
    		if(v[i]==v[i-1]){
    			cnt++;
    		}else{
    			if(cnt>max_cnt){
    				max_cnt=cnt;
    				ans=temp;
    			}
    			temp=v[i];
    			cnt=1;
    		}
    	}
    	if(cnt>max_cnt){
    		ans=temp;
    	}
    	cout<<ans;
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    2.4 1053 Path of Equal Weight

    披着树的结构,本质上是DFS搜索所有可能的路径,最后按照数组的降序输出,需要注意的是,只有走到叶子结点的路径才算数,以及排序sort函数可以直接对vector排序

    #include
    using namespace std;
    typedef long long ll;
    struct Node{
    	ll w;
    	vector<int>childs;
    	int len=0;
    };
    int N,M,id,K;
    ll S;
    vector<Node>nodes(101);
    vector<vector<ll> >ans;
    vector<ll>temp;
    void dfs(int id,vector<ll>temp,ll sum){
    	sum+=nodes[id].w;
    	temp.push_back(nodes[id].w);
    	if(sum==S&&nodes[id].len==0){
    		ans.push_back(temp);
    	}else if(sum<S){
    		for(int i=0;i<nodes[id].len;i++){
    			dfs(nodes[id].childs[i],temp,sum);
    		}
    	}
    	temp.pop_back();
    }
    int main(){
    	scanf("%d %d %lld",&N,&M,&S);
    	for(int i=0;i<N;i++){
    		scanf("%lld",&nodes[i].w);
    	}
    	while(M--){
    		scanf("%d %d",&id,&K);
    		nodes[id].len=K;
    		nodes[id].childs.resize(K);
    		for(int i=0;i<K;i++){
    			scanf("%d",&nodes[id].childs[i]);
    		}
    	}
    	dfs(0,temp,0);
    	int len=ans.size();
    	sort(ans.begin(),ans.end());
    	for(int i=len-1;i>=0;i--){
    		for(int j=0;j<ans[i].size();j++){
    			if(j){
    				printf(" ");
    			}
    			printf("%lld",ans[i][j]);
    		}
    		printf("\n");
    	}
    	return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    3. 参考代码

    1056. Mice and Rice (25)-PAT甲级真题(queue的用法)_柳婼的博客-CSDN博客

  • 相关阅读:
    深入理解Spring中的Ioc
    VideoMAE复现之SSV2数据集解压(Linux)、webm转mp4格式
    Transformer在小目标检测上的应用
    操作系统实现-中断及任务调度
    Apache-tomcat-8.5.82下载安装以及环境变量配置
    化工DCS/SIS/MIS系统时钟同步(NTP服务器)建设
    【缓存】OS层面缓存设计机制
    2. 慢查询、索引、执行计划详解
    lerna在项目中使用
    2022牛客多校第一场补题
  • 原文地址:https://blog.csdn.net/qq_45751990/article/details/126589100