跳到正文

个人记录

2026/3/21总结

cyz20121112约 5 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年3月28日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

第三题:

C++
#include<bits/stdc++.h>
using namespace std;
int n;
priority_queue<int, vector<int>, greater<int> > q;//小根堆
long long su;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		q.push(x);
	}
	while(q.size()>1){
		int a=q.top();//取最大的
		q.pop();
		int b=q.top();//取第二大的
    q.pop();
		su+=a+b;
		q.push(a+b);//再次入栈
	}
	cout<<su;
	return 0;
}

第四题:

C++
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
const int N=2e5+10;
priority_queue<PIII,vector<PIII>,greater<PIII> >q;
int n,a[N],is[N],ans1[N],ans2[N],nxt[N],pre[N];
int cnt=0;
string s;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>s;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<n;i++){
		if(s[i-1]!=s[i]){
			q.push({abs(a[i]-a[i+1]),{i,i+1}});
		}
	}
	for(int i=1;i<=n;i++){
		nxt[i]=i+1;
		pre[i]=i-1;
	}
	while(!q.empty()){
		PIII t=q.top();
		q.pop();
		int x=t.second.first,y=t.second.second;
		if(is[x]==0&&is[y]==0){
			nxt[pre[x]]=nxt[y];
			pre[nxt[y]]=pre[x];
			cnt++;
			ans1[cnt]=x,ans2[cnt]=y;
			is[x]=1,is[y]=1;
			if(is[pre[x]]==0&&is[nxt[y]]==0){
				if(s[pre[x]-1]+s[nxt[y]-1]==('G'+'B')){
					q.push({abs(a[pre[x]]-a[nxt[y]]),{pre[x],nxt[y]}});
				}
			}
		}
	}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++){
		cout<<ans1[i]<<" "<<ans2[i]<<endl;
	}
	return 0;
}

第五题:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e4 + 10;
typedef pair<int, int> PII;
PII temp[N], stations[N];
priority_queue<int> pq;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		int d, f;
		cin >> d >> f;
		temp[i] = {d, f};
	}
	int l, p;
	cin >> l >> p;
	// 转换成离起点的距离,并加入终点
	for(int i = 1; i <= n; i++){
		int d = temp[i].first, f = temp[i].second;
		stations[i] = {l - d, f}; // 转换成离起点的距离
	}
	stations[n + 1] = {l, 0}; // 终点虚拟站点
	sort(stations + 1, stations + n + 2); // 按离起点距离从小到大排序
	// pos:当前位置 you:剩余油量 ans:停车加油的次数
	int pos = 0, you = p, ans = 0;
	// 遍历每个站点,看能不能到达
	for(int i = 1; i <= n + 1; i++){
		PII s = stations[i];
		int dist = s.first - pos; // 要走的距离
		// 油不够,需要加油
		while(you < dist){
			if(pq.empty()) {// 没有可用的加油站了
				cout << -1 << endl;
				return 0;
			}
			you += pq.top(); // 取之前最大的加油站加油
			pq.pop(); // 用完了删掉
			ans++; // 停车次数+1
		}
		you -= dist; // 油量减少
		pos = s.first; // 当前位置更新
		pq.push(s.second); // 当前位置的加油站入队,留到后面用
	}
	cout << ans << endl;
	return 0;
}
原文