[CF] A. Kevin and Arithmetic - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
Solutions Codeforces 贪心 奇偶性 Easy
Lastmod: 2025-01-20 周一 22:32:51

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

题目大意

给出 $n \ (n \le 100)$ 个数的数组 $a$,其中 $1 \le a_i \le 10^9$。给定 $s = 0$。可以执行以下操作若干次:

从 $a$ 中选一个还没有选过的 $a_i$ 加到 $s$ 上,如果 $s$ 是偶数则得到一分,并且重复除以 $2$ 直到 $s$ 变为奇数。

问最多可以得到多少分。

简要题解

显然这题只与奇偶性有关,除了头一次操作,因为每次都要将值变为奇数,而奇数加偶数还是奇数,因此只有再选一个奇数才能得分。而第一次要尽量选一个偶数。

统计,并且贪心选取即可。

复杂度

$T$:$O(n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n; cin >> n;
  vector<int> a(n);
  for (int& i : a) cin >> i;

  vector<int> cnt(2);
  for (int i : a) cnt[i & 1]++;

  int ans = cnt[1];
  if (cnt[0]) {
    ans++;
  } else {
    ans--;
  }

  cout << ans << '\n';
}

int main() {
  int t = 1; 
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] E. Graph Composition - Codeforces Round 998 (Div. 3)
Next: [CF] B. Kevin and Geometry - IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)