第二次训练课总结

· · 个人记录

P1897 电梯里的尴尬

这道题就是让我们计算电梯完成这趟任务需要的时间,

所以我们只需要求出达到每一层的时间,开门的时间,下一个人的时间和返回0层的时间。

#include<bits/stdc++.h>
using namespace std;
long long  n,a[1000005],m,ans;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+1+n);//从小到大排序
    for(int i=1;i<=n;i++){
        ans+=(a[i]-a[i-1])*6;
        if(a[i]!=a[i-1]){// 电梯开门,如果我和前一个人是去同一楼层,不需要重新开门
            ans+=5;
        }
        ans++;// 第i个人走出电梯
    } 
    ans+=a[n]*4;
    cout<<ans;
    return 0;
}

P3662 [USACO17FEB] Why Did the Cow Cross the Road II S

这道题计算让我们要计算正好有连续的k个信号灯亮,最少需要修复多少灯,

所以我们可以用前缀和来做,我们可以将坏灯的a[i]设置1, 好灯设置为0,那么其实就是要计算l ~ r之间1的数量。

#include<bits/stdc++.h>
using namespace std;
int n,m,k,a[100005],sum[100005],ans = 1e9;
int main(){
    cin>>n>>k>>m;
    for(int i=1;i<=m;i++){
        int x;
        cin>>x;
        a[x]=1;
    }
    for(int i=1;i<=n;i++){
        sum[i]=sum[i-1]+a[i];//前缀和
    }
    for(int r=k;r<=n;r++){
        int l=r-k+1;
        int cnt=sum[r]-sum[l-1];
        ans=min(ans,cnt);//求最小值
    }
    cout<<ans;
    return 0;
}

B3784 [语言月赛202306] 演唱会

这道题就是让我们他按快欢乐度列出演唱会的歌单,

所以我们需要从大到小排序,再判断他最喜欢的歌在不在前m个,如果在就先输出,否则最后输出,

#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,h[105][100005];
struct q{
    int v,id;
}c[100005];
int cmp(q a1,q a2){
    return a1.v>a2.v;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>a>>b;
    for(int i=1;i<=a;i++){
        for(int j=1;j<=n;j++){
            cin>>h[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=a;j++){
            c[i].v+=h[j][i];// 第i首歌的欢乐度 = 所有同学对于这首歌的喜爱度
        }
        c[i].id=i;
    }
    sort(c+1,c+1+n,cmp);// 从小到大排序
    int mx=1;
    for(int i=2;i<=n;i++){
        if(h[b][i]>h[b][mx]){
            mx=i;// 求出他最喜欢的歌
        }
    }
    int f=0;
    for(int i=1;i<=m;i++){
        if(c[i].id==mx){
            f=1;
        }
    }

    if(f==1){// 在歌单中
        cout<<mx<<" ";//先输出
        for(int i=1;i<=m;i++){
            if(c[i].id!=mx){
                cout<<c[i].id<<" ";
            }
        }
    }
    else{// 不在歌单中

        for(int i=1;i<m;i++){
            cout<<c[i].id<<" ";
        }
        cout<<mx;//后输出
    }
    return 0;
}

P2772 寻找平面上的极大点

这道题就让我们是按x轴坐标最小到大的顺序输出所有极大点,

将所有的点按照x坐标从大到小排序,如果x相同那么按照y从大到小排序,

再判断1 ~ i-1中y的最大值是否大于等于 a[i].y。

#include<bits/stdc++.h>
using namespace std;
struct q{
    int x,y;
}a[500005];
int n;
q b[500005];
int cmp(q a1,q a2){
    if(a1.x == a2.x)// 按照x从大到小排序,如果x相同按照y从大到小排序
        return a1.y > a2.y;
    return a1.x>a2.x;
}
int cmp1(q a1,q a2){
    return a1.x<a2.x;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
    }

    sort(a+1,a+1+n,cmp);
    int maxn=-1e9;
    int idx=0;
    for(int i=1;i<=n;i++){
        if(maxn>=a[i].y){
            continue;
        }
        idx++;
        b[idx]=a[i];// 存储所有的极大点
        maxn=max(maxn,a[i].y);
    }
    sort(b+1,b+1+idx,cmp1);// 按照x坐标从小到大排序
    for(int i=1;i<=idx;i++){
        cout<<"("<<b[i].x<<","<<b[i].y<<")";// 输出极大点
        if(i!=idx){
            cout<<",";
        }
    }
    return 0;
}