/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 9ms 540.0 KiB
#3 Accepted 16ms 540.0 KiB
#4 Accepted 13ms 584.0 KiB
#5 Accepted 13ms 540.0 KiB
#6 Accepted 14ms 820.0 KiB
#7 Accepted 13ms 1.277 MiB
#8 Accepted 14ms 1.277 MiB
#9 Accepted 6ms 540.0 KiB

Code

#include <bits/stdc++.h>
#define nl '\n'
#define ll long long int
#define all(c) c.begin(),c.end()
#define print(c) for(auto e : c) cout << e << " "; cout << nl
using namespace std;
void solve()
{
    int n, k, d; cin >> n >> k >> d;
    vector<int> a(n); for(auto &e : a) cin >> e; // only 0 and 1

    // if there are k times one's without inclusive zero
    bool isON = false;
    int sum = 0;
    for (int l = 0, r = 0; r < n;)
    {
        if(isON == false && a[r] == 1)
        {
            isON = true;
            l = r;
            sum += a[l];
            if(r-l+1 == k && sum%d == 0)
            {
                cout << l+1 << nl; return;  
            }
        }
        else if(a[r] == 1) 
        {
            sum += a[l];
            if(r-l+1 == k && sum%d == 0)
            {
                cout << l+1 << nl; return;
            }
        }
        else 
        {
            sum = 0; // sum will be reset
            isON = false;
        }

        r++;
    }

    // without inclusive 1, that means, 0 available, product is zero
    sum = 0;
    for (int l = 0, r = 0; r < n; r++)
    {
        sum += a[r];
        if(r-l+1 == k)
        {
            if(sum%d == 0)
            {
                cout << l+1 << nl; return;
            }
            sum -= a[l];
            l++;
        }
    }

    // didn't match
    cout << -1 << nl;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t;
    for(int tt = 1; tt <= t; tt++)
    {
        // cout << "TEST CASE-" << tt << nl;
        solve();
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1190 Segment Strength
Contest
Brain Booster #9
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 16:22:45
Judged At
2025-04-06 16:22:45
Judged By
Score
100
Total Time
16ms
Peak Memory
1.277 MiB