2022.7.16 练习题+优先队列

本文介绍了C++中的优先队列及其应用,通过实例展示了如何使用优先队列进行单调栈的操作。同时,文章还涉及了矩阵运算,如计算四阶行列式的值,以及字符串哈希的基础知识。此外,还提供了一个简单的计时问题解决方案。内容覆盖了数据结构、算法和基础数学知识,适合编程初学者和进阶者学习。

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

1.094172d3e57b4525a8444e77a53f1674.png

此题需要了解一个知识点———优先队列

    1.含义:优先队列同队列基本操作相同,但是队列内的元素是按照加入队列的顺序排序,但是优先队列可以按照单增、单减或自定义顺序排序。

    2.头文件 #include <queue>

    3.操作:同队列的插入(push)、删除(pop)、判断是否为空(empty())等操作相同。

    4.定义:

priority_queue<int,vector<int>,greater<int>> q;//队列内单调递增排序
priority_queue<int,vector<int>,less<int>> q;//队列内单调递减排序

以下为本题代码: 

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N=1e5+10;
priority_queue<int,vector<int>,greater<int>> p;
int n;
int main(){
	int ans=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		p.push(x);
	}
	while(1){
		int tmp=0;
		tmp+=p.top();
		p.pop();
		tmp+=p.top();
		p.pop();
		ans+=tmp;
		if(p.size()==0) break;
		p.push(tmp);
	}
	cout <<ans<<endl;
}

2.计算四阶行列式的值

用数学方法中的按行按列展开即可完成。

#include <iostream>
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		double a[4][4];
		int i,j,k;
		for(i=0;i<4;i++){
			for(j=0;j<4;j++) scanf("%lf",&a[i][j]);
		}//shu ru
		int x=1;
		if(a[0][0]==0){
			for(k=1;k<4;k++){
				if(a[0][k]!=0){
					for(i=0;i<4;i++){
						int t;
						t=a[i][0];
						a[i][0]=a[i][k];
						a[i][k]=t;
					}
					x=-1;
					break;
				}
			}
		}//bi mian fen mu wei ling       right
		double k0=a[0][0];
		for(i=0;i<4;i++) a[0][i]/=k0;
		double k1=a[1][0]/a[0][0];
		for(i=0;i<4;i++){
			a[1][i]-=k1*a[0][i];
		}//1,0 bian 0
		double k2=a[2][0]/a[0][0];
		for(i=0;i<4;i++){
			a[2][i]-=a[0][i]*k2;
		}//2,0 bian 0
		double k3=a[3][0]/a[0][0];
		for(i=0;i<4;i++){
			a[3][i]-=a[0][i]*k3;
		}//3,0 bian 0
		double m=a[1][1]*a[2][2]*a[3][3] + a[2][1]*a[3][2]*a[1][3] + a[1][2]*a[2][3]*a[3][1]-(a[1][3]*a[2][2]*a[3][1]+a[1][2]*a[2][1]*a[3][3]+a[1][1]*a[3][2]*a[2][3]);
		printf("%.0lf\n",k0*a[0][0]*m*x);
	}
}

 3.acae14935d404dfaa5c39c77d85a8d39.png

简单题 

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int t;
	cin>>t;
	int h=0,m=0,s=0;
	h=t/3600;
	m=t%3600/60;
	s=t%60;
	printf("%d:%d:%d",h,m,s);
}

4.单调栈2259c73d6f5b4312874a55d75ccb2023.png

 模板题复习

#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int stk[N],tt;
int main() {
	int n;
	cin>>n;
	int i,j;
	while(n--) {
		int x;
		cin>>x;
		if(tt==0) {
			stk[++tt]=x;
			cout <<"-1"<<" ";
		} else {
			while(stk[tt]>=x) {
				tt--;
			}
			if(tt==0) {
			    cout <<"-1"<<" ";
			    stk[++tt]=x;
			}
			else {
				cout <<stk[tt]<<" ";
				stk[++tt]=x;
			}
		}
	}
}

5.

字符串哈希----模板题(要多复习,不然会忘记细节)

#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const int N=1e5+10;
char a[N];
ull h[N],p[N];//p[i]代表131的i次方
ull num(int l,int r){
    return h[r]-h[l-1]*p[r-l+1];
}
int main(){
    int n,m;
    cin>>n>>m;
    cin>>a+1;
    int i;
    p[0]=1;
    for(i=1;i<=n;i++){
        h[i]=h[i-1]*131+a[i];//哈希后的数组
        p[i]=p[i-1]*131;
    }
    while(m--){
        int l1,r1,l2,r2;
        cin>>l1>>r1>>l2>>r2;
        if(num(l1,r1)==num(l2,r2)) cout <<"Yes"<<endl;
        else cout <<"No"<<endl;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值