A. Team

given n lines containing three single integers. each integer is either 1 or 0.

output the number of the lines which at least contain 2 intergers which’s value is 1.

Solving

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

int main(){
	int n; cin >> n;
	int ans = 0;
	while(n--) {
		int a,b,c;
		int cnt = 0;
		cin >> a >> b >> c;
		if(a) cnt++;
		if(b) cnt++;
		if(c) cnt++;
		if(cnt >= 2) ans++;
	}
	cout << ans << endl;
	return 0;
}
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int solved_count = 0;
    for (int i = 0; i < n; ++i) {
        int p, v, t;
        cin >> p >> v >> t;
        if (p + v + t >= 2) {
            solved_count++;
        }
    }
    cout << solved_count << endl;
    return 0;
}

B. Magic, Wizardry and Wonders

given four numbers: 4 1 1 3,

Each iteration will calculate the difference between the two rightmost numbers:

4, 1, 1, 3
4, 1, -2
4, 3
1

Input:

Output:

return -1, if there doen’t exist a set that meets the requirements.

otherwise, return the original number list before any iteration.

Solving

$$ \begin{align} d &= a_1 - a_2 + a_3 - a_4 \ d &=(a1 + a3) - (a2 + a4) \ d &= \text{sum in odd} - \text{sum in even} \end{align} $$ We can greadily reach to $d$ by starting from its minimal value, achieved by minimizing the odd sum part, and maximizing the even sum part.

First, do a feasibility check:

$a_i$ is between 1 and L. The minimal possible value for $d$ is cnt_odd - cnt_even × L. The maximal possible value for $d$ is cnt_odd × L - cnt_even. if d is out of this range, return -1.

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

int main() {
    int n, D, L;
    cin >> n >> D >> L;

    int cnt_odd = (n + 1) / 2;
    int cnt_even = n / 2; 

    // Calculate min and max possible D
    int min_S = cnt_odd - cnt_even * L;
    int max_S = cnt_odd * L - cnt_even;

    if (D < min_S || D > max_S) {
        cout << -1 << endl;
        return 0;
    }

    vector<int> odd(cnt_odd, 1);   // Start with minimal odd terms
    vector<int> even(cnt_even, L); // Start with maximal even terms

    int current_S = min_S;

    // Greedily adjust to reach D
    int i = 0, j = 0;
    while (current_S < D && (i < cnt_odd || j < cnt_even)) {
        if (i < cnt_odd) {
            if (odd[i] < L) {
                odd[i]++;
                current_S++;
            } else {
                i++;
            }
        } else {
            if (even[j] > 1) {
                even[j]--;
                current_S++;
            } else {
                j++;
            }
        }
    }

    // Construct the answer
    vector<int> ans;
    for (int k = 0; k < n; k++) {
        if (k % 2 == 0) { // Odd position (1-based)
            ans.push_back(odd[k / 2]);
        } else {           // Even position
            ans.push_back(even[k / 2]);
        }
    }

    for (int num : ans) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

C. To Add or Not to Add