[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.



  • -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; }


  • -1
    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; }


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

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


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

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


  • -1
    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.


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

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