【题解】P5462 X龙珠

该博客是P5462 X龙珠赛题的题解,介绍了使用set的做法。维护两个set,一个按顺序排序,一个按值排序,从大往小取数,通过查看按顺序排序的数是否有后继来解决问题,编译需用c++11。

【题解】P5462 X龙珠

赛题 #B: P5462 X龙珠 | 满分: 100分

发一个set做法

维护两个set,一个按照顺序排序,一个按照值排序。

每次从大往小取,问题就变成了判断这个最大值后面是否有数,直接查看一下按照顺序排序的该数是否有后继。

编译记得用c++11,请安心食用。

(为了方便理解,按照顺序排序的set写得不够优美)

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>

using namespace std;  typedef long long ll;
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57)f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}
typedef pair < int , int > node;
#define x first
#define y second
#define mk make_pair
set < node > s;
set < int ,greater < int > > s1;
int n;
const int maxn=1e5+5;
int arc[maxn];
vector < int > ve;
int main(){
#ifndef ONLINE_JUDGE
      freopen("in.in","r",stdin);
      //freopen("out.out","w",stdout);
#endif
      n=qr();
      for(register int t=1,c;t<=n;++t)
        s.insert(mk(t,c=qr())),s1.insert(c),arc[c]=t;
      for(register int t=1;t<=n>>1;++t){
        for(auto it:s1){
          auto ti=s.find(mk(arc[it],it));
          if(ti!=s.end()&&++ti!=s.end()){
            auto t1=*ti--;
            auto t2=*ti;
            s.erase(t1);
            s.erase(t2);
            ve.push_back(t2.second);
            ve.push_back(t1.second);
            s1.erase(t1.second);
            s1.erase(t2.second);
            break;
            
          }
        }
      }
      int cnt=0;
      for(auto t:ve) printf("%d%c",t,++cnt==n?'\n':' ');
      return 0;
}

转载于:https://www.cnblogs.com/winlere/p/11185307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值