洛谷题单心得-算法1-1【模拟与高精度】-P1786

P1786 帮贡排序 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

模拟排序题,类型:先排序一个数据,相同的话,再排序另一个数据,两种方法,第一种偷师的题解,推荐,第二种自己想的,比较复杂(第二种在第二道题)

sort函数用法     库文件algorithm       sort(a,a+n,cmp)

【a为数组,其实是指针,cmp为编写的比较函数,cmp内可用if来判断上一层数据相同情况下,再比较的数据】

附本题代码

#include<iostream>
#include<algorithm>
using namespace std;
string job[7]={"BangZhu","FuBangZhu","HuFa","ZhangLao","TangZhu","JingYing","BangZhong"};

int jobnum(string a){
	for(int i=0;i<7;i++){
		if(a==job[i]){
			return i;
		}
	}
}

struct person{
	string name,post;
	int ctb,level,index;//ctb:contribution    index:初始顺序
};
bool comparectb(person a,person b){
	if(a.ctb==b.ctb){
		return a.index<b.index;
	}
	return a.ctb>b.ctb;	
}
bool comparelevel(person a,person b){
	if(a.post==b.post){
		if(a.level==b.level){
			return a.index<b.index;
		}
		return a.level>b.level;
	}
	return jobnum(a.post)<jobnum(b.post);
}
person p[115];

int main(){
	int n;
	cin>>n;	
	for(int i=0;i<n;i++){
		cin>>p[i].name>>p[i].post>>p[i].ctb>>p[i].level;
		p[i].index=i;
	}
	sort(p+3,p+n,comparectb);
	//调整职位
	int pp=2;
		for(int i=3;i<=4;i++){p[i].post=job[pp];}pp++;
		for(int i=5;i<=8;i++){p[i].post=job[pp];}pp++;
		for(int i=9;i<=15;i++){p[i].post=job[pp];}pp++;
		for(int i=16;i<=40;i++){p[i].post=job[pp];}pp++;
		for(int i=41;i<n;i++){p[i].post=job[pp];}pp++;
	sort(p+3,p+n,comparelevel);		
	
	
	for(int i=0;i<n;i++){
		cout<<p[i].name<<" "<<p[i].post<<" "<<p[i].level<<endl;
	}
	return 0;
}

附cuboid排序(原创垃圾复杂恶心排序法)

#include<iostream>
#include<algorithm>
using namespace std;
struct cuboid {		//长方体
	int id;
	long long int x, y, z;
	long long int square, volume;
};
struct same{	//获取同样序列
	int length=0;
	int endpos=0;
};
bool comparevolume(cuboid x, cuboid y) {
	return x.volume > y.volume;
}
bool comparesquare(cuboid x, cuboid y) {
	return x.square < y.square;
}
bool compareid(cuboid x, cuboid y) {
	return x.id > y.id;
}
cuboid input() {
	cuboid temp;
	cin >> temp.id >> temp.x >> temp.y >> temp.z;
	temp.square = (temp.x * temp.y + temp.x * temp.z + temp.y * temp.z) * 2;
	temp.volume = temp.x * temp.y * temp.z;
	return temp;
}

int main() {
	int n;
	cin >> n;
	cuboid a[n];
	for (int i = 0; i < n; i++) {
		a[i] = input();
	}
	sort(a,a+n,comparevolume);//体积排序
	
	same b[n];//获取相同序列
	int j=0;
	for(int i=0;i<n;i++){
		if(a[i].volume==a[i+1].volume){
			b[j].length++;
		}
		if(a[i-1].volume==a[i].volume&&a[i].volume!=a[i+1].volume){
			b[j].endpos=i;
			j++;
		}		
	}	
	for(int i=0;i<j;i++){
		sort(a+b[i].endpos-b[i].length,a+b[i].endpos+1,comparesquare);
	}
	
	same c[n];
	j=0;
	for(int i=0;i<n;i++){
			if(a[i].square==a[i+1].square){
				c[j].length++;
			}
			if(a[i].square!=a[i+1].square&&a[i-1].square==a[i].square){
				c[j].endpos=i;
				j++;	
			}		
	}	
	for(int i=0;i<j;i++){			
		sort(a+c[i].endpos-c[i].length,a+c[i].endpos+1,compareid);
	}
	
	for(int i=0;i<n;i++){
		cout<<a[i].id<<" "<<a[i].x<<" "<<a[i].y<<" "<<a[i].z
		<<" "<<a[i].square<<" "<<a[i].volume
		<<endl;
	}
}

//6			3	
//5 1 1 1	2 4 3 2
//6 4 1 1	5 2 4 3
//4 2 1 1	6 3 4 2
//1 1 1 2	
//2 1 4 2	
//3 1 2 2

//8					
//5 1 1 1
//3 1 3 1
//1 2 2 4
//4 4 2 2
//2 1 4 2
//6 8 1 1
//7 2 2 2
//8 2 4 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值