思路是第一篇题解的。。hh
by lew2018 @ 2019-07-14 21:02:05
1.要排序,从大到小输出
2.边界需要特判,最后一颗龙珠不能向后找同伴。
by Clu3ter @ 2019-07-14 21:12:05
@[lew_2018](/space/show?uid=82965)
我的差不多
```
#include<iostream>
#include<algorithm>
using namespace std;
int n,a[100005],next[100005],pre[100005],b[100005],sum1,sum2,tail,head,vis[100005];
struct node
{
int num,id;
}c[100005];
bool cmp(node a,node b)
{
return a.num>b.num;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++)
{
if(i==n)
{
pre[n]=n-1;
next[n]=0;
break;
}
if(i==1)
{
pre[1]=0;
next[1]=2;
continue;
}
pre[i]=i-1,next[i]=i+1;
}
sum1=n,tail=n,head=1;
for(int i=1;i<=n;i++) c[i].num=a[i],c[i].id=i;
sort(c+1,c+n+1,cmp);
while(sum1)
{
while(vis[c[head].id]) head++;
int p=c[head].id;head++;
while(vis[c[head].id]) head++;
if(p==tail) p=c[head].id,head++;
while(vis[c[head].id]) head++;
if(p==pre[tail])
{
tail=pre[p];
}
b[++sum2]=a[p];vis[p]=vis[next[p]]=1;
b[++sum2]=a[next[p]];
sum1-=2;
pre[next[next[p]]]=pre[p];
next[pre[p]]=next[next[p]];
}
for(int i=1;i<=sum2;i++) cout<<b[i]<<" ";
return 0;
}
```
by 有朋自远方来 @ 2019-07-14 21:19:29
@[Veturins](/space/show?uid=70319)
是不需要排序的
详细你可以看一下该题的第一篇题解
by lew2018 @ 2019-07-14 21:21:44
@[Veturins](/space/show?uid=70319)
而且特判也有啊
by lew2018 @ 2019-07-14 21:22:04
我明白了
by lew2018 @ 2019-07-14 21:25:43
```
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100010;
int a[N], nxt[N], pre[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
nxt[a[i - 1]] = a[i];
pre[a[i]] = a[i - 1];
}
for (int i = n; i >= 1; --i) {
if(nxt[i]) {
printf("%d %d ",i,nxt[i]);
nxt[pre[i]]=nxt[nxt[i]];
pre[nxt[pre[i]]]=pre[i];
nxt[nxt[i]]=0;
}
}
return 0;
}
```
by lew2018 @ 2019-07-14 21:26:47
没加大括号。。。。。。
by lew2018 @ 2019-07-14 21:27:06
@[lew_2018](/space/show?uid=82965) em,看错了,我的锅
by Clu3ter @ 2019-07-14 21:29:52