码迷,mamicode.com
首页 > 其他好文 > 详细

【OJ2178】RP俱乐部

时间:2018-07-22 00:21:24      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:++   algorithm   inline   大于   开始   strong   第一个   char   time   

2178 -- RP俱乐部(Solution)

题目大意 :\(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);
    }
}

【OJ2178】RP俱乐部

标签:++   algorithm   inline   大于   开始   strong   第一个   char   time   

原文地址:https://www.cnblogs.com/farway17/p/9348382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!