堆排序的三种优先级设置方法——落谷P2085

142 篇文章 0 订阅
73 篇文章 0 订阅

堆排序时间复杂度为O(nlogn)与快速排序时间复杂度相同,且不会退化,快排会退化。
原题:https://www.luogu.org/problemnew/show/P2085
非堆排序解法:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j,cmin,jmin;
	int A[10010],B[10010],C[10010];
	int F[10010];
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		cin>>A[i]>>B[i]>>C[i];
		F[i]=1;
	}
	for(i=0;i<m;i++)
	{
		cmin=100000000;
		for(j=0;j<n;j++)
		{
			if(A[j]*F[j]*F[j]+B[j]*F[j]+C[j]<cmin)
			{
				cmin=A[j]*F[j]*F[j]+B[j]*F[j]+C[j];
				jmin=j;
			}
		}
		cout<<cmin<<' ';
		F[jmin]++;
	}
	return 0;
}

在这里插入图片描述
堆排序优化:
优先级设置方法(以小根堆为例)
1.比较级重载(最少)

priority_queue<value,vector<value>,less<value> >q; 
//只能是less,不能是greater,下一句是对less中“<"的重载 
bool operator <(value a,value b) {return a.val>b.val;}

2.结构体内部重载(operator必须有返回值类型)

struct value
{
    int num,x,val;
    friend bool operator <(value a,value b){
    	return a.val>b.val;
	} 
}res[100004];
priority_queue<value>q; 

3.结构体外部重载(operator必须有返回值类型)

struct cmp{//注意返回值的类型
	bool operator()(value a,value b){
		return a.val>b.val;
	}
};
//cmp的定义必须要在优先队列的上面
priority_queue<value,vector<value>,cmp>q; 
#include<bits/stdc++.h>
using namespace std;
int n,m;
struct func
{
    int a,b,c;
 } f[10004];
struct value
{
    int num,x,val;
}res[100004];//比较级重载 
priority_queue<value,vector<value>,less<value> >q; 
//只能是less,不能是greater,下一句是对less中“<"的重载 
bool operator <(value a,value b) {return a.val>b.val;}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&f[i].a ,&f[i].b ,&f[i].c );
        q.push((value){i,1,f[i].a +f[i].b +f[i].c});
    }
    for(int i=1;i<=m;i++){
        value t=q.top();
        q.pop();
        cout<<t.val <<" ";
        q.push((value){t.num ,t.x +1,f[t.num ].a *(t.x +1)*(t.x +1)+f[t.num ].b *(t.x +1)+f[t.num ].c } );
    }
    return 0;
}

#include<bits/stdc++.h>//结构体内部 
using namespace std;
int n,m;
struct func
{
    int a,b,c;
 } f[10004];
struct value
{
    int num,x,val;
    friend bool operator <(value a,value b){
    	return a.val>b.val;
	} 
}res[100004];
priority_queue<value>q; 
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&f[i].a ,&f[i].b ,&f[i].c );
        q.push((value){i,1,f[i].a +f[i].b +f[i].c});
    }
    for(int i=1;i<=m;i++){
        value t=q.top();
        q.pop();
        cout<<t.val <<" ";
        q.push((value){t.num ,t.x +1,f[t.num ].a *(t.x +1)*(t.x +1)+f[t.num ].b *(t.x +1)+f[t.num ].c } );
    }
    return 0;
}

#include<bits/stdc++.h>//结构体外部 
using namespace std;
int n,m;
struct func
{
    int a,b,c;
 } f[10004];
struct value
{
    int num,x,val;
}res[100004];
struct cmp{
	bool operator()(value a,value b){
		return a.val>b.val;
	}
};
priority_queue<value,vector<value>,cmp>q; 
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&f[i].a ,&f[i].b ,&f[i].c );
        q.push((value){i,1,f[i].a +f[i].b +f[i].c});
    }
    for(int i=1;i<=m;i++){
        value t=q.top();
        q.pop();
        cout<<t.val <<" ";
        q.push((value){t.num ,t.x +1,f[t.num ].a *(t.x +1)*(t.x +1)+f[t.num ].b *(t.x +1)+f[t.num ].c } );
    }
    return 0;
}
#include"stdio.h" char A[7]={'+','-','*','/','(',')','#'}; char B[7][7]={{'>','>','<','<','<','>','>'}, {'>','>','<','<','<','>','>'}, {'>','>','>','>','<','>','>'}, {'>','>','>','>','<','>','>'}, {'<','<','<','<','<','=','0'}, {'>','>','>','>','0','>','>'}, {'<','<','<','<','<','0','='}}; typedef struct stack1 { int top; int base; char s[20]; }stack1; typedef struct stack2 { int top; int base; int s[20]; }stack2; void Initstack1(stack1 *S) { S->top=S->base=0; } void Initstack2(stack2 *S) { S->top=S->base=0; } int push1(stack1 *S,char ch) { S->s[S->top]=ch; S->top++; } int push2(stack2 *S,int ch) { S->s[S->top]=ch; S->top++; } int search(char ch) { int i=0; while(ch!=A[i]) { i++; } return i; } char precede(char c1,char c2) { int i,j; i=search(c1); j=search(c2); return B[i][j]; } char gettop1(stack1 S) { char e; if(S.top==S.base) printf("!!!"); e=S.s[S.top-1]; return e; } int gettop2(stack2 S) { int e; if(S.top==S.base) printf("!!!"); e=S.s[S.top-1]; return e; } char pop1(stack1 *S) { if(S->top==S->base) return('!'); else { S->top--; return(S->s[S->top]); } } int pop2(stack2 *S) { if(S->top==S->base) return('!'); else { S->top--; return(S->s[S->top]); } } int operate(int a,char op,int b) { switch(op) { case '+':return(a+b);break; case '-':return(a-b);break; case '*':return(a*b);break; case '/':return(a/b);break; } } int main() { struct stack1 OPTR; struct stack2 OPND; char c,op; int a,b,an; Initstack1(&OPTR); push1(&OPTR,'#'); Initstack2(&OPND); c=getchar(); while(c!='#'||gettop1(OPTR)!='#') { if(c>='0'&&c<='9') { push2(&OPND,c-'0'); c=getchar(); do { if(c>='0'&&c<='9') push2(&OPND,pop2(&OPND)*10+(c-'0')); }while(c>='0'&&c<='9'); } else { switch(precede(gettop1(OPTR),c)) { case '<': push1(&OPTR,c);c=getchar();break; case '=': c=pop1(&OPTR);c=getchar();break; case '>': op=pop1(&OPTR);a=pop2(&OPND);b=pop2(&OPND); push2(&OPND,operate(b,op,a));break; }//switch } //else } //while an=pop2(&OPND); printf("\nyour answer is:\n=%d",an); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值