ACWING 第 22 场周赛 AB

好久没有打acwing的周测了欸。

4000.排位

#include<iostream>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n,a,b;
        cin>>n>>a>>b;
        int sum=n-a;
        if(sum-b>1) sum=b+1;
        cout<<sum<<endl;
    }
    return 0;
}

 4001.训练

一开始还以为是并查集,看了y总的写法,妙蛙!

 巩固一下lower_bound()函数的知识

lower_bound()函数:用于在指定范围内查找不小于目标值的第一个目标。

分析题目样例

a[n]: 10  4  10 15

b[n]:  4  10 10 15  (sort之后)

经过int k=lower_bound(b+1,b+1+n,a[i])-b; 之后

k=     2   1   2   4(这里返回的是<=a[i]的第一个目标位置)

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    int a[2002000],b[2002000],cnt[2002000];
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }                //10 4 10 15
    sort(b+1,b+1+n); //4 10 10 15
    while(k--)
    {
        int x,y;
        cin>>x>>y;
        if(a[x]>a[y]) cnt[x]++;//cnt[1]++; cnt[4]++;bb
        else if(a[x]<a[y]) cnt[y]++;
    }
    for(int i=1;i<=n;i++)
    {
        int k=lower_bound(b+1,b+1+n,a[i])-b;
        //cout<<k<<" "<<k-1-cnt[i]<<endl;
        cout<<k-1-cnt[i]<<" ";
    }
    cout<<endl;
    return 0;
}

边学边忘欸,可恶啊啊啊啊啊。

补一个lower_bound()的例题

 Problem - 706B - Codeforces

#include<iostream>
#include<cmath>
#include<map>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int a[200100];
    map<int,int> mp;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    sort(a+1,a+1+n);
    //for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl;
    int q;
    cin>>q;
    while(q--)
    {
        int m;
        cin>>m;
        if(m<a[1]) cout<<"0"<<endl;
        else
        {
            int k=lower_bound(a+1,a+1+n,m)-a;
            if(m==a[k]) cout<<k-1+mp[a[k]]<<endl;
            else cout<<k-1<<endl;
        }
    }
    return 0;
}

要注意的点是lower_bound返回的是查找到它的第一个位置,并不管它后面是否存在相同的数字,所以这题要用map标记个数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vijurria

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值