You are given an array a with n non-negative integers. You can apply the following operation on it.
- Choose two indices l and r (1≤l<r≤n).
- If al+ar is odd, do ar:=al. If al+ar is even, do al:=ar.
Find any sequence of at most n operations that makes a non-decreasing. It can be proven that it is always possible. Note that you do not have to minimize the number of operations.
An array a1,a2,…,an is non-decreasing if and only if a1≤a2≤…≤an.
Input
The first line contains one integer t (1≤t≤10^5) — the number of test cases.
Each test case consists of two lines. The first line of each test case contains one integer n (1≤n≤10^5) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤10^9) — the array itself.
It is guaranteed that the sum of n over all test cases doesn't exceed 10^5.
Output
For each test case, print one integer m (0≤m≤n), the number of operations, in the first line.
Then print m lines. Each line must contain two integers li,ri, which are the indices you chose in the i-th operation (1≤li<ri≤n).
If there are multiple solutions, print any of them.
input
3
2
7 8
5
1 1000000000 3 0 5
1
0
output
0
2
3 4
1 2
0
Note
In the second test case, a changes like this:
- Select indices 3 and 4. a3+a4=3 is odd, so do a4:=a3. a=[1,1000000000,3,3,5] now.
- Select indices 1 and 2. a1+a2=1000000001 is odd, so do a2:=a1. a=[1,1,3,3,5] now, and it is non-decreasing.
In the first and third test cases, a is already non-decreasing.
题意:给定长度为n的数组,在一次操作中,你可以选择 l,r 下标,如果al+ar是奇数,那么ar变成al,否则al变成ar,输出最多操作n次使得原数组单调不减的方案。
解析:我们发现可以从两头开始思考,第一步我们直接选1和n,那么操作之后a1和an就相等了,此时我们需要判断是a1变成an还是an变成a1,然后根据a1和an分两类,奇奇和偶偶,如果是奇奇,那么我们遍历2~n-1,如果它是偶数,那么可以直接选a1跟他匹配,否则就选an跟它匹配,然后就可以使得每个数都相等,操作n-1次,如果是偶偶,匹配反一下就行。文章来源:https://www.toymoban.com/news/detail-512477.html
注意:因为规定L!=R,因此当n=1时,我们需要特判一下。文章来源地址https://www.toymoban.com/news/detail-512477.html
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef pair<int,int> PII;
int a[N];
void solve()
{
int n,op;
scanf("%d",&n);
vector<PII> ans;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
if(n!=1) ans.push_back({1,n});//因为l,r需要不同,特判
if((a[1]+a[n])%2)
{
if(a[1]%2) op=1;
else op=2;
}else
{
if(a[n]%2) op=1;
else op=2;
}
if(op==1)//奇奇操作
{
for(int i=2;i<n;i++)
{
if(a[i]%2==0) ans.push_back({1,i});
else ans.push_back({i,n});
}
}
else//偶偶操作
{
for(int i=2;i<n;i++)
{
if(a[i]%2) ans.push_back({1,i});
else ans.push_back({i,n});
}
}
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second);
}
int main()
{
int t=1;
scanf("%d",&t);
while(t--) solve();
return 0;
}
到了这里,关于CF Round 821 (Div. 2)--C. Parity Shuffle Sorting的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!