[Mảng 1 Chiều Cơ Bản]. Bài 44. Dãy con chẵn lẻ

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng A[] gồm N phần tử, bạn hãy đếm số lượng dãy con liên tiếp mà số lượng số chẵn bằng số lượng số lẻ.


Đầu vào

Dòng 1 là N : số phần tử trong mảng

Dòng 2 là N phần tử cách nhau 1 khoảng trắng


Giới hạn

1<=N<=1000

0<=A[i]<=1000


Đầu ra

In ra đáp án của bài toán


Ví dụ :

Input 01
7
3 2 1 6 4 0 0
Output 01
4
Giải thích :

Các dãy con thỏa mãn {3, 2}, {3, 2, 1, 6}, {1, 6}, {2, 1}


Comments

Please read the guidelines before commenting.



  • 5
    nmtuong_cpp  commented on July 5, 2025, 11:40 a.m.

    Bài này cũng thú vị. Cách làm của mình là ta sẽ xét mỗi phần tử trong mảng (1). Với mỗi phần tử đang xét, ta mở rộng sang phải để tạo dãy con bằng một vòng for nửa. Bạn để ý thì để điều kiện đếm thỏa mãn thì số chẵn phải bằng số lẽ (2). Mình nghĩ code mình khá dễ hiểu, không map không vector.

    Source code:

    include <iostream>

    using namespace std;

    int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i];

    int numberOfEvens = 0, numberOfOdds = 0, numberOfSubsequences = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (arr[j] % 2 == 0) numberOfEvens++;
            else numberOfOdds++;
    
            if (numberOfEvens == numberOfOdds) numberOfSubsequences++;
        }
        numberOfEvens = 0; numberOfOdds = 0;
    }
    cout << numberOfSubsequences;
    
    return 0;
    

    }


  • 1
    bengokyeuanh99  commented on May 1, 2025, 8:09 p.m.

    include <bits/stdc++.h>

    using namespace std;

    using ll = long long;

    ll countBalancedSubarrays(const vector<int>& arr) { unordered_map<int, int> freq; freq[0] = 1;

    int prefixSum = 0;
    ll count = 0;
    
    for (int x : arr) {
        prefixSum += (x % 2 == 0 ? 1 : -1);
        count += freq[prefixSum];
        freq[prefixSum]++;
    }
    
    return count;
    

    }

    int main() { iosbase::syncwith_stdio(false); cin.tie(nullptr);

    int n;
    cin >> n;
    
    vector<int> a(n);
    for (int& x : a) cin >> x;
    
    cout << countBalancedSubarrays(a) << '\n';
    return 0;
    

    }


  • -3
    VDev  commented on Feb. 27, 2025, 10:00 a.m. edited

    include <bits/stdc++.h>

    #define ll long long
    using namespace std;
    ll a[10000011];
    
    int main(){
        ll n, chan = 0, le = 0, cnt = 0;
        cin >> n;
        for(ll i = 1; i <= n; i++){
            cin >> a[i];
        }
        for(ll i = 1; i <= n; i++){
                chan = 0, le = 0;
            for(ll j = i; j <= n; j++){
                    if(a[j] % 2 == 0) chan++;
                    else le++;
                    if(chan == le) cnt++;
            }
        }
        cout << cnt;
        return 0;
    }
    

  • -2
    knight01  commented on Feb. 6, 2025, 8:06 p.m. edit 2

    include <iostream>

    #include <numeric>
    using namespace std;
    bool check(int a[], int n, int i, int j) {
        int evenCnt = 0, oddCnt = 0;
        for (int k = i; k <= j; k++) {
            if (a[k] % 2 == 0) evenCnt++;
            else oddCnt++;
        }
        return evenCnt == oddCnt;
    }
    
    int main() {
        int n; cin >> n;
        int a[n];
        for (int i = 0; i < n; i++) cin >> a[i];
        int cnt = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = i; j < n; ++j) {
                if (check(a, n, i, j)) cnt++;
            }
        }
        cout << cnt;
    }
    

  • -2
    duongpvd12044  commented on Sept. 24, 2024, 5:41 p.m.

    ae tham khảo ^_^

    include<bits/stdc++.h>

    using namespace std;
    int main() { map mp; long long tong=0; int dem=0; mp[0].pushback(1); int n;cin>>n; for(int i=0;i<n;i++) { int x;cin>>x; if(x%2==0) tong=tong+1; else tong=tong-1; if(mp.count(tong)!=0)
    { dem=dem+mp[tong].size(); mp[tong].push
    back(i); } else mp[tong].push_back(i); } cout<<dem; }


  • -2
    Nguyen_Van_Tien_ccp012  commented on Aug. 26, 2024, 7:32 a.m.

    include <iostream>

    include <math.h>

    include <set>

    include <map>

    include <bits/stdc++.h>

    using namespace std; long long gt(int n) { long long sum = 0; for (int i = 0;i <= n;i++) sum += i; return sum; } int main() { int n;cin >> n; int a[n]; for (int i = 0;i < n;i++) cin >> a[i]; int tong = 0; long long demt = 0; map <int,int> mp; for (int i = 0;i < n;i++) { if (a[i] % 2 == 0) a[i] = 1; else a[i] = -1; } for (int i = 0;i < n;i++) { tong += a[i]; mp[tong]++; } for (auto it : mp) { if (it.first == 0) demt += gt(it.second); else { demt += gt(it.second - 1); } } cout << demt << endl; }


    • 5
      duy210  commented on Sept. 20, 2024, 11:13 a.m.

      anh giải thích ý tưởng code của anh được không :((


      • 0
        Tunganh9CT  commented on May 1, 2025, 5:15 p.m.
        1. Biến đổi mảng Chuyển mỗi phần tử:

        Nếu là số chẵn, gán thành -1

        Nếu là số lẻ, gán thành +1

        Như vậy:

        Một dãy con có số lẻ = số chẵn ⇨ tổng của dãy con sau biến đổi sẽ bằng 0.

        1. Dùng prefix sum và hashmap đếm số dãy con có tổng bằng 0 Duyệt từ đầu đến cuối mảng, tính prefix_sum sau khi biến đổi.

        Dùng một map lưu số lần xuất hiện của mỗi prefix_sum.

        Nếu tại một thời điểm nào đó, tổng prefix_sum = x, thì số lần ta đã gặp x trước đó chính là số dãy con kết thúc tại vị trí hiện tại có tổng = 0.


  • -6
    TomGetsu  commented on Aug. 21, 2024, 3:59 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 0
    Lamlilac1234  commented on Aug. 11, 2024, 3:18 p.m.

    đề bài nói là dãy liên tiếp nhé, ae cẩn thận. tôi làm hết tất cả dãy con muốn sảng luôn mới biết.


  • -29
    phipham304  commented on April 7, 2024, 5:28 a.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.