已知可以得到 k k k 的方式 ( l , r ) (l,r) (l,r),就一定有得到 k − 2 k-2 k−2 的方式:
- 若 a l = 2 a_l=2 al=2,则取 ( l + 1 , r ) (l+1,r) (l+1,r)
- 若 a r = 2 a_r=2 ar=2,则取 ( l , r − 1 ) (l,r-1) (l,r−1)
- 否则 a l = a r = 1 a_l=a_r=1 al=ar=1,取 ( l + 1 , r − 1 ) (l+1,r-1) (l+1,r−1)
所以我们求出能得到的最大奇数和最大偶数,设数列总和为
S
S
S,则
S
S
S
是什么数(奇或偶),那么最大什么数就是
S
S
S,另一个也很好算,求出那个离边界最近的
1
1
1 的位置就行。
//P3514
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N], sum, l1, r1, m1, m2;
pair<int, int> ans[N*2];
char s[N];
int main(){
scanf("%d%d%s", &n, &m, s+1);
for(int i = 1; i <= n; ++ i){
a[i] = (s[i] == 'T' ? 2 : 1);
sum += a[i];
if(!l1 && a[i] == 1) l1 = i;
}
for(int i = n; i >= 1; -- i) if(!r1 && a[i] == 1) r1 = i;
if(sum%2){
m1 = sum;
ans[m1] = make_pair(1, n);
if(!r1 && !l1) m2 = -1;
if(n-r1 < l1-1) m2 = m1 - (n-r1)*2 - 1, ans[m2] = make_pair(1, r1-1);
else m2 = m1 - l1*2 + 1, ans[m2] = make_pair(l1+1, n);
} else {
m2 = sum;
ans[m2] = make_pair(1, n);
if(!r1 && !l1) m1 = -1;
if(n-r1 < l1-1) m1 = m2 - (n-r1)*2 - 1, ans[m1] = make_pair(1, r1-1);
else m1 = m2 - l1*2 + 1, ans[m1] = make_pair(l1+1, n);
}
for(int i = m1-2; i >= 1; i -= 2){
int l = ans[i+2].first, r = ans[i+2].second;
if(a[l] == 2) ans[i] = make_pair(l+1, r);
else if(a[r] == 2) ans[i] = make_pair(l, r-1);
else ans[i] = make_pair(l+1, r-1);
}
for(int i = m2-2; i >= 2; i -= 2){
int l = ans[i+2].first, r = ans[i+2].second;
if(a[l] == 2) ans[i] = make_pair(l+1, r);
else if(a[r] == 2) ans[i] = make_pair(l, r-1);
else ans[i] = make_pair(l+1, r-1);
}
for(int i = 1, k; i <= m; ++ i){
scanf("%d", &k);
if((k%2 && k>m1) || (k%2==0 && k>m2)) puts("NIE");
else printf("%d %d\n", ans[k].first, ans[k].second);
}
return 0;
}