Codeforces Round #651 (Div. 2) 参与排名人数14559
[codeforces 1370B] GCD Compression 构造共有因子为2的数组集合
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.com/contest/1370/problem/B
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
B - GCD Compression | GNU C++17 | Accepted | 46 ms | 200 KB |
题目大意:给定包含元素个数为2n的数组a,删去其中2个元素,剩下的元素两两组合求和,构成包含n-1个元素的数组b,要求数组b中的元素共有的因子大于1,输出数组a两两选择数据所处的位置.
基本思路:数组a中元素数值的构成要么奇数,要么偶数,考虑到奇数+奇数=偶数,偶数+偶数=偶数,可以构造出和值为偶数的n-1个数,这样,共有因子是2.
样例模拟如下
5
1 3 3 4 5 90 100 101 2 3
1 2
3 5
4 6
7 9
可这样构成
位置1 2 3 4 5 6 7 8 9 10
数值1 3 3 4 5 90 100 101 2 3
目的让和值为偶数
1+3=4,对应位置1 2
3+5=8,对应位置3 5
4+90=94,对应位置4 6
100+2=102,对应位置7 9
AC代码如下
#include <stdio.h>
#define maxn 1010
int a[maxn<<1];
void solve(){
int i,n,pre1,pre0,cnt1=0,cnt0=0,cnt=0;
scanf("%d",&n);
for(i=1;i<=n*2;i++)scanf("%d",&a[i]),a[i]%=2;
for(i=1;i<=n*2;i++){
if(a[i]){//奇数配对
cnt1++;
if(cnt1%2==0)printf("%d %d\n",pre1,i),cnt++;
pre1=i;
}else{//偶数配对
cnt0++;
if(cnt0%2==0)printf("%d %d\n",pre0,i),cnt++;
pre0=i;
}
if(cnt==n-1)break;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}