标签:++ algorithm inline 大于 开始 strong 第一个 char time
题目大意 : 有 \(n\) 个人,每个人有两个属性 \(RP\) 和 \(ID\) ,保证 \(RP\) 和 \(ID\) 互不相同。第 \(i\) 个人与前 \(i-1\) 个人中 \(RP\) 与他差值最小的人比赛(如果差值相同则选择 \(RP\) 更小的)。求每场比赛双方的 \(ID\) 。(最开始有一个人\(ID=1, RP=10^9\)) \((n\le2\times 10^5,\, ID,RP\leq10^9)\)
Tag: STL
Analysis By LC:
将每个人的信息存入 \(\rm set\) ,利用 \(\rm set\) 中的 \(\rm lower\_bound\) 找到第一个大于和第一个小于即可。
Code By LC :
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
inline int _read()
{
char c; int x=0;
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
return x;
}
const int N=200005;
struct Node
{
int id,rp;
}a[N];
bool operator < (Node x, Node y)
{
if(x.rp==y.rp) return x.id>y.id;
return x.rp<y.rp;
}
set<Node> s;
int main()
{
int n=_read(); s.insert((Node){1,1000000000});
for(int i=1;i<=n;i++)
{
Node u;
u.id=_read(),u.rp=_read();
printf("%d ",u.id);
auto x=s.lower_bound(u);
if(x->rp==u.rp||x==s.begin()) printf("%d\n",x->id);
else
{
auto y=x; y--;
if(u.rp-y->rp>x->rp-u.rp) printf("%d\n",x->id);
else printf("%d\n",y->id);
}
s.insert(u);
}
}
标签:++ algorithm inline 大于 开始 strong 第一个 char time
原文地址:https://www.cnblogs.com/farway17/p/9348382.html