好久没有打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()的例题
#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标记个数。