CFEDU100 Find The Array

题目链接:Find The Array

 

题目大意:

有一个数组A,要找一个数组B,要求B中每个数小于1e9,并且,bi能整除bi+1(或者bi+1整除bi,或者互相整除),并且2*sum(|ai-bi|) <= A的和

 

解题思路:

考虑Sodd和Seven,显然,Sodd和Seven必有一个大于等于S/2,一个小于等于S/2,所以可以考虑先让偶数位的数都为1,这是满足第二个条件,然后判断第三个条件是否满足,如果不满足,就让所有奇数位的数都为1

 

参考代码:

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 int a[60];
 5 signed main() {
 6     int t,n;
 7     cin >> t;
 8     while(t--) {
 9         cin >> n;
10         int s = 0;
11         for(int i = 1; i <= n; i++) {
12             cin >> a[i];
13             s += a[i];
14         }
15         int s1 = 0;
16         for(int i = 1; i <= n; i++) {
17             if(i % 2 == 0) {
18                 s1 += abs(a[i]-1);
19             }
20         }
21         if(s1*2 <= s) {
22             for(int i = 1; i <= n; i++) {
23                 if(i % 2 == 0) cout << 1 << " ";
24                 else cout << a[i] << " ";
25             }
26             cout << endl;
27         } else {
28             for(int i = 1; i <= n; i++) {
29                 if(i % 2 == 1) cout << 1 << " ";
30                 else cout << a[i] << " ";
31             }
32             cout << endl;
33         }
34     }
35     return 0;
36 }
View Code

 

posted @ 2020-12-20 19:26  不敢说的梦  阅读(189)  评论(0)    收藏  举报