def read_int():
return int(input())
def read_array():
return list(map(int, input().split()))
n = read_int()
s = input()
levels = read_array()
import heapq
h = []
b, g = 0, 0
for i in range(len(s)):
if i > 0 and s[i] != s[i - 1]:
heapq.heappush(h, (abs(levels[i] - levels[i - 1]), i - 1, i))
if s[i] == 'B':
b += 1
elif s[i] == 'G':
g += 1
pr = [i for i in range(n)]
pl = [i for i in range(n)]
sz = [1 for _ in range(n)]
def find(v, p):
if v < 0 or v == n:
return v
if v == p[v]:
return v
p[v] = find(p[v], p)
return p[v]
def union(u, v, p):
a = find(u)
b = find(v)
if a == b:
return
if sz[a] < sz[b]:
a, b = b, a
p[a] = p[b]
sz[a] += sz[b]
ans = []
x = min(b, g)
deleted = set()
while x > 0:
_, l, r = heapq.heappop(h)
if l in deleted or r in deleted:
continue
x -= 1
ans.append((l, r))
deleted.add(l)
deleted.add(r)
pr[l] = r + 1
pr[r] = r + 1
pl[l] = l - 1
pl[r] = l - 1
r, l = find(l, pr), find(r, pl)
if l >= 0 and r < len(s) and s[l] != s[r]:
heapq.heappush(h, (abs(levels[l] - levels[r]), l, r))
print(len(ans))
for l, r in ans:
print(l+1, r+1)
#include <stdio.h>
#define abs(a) ((a)<0?-(a):(a))
char str[200005]={'\0'};
long a[200005]={0};
long next[200005]={0};
long last[200005]={0};
long ans[200005][2]={0};
long calc=0;
struct case1
{
long num;
long x,y;
}heap[200005]={{0,0,0}};
long tot=0;
long trans[200005][2]={0};
void swap(long i,long j)
{
struct case1 temp;
trans[heap[i].x][0]=j;
trans[heap[i].y][1]=j;
trans[heap[j].x][0]=i;
trans[heap[j].y][1]=i;
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
}
void up(long x)
{
if(x>tot)
return;
for(;x>1;x>>=1)
{
if(heap[x].num>heap[x>>1].num||heap[x].num==heap[x>>1].num&&heap[x].x>heap[x>>1].x)
return;
swap(x,x>>1);
}
}
void down(long x)
{
for(;x*2<=tot;)
{
if((heap[x].num<heap[x*2].num||heap[x].num==heap[x*2].num&&heap[x].x<heap[x*2].x)&&(heap[x].num<heap[x*2+1].num||heap[x].num==heap[x*2+1].num&&heap[x].x<heap[x*2+1].x||x*2+1>tot))
return;
if(heap[x*2].num<heap[x*2+1].num||heap[x*2].num==heap[x*2+1].num&&heap[x*2].x<heap[x*2+1].x||x*2+1>tot)
{
swap(x,x*2);
x=x*2;
}
else
{
swap(x,x*2+1);
x=x*2+1;
}
}
}
void del(long x)
{
if(x)
{
swap(x,tot);
trans[heap[tot].x][0]=0;
trans[heap[tot].y][1]=0;
heap[tot].num=heap[tot].x=heap[tot].y=0;
tot--;
up(x);
down(x);
}
}
int main()
{
long n,i,x,y;
scanf("%ld",&n);
scanf("%s",str+1);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);
for(i=1;i<=n;i++)
{
next[i]=i+1;
last[i]=i-1;
}
for(i=1;i<n;i++)
{
if(str[i]!=str[i+1])
{
heap[++tot].x=i;
heap[tot].y=i+1;
heap[tot].num=abs(a[i]-a[i+1]);
trans[i][0]=tot;
trans[i+1][1]=tot;
up(tot);
}
}
for(;tot;)
{
x=heap[1].x;
y=heap[1].y;
ans[++calc][0]=x;
ans[calc][1]=y;
del(trans[x][0]);
del(trans[x][1]);
del(trans[y][0]);
del(trans[y][1]);
if(next[y]<=n)
last[next[y]]=last[x];
if(last[x]>=1)
next[last[x]]=next[y];
if(next[y]<=n&&last[x]>=1&&str[next[y]]!=str[last[x]])
{
heap[++tot].x=last[x];
heap[tot].y=next[y];
heap[tot].num=abs(a[next[y]]-a[last[x]]);
trans[last[x]][0]=tot;
trans[next[y]][1]=tot;
up(tot);
}
}
printf("%ld\n",calc);
for(i=1;i<=calc;i++)
{
printf("%ld %ld\n",ans[i][0],ans[i][1]);
}
return 0;
}
80A - Panoramix's Prediction | 1354B - Ternary String |
122B - Lucky Substring | 266B - Queue at the School |
1490A - Dense Array | 1650B - DIV + MOD |
1549B - Gregor and the Pawn Game | 553A - Kyoya and Colored Balls |
1364A - XXXXX | 1499B - Binary Removals |
1569C - Jury Meeting | 108A - Palindromic Times |
46A - Ball Game | 114A - Cifera |
776A - A Serial Killer | 25B - Phone numbers |
1633C - Kill the Monster | 1611A - Make Even |
1030B - Vasya and Cornfield | 1631A - Min Max Swap |
1296B - Food Buying | 133A - HQ9+ |
1650D - Twist the Permutation | 1209A - Paint the Numbers |
1234A - Equalize Prices Again | 1613A - Long Comparison |
1624B - Make AP | 660B - Seating On Bus |
405A - Gravity Flip | 499B - Lecture |