2026.3.21

· · 个人记录

链接

priority_queue<int> pq;//大根堆
priority_queue<int, vector<int>, greater<int> > pq;//小根堆

:::::info[P3378 【模板】堆]

::::info[题目描述]{open}

题目描述

给定一个数列,初始为空,请支持下面三种操作:

  1. 给定一个整数 x,请将 x 加入到数列中。
  2. 输出数列中最小的数。
  3. 删除数列中最小的数(如果有多个数最小,只删除 1 个)。

说明/提示

【数据规模与约定】

思路:

这一题的正解为小根堆。

::::success[正确代码✔️]

:::error[禁止不思考就看题解!!!]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
    int T;
    cin>>T;
    while(T--){
        int op,x;
        cin>>op;
        if(op==1){
            cin>>x;
            q.push(x);
        }else if(op==2) cout<<q.top()<<'\n';
        else if(op==3) q.pop(); 
    } 
    return 0;
}
//localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'

:::

::::

:::::

:::::info[P3887 [GDOI2014] 世界杯]

::::info[题目描述]{open}

如,3-5-2 阵型是指有三个后卫、五个中场及两名前锋

对于每个阵型,从仍未选择的球员中选择最好的对应数量的守门员、后卫、中场和前锋。比如说,对于第一阵型,在所有球员中选择;对于第二阵型,在除了第一阵型外的所有球员中选择;对于第三阵型,在除了第一阵型和第二阵型外的所有球员中选择;以此类推。

::::warning[思路]

// 四个位置的⼤根堆
priority_queue<int> pq_k, pq_d, pq_m, pq_f;
while(Q--){
    cin>>A>>B>>C;
    // 取1个守⻔员
    // 取A个后卫
    // 取B个中场
    // 取C个前锋
    平均值 = 这⼀轮取出来的能⼒值之和 / 11.0
}

::::

::::success[正确代码✔️]

:::error[禁止不思考就看题解!!!]

#include <bits/stdc++.h>
using namespace std;
int a[100005],b[100005],c[100005],d[100005];
int k,g,f,m,q;
double ans;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    cin>>k>>g>>f>>m;
    for(int i=1;i<=k;i++)
       cin>>a[i];
    for(int i=1;i<=g;i++)
       cin>>b[i];
    for(int i=1;i<=f;i++)
       cin>>c[i];
    for(int i=1;i<=m;i++) 
       cin>>d[i];
    sort(a+1,a+k+1,cmp);
    sort(b+1,b+g+1,cmp);
    sort(c+1,c+f+1,cmp);
    sort(d+1,d+m+1,cmp);
    cin>>q;
    int l,z,y;
    int x=1,r=1,w=1,h=1;
    int s;
    while(q--){
        cin>>l>>z>>y;
        ans=0;
        ans+=a[x];
        x++;
        for(int i=r;i<r+l;i++)
           ans+=b[i];
        r+=l;
        for(int i=w;i<w+z;i++)
           ans+=c[i];
        w+=z;
        for(int i=h;i<h+y;i++)
           ans+=d[i];
        h+=y;
        printf("%.2f\n",ans/11);
    }
    return 0;
}

:::

::::

:::::

:::::info[P1334 瑞瑞的木板]

::::warning[思路]{open}

伪代码:

priority_queue<int, vector<int>, greater<int>> heap; // ⼩根堆存⽊板⻓度
ll ans = 0;
//输⼊n
//输⼊n个l_i,逐个加⼊heap
while heap.size() > 1:
    a = heap.top(); heap.pop(); // 取最⼩的
    b = heap.top(); heap.pop(); // 取次⼩的
    sum = a + b;
    ans += sum; // 累加合并代价
    heap.push(sum); // 合并后的⽊板放回堆
//输出ans

::::

::::success[正确代码✔️]

:::error[禁止不思考就看题解!!!]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue<ll,vector<ll>,greater<ll> > q;
int main(){
    long long ans=0,n;
    cin>>n;
    for(int i=1,x;i<=n;i++) cin>>x,q.push(x);
    for(int i=1,c,d;i<=n-1;i++){
        c=q.top();
        q.pop();
        d=q.top();
        q.pop();
        ans+=c+d;
        q.push(c+d);
    }
    cout<<ans;
    return 0;
}

/*
system("cls");
Sleep(1000);
*/

:::

::::

:::::

:::::info[P1878 舞蹈课]

::::warning[思路]{open}

伪代码:

⼩根堆 q(存{差值diff, 左位置x, 右位置y})
数组 is(标记是否已配对)、pre/nxt(双向链表)、ans(存答案对)
cnt = 0
输⼊n、性别串s、技术值a[1..n]
// 初始相邻异性对⼊队
// 初始化双向链表
// 处理配对
while q⾮空:
    取出队⾸{diff, x, y}
    if x和y都未配对:
        // 从链表删除x、y
        nxt[pre[x]] = nxt[y]
        pre[nxt[y]] = pre[x]
        // 记录答案
        cnt++
        ans[cnt] = {x, y}
        标记x、y已配对
        // 新相邻的左右若为异性,⼊队
        l = pre[x], r = nxt[y]
        if l和r都未配对 且 s[l-1] != s[r-1]:
            q.push({|a[l]-a[r]|, l, r})
输出cnt
逐⾏输出ans[1..cnt]

::::

::::success[正确代码✔️]

:::error[禁止不思考就看题解!!!]

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
struct B{
    int diff,l,r;
    bool operator>(const B& other)const{
        if(this->diff != other.diff){
            return this->diff > other.diff;
        }
        return this->l > other.l;
    }
    B(int d, int l, int r):diff(d), l(l), r(r){}
};
#ifdef DEBUG
const int maxn = 100;
#else
const int maxn = 2e5+10;
#endif
int a[maxn];
char g[maxn];
int is_visited[maxn];
int k;
int ls[maxn], rs[maxn];

int pre[maxn], nex[maxn];

int main(){
    int n;
    scanf("%d",&n);
    scanf("%s",g+1);
    for (int i=1;i<=n;i++){
        scanf("%d",a+i);
        pre[i]=i-1;
        nex[i]=i+1;
    }
    pre[1]=1;
    nex[n]=n;
    std::priority_queue<B, std::vector<B>, std::greater<B> > q;
    for (int i=1;i<=n-1;i++)
        if(g[i]!=g[i+1])
            q.push(B(abs(a[i]-a[i+1]), i, i+1));
    while(!q.empty()){
        B b=q.top();
        q.pop();
        int l=b.l,r=b.r;
        if(is_visited[l]==false && is_visited[r]==false){
            ls[k]=l;
            rs[k]=r;
            k++;
            is_visited[l]=is_visited[r]=1;
            int ll=pre[l],rr=nex[r];
            if(ll==0||rr==0)continue;          
            pre[rr]=ll;
            nex[ll]=rr;
            if(g[ll]!=g[rr]) q.push(B(abs(a[ll]-a[rr]),ll,rr));
        }
    }
    cout<<k<<endl;
    for (int i=0; i<k; i++)
        cout<<ls[i]<<" "<<rs[i]<<endl;
    return 0;
}

:::

::::

:::::

:::::info[T735985 书架]

::::warning[思路]{open}

伪代码:

结构体数组 stations; // 存(离起点距离, 油量)
priority_queue<int> pq; // ⼤根堆,存经过的油站油量
输⼊n
输⼊n个加油站的d(离城市)和f
输⼊l和p //离城 l 个单位, 还有 p 个单位的油
将暂存的加油站转换成(l - d, f),加⼊stations // l-d是 起点到加油站的距离
将终点(l, 0)加⼊stations
对stations按离起点距离从⼩到⼤排序
pos=0; // 记录卡⻋当前所在的位置
you=p // 记录卡⻋当前油箱⾥剩余的油量
ans=0 // 记录卡⻋已经加油的次数
遍历每个站点 in stations:
    dist = 站点距离 - pos // 当前位置开到下⼀个站点要⾛的路程
    while(剩余油不够)
        如果 队列空: 输出-1,return 0;
        you += pq.top(); pq.pop();
         加油次数+1
    you -= dist
    pos = 站点距离
    if 是终点: break
    pq.push(站点油量)
输出ans

::::

::::success[正确代码✔️]

:::error[禁止不思考就看题解!!!]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#define lp (p*2)
#define rp (p*2+1)
#define mid ((l+r)/2)
#define int long long
#define N 10010
using namespace std;
int n,l,p,sum,tim=1;
int vis[N];
struct oil
{
    int local,hav;//local是加油站距城市的距离,hav就是它能提供的油量
}a[N];
priority_queue<int>q;//利用优先队列去存它的最大能加的油量 
bool cmpx(const oil a,const oil b)//定义sort让a[]从大到小排列 
{
    return a.local>b.local;
}
int re() 
{
    int x=0,p=1;
    char y=getchar();
    for(;y<'0'||y>'9';y=getchar())
        if(y=='-')
            p=-p;
    for(;y>='0'&&y<='9';y=getchar())
        x=x*10+y-'0';
    return x*p;
}
void wr(int x) 
{
    if(x<0)
        x=-x,putchar('-');
    if(x>9)
        wr(x/10);
    putchar(x%10+'0');
}
signed main()
{
    n=re();
    for(int i=1;i<=n;i++)
        a[i].local=re(),a[i].hav=re();
    l=re(),p=re();
    sort(a+1,a+n+1,cmpx);
    while(1)
    {
        l-=p;
        if(l<=0)
            break;

        for(int i=tim;i<=n;i++)
            if(a[i].local>=l&&a[i].local<=l+p)
                    q.push(a[i].hav);
            else {
                tim=i;
                break;
            }   
        p=q.top();
        q.pop();
        sum++;
        if(q.empty()&&a[tim].local<(l-p))
            wr(-1),exit(0);
    }
    wr(sum);
}

:::

::::

:::::

P14330 [JOI2021 预选赛 R2] 往返滑道 / Round Sugoroku

题目描述

一滑道由 N+2 个格子横向排列组成。这些格子从左至右依次编号为 0 到 N+1 。初始时,格子 0 和格子 N+1 上写有字母 X,而格子 i ( 1 \le i \le N )上写有字符 S_i 。其中, S_i 为一个符号 . 或符号 #。

初始时,棋子位于格子 A ( 1 \le A \le N ),且朝向右方。注意, S_A 一定是符号 .。每隔 1 秒钟,将棋子向其当前朝向的方向移动 1 格。

规则:

棋子反转方向或字符变更所耗费的时间忽略不计。

当给定滑道与棋子的初始状态时,请编写程序,计算所有写有 # 的格子均变为 . 所需的时间。

输入格式

第一行:N,A
第二行:S

其中, S 是一个长度为 N 的字符串,其第 i 个字符( 1 \le i \le N )为 S_i 。

输出格式

输出一行:从开始到所有写有 # 的格子均变为 . 所需的秒数。

数据范围

正确思路:

这一题就是*~~超级无敌疯狂究极牛classporMAX~~**大模拟。

正确代码:

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
int main() {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,a,num,cnt=1;
    ll ans=0;
    string s;
    cin>>n>>a;
    num=a;
    cin>>s;
    set<int> se;
    for(int i=0;i<n;++i)
        if(s[i]=='#') 
            se.insert(i+1);
    while(!se.empty()) {
        if(cnt==1){
            auto it=se.upper_bound(num);
            if(it!=se.end()){
                int next=*it;
                ans+=(next-num);
                num=next;
                se.erase(it);
                cnt=-1;
            }else{
                ans+=(n+1-num);
                num=n+1;
                cnt=-1;
            }
        }else{
            auto it=se.lower_bound(num);
            if(it!=se.begin()){
                --it;
                int next=*it;
                ans+=(num-next);
                num=next;
                se.erase(it);
                cnt=1;
            }else{
                ans+=num;
                num=0;
                cnt=1;
            }
        }
    }
    cout<<ans;
    return 0;
}
/*

*/
//大:localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'
//小:localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6'