Problem A - Ahahahahahahahaha

https://codeforces.com/contest/1407/problem/A

Codeforces Round #669 (Div. 2) A、B题题解 - 图1

题意:

给定一个偶数数组(元素值 0,1),在删除一定的数组元素(最多 Codeforces Round #669 (Div. 2) A、B题题解 - 图2 个) 以后偶数位和 是否能等于 奇数位和。

在不改变原序列的情况下输出剩下的 k个数 Codeforces Round #669 (Div. 2) A、B题题解 - 图3#card=math&code=%28n%20%2F%202%20%3C%3D%20K%20%3C%3D%20n%29)

思路:

先统计 数组整体和 Codeforces Round #669 (Div. 2) A、B题题解 - 图4 , 如果 Codeforces Round #669 (Div. 2) A、B题题解 - 图5 即数组中一半以上是 Codeforces Round #669 (Div. 2) A、B题题解 - 图6,这种情况下只需删去 $ s % 2 $,奇数转偶数。具体可以参考以下代码

  1. //Cpp
  2. #include<bits/stdc++.h>
  3. #define ll int long long
  4. using namespace std;
  5. int i, j, k;
  6. string yn[2] = { "No\n","Yes\n" };
  7. int main(){
  8. int t; cin >> t; while (t--) {
  9. int o = 0, e = 0, x = 0, n, s = 0; cin >> n;
  10. int a[n];
  11. for (i = 0; i < n; i++) {
  12. cin >> a[i]; s += a[i];
  13. }
  14. if (s > n / 2)x++;
  15. if (x)s -= s % 2;
  16. else s = n - s;
  17. cout << s << endl;
  18. for (i = 0; i < s; i++)cout << x << " ";
  19. cout << endl;
  20. }
  21. }
  1. #python
  2. for _ in " "*int(input()):
  3. a=int(input())
  4. z=list(map(int,input().split()))
  5. s=z.count(0);k=a//2
  6. s1=z.count(1)
  7. if s>=s1:print(k);print('0 '*k)
  8. else:print(k+k%2);print('1 '*(k+k%2))

Problem B - Big Vova

https://codeforces.com/contest/1407/problem/B

Codeforces Round #669 (Div. 2) A、B题题解 - 图7

首先把a从大到小排序,因为最大的gcd肯定最大 所以直接输出a1,然后暴力找于a1gcd最大的元素并把它做上标记以此类推,on2找完所有元素,但是如果迭代中发现gcd==1了就可以跳出循环从大到小输出其数组元素就可以了

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int gcd(int a,int b){
  5. if(b==0) return a;
  6. else return gcd(b,a%b);
  7. }
  8. void solve(){
  9. int n;cin>>n;
  10. int a[n];
  11. for(int i=0;i<n;i++) cin>>a[i];
  12. sort(a,a+n);
  13. for(int i=0;i<n/2;i++) swap(a[i],a[n-i-1]);
  14. int ans=a[0],y;
  15. for(int i=1;i<n;i++){
  16. int mxm_gcd=1,y=i;
  17. for(int j=i;j<n;j++){
  18. int x=mxm_gcd;
  19. mxm_gcd=max(gcd(a[j],ans),mxm_gcd);
  20. if(mxm_gcd>x) {
  21. y=j;
  22. }
  23. }
  24. swap(a[i],a[y]);
  25. ans=gcd(ans,a[i]);
  26. }
  27. for(int i=0;i<n;i++) cout<<a[i]<<" ";
  28. cout<<"\n";
  29. }
  30. int main(){
  31. int t;cin>>t;
  32. while(t--) solve();
  33. }