1. 程式人生 > 實用技巧 >CFEDU100 Find The Array

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