/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 10ms 564.0 KiB
#3 Accepted 15ms 572.0 KiB
#4 Accepted 15ms 580.0 KiB
#5 Accepted 15ms 536.0 KiB
#6 Accepted 17ms 2.031 MiB
#7 Accepted 18ms 3.953 MiB
#8 Accepted 18ms 3.891 MiB
#9 Accepted 6ms 568.0 KiB

Code

// Author: Shohidur Rahman
#include <bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
 
int main() {
    fast;
    int t;
    cin >> t;
    while(t--){
    	int n,k,d;
    	cin >> n >> k >> d;
    	int arr[n];
    	int pre[n], zero[n];
    	int zero_count = 0, sum = 0;
    	for (int i = 0; i < n; ++i)
    	{
    		cin >> arr[i];
    		if(arr[i] == 0) zero_count++;
    		zero[i] = zero_count;
    		sum += arr[i];
    		pre[i] = sum;
    	}
    	int maxi = 0, maxi_count = 0;
    	bool ans = false;
    	std::vector<int> v;
    	// v[0] = 0;
    	for (int i = 0; i < n-k+1; ++i)
    	{
    		if(maxi == 1) break;
    		if(i == 0) {
    			if((pre[i+k-1]) % d == 0){
    			if(zero[i+k-1]) {
    				maxi = 0;
    				maxi_count = i+1;
    				ans = true;
    			}
    			else{
    				maxi = 1;
    				maxi_count = i+1;
    				ans = true;
    			}
    		}
    		}
    		else if((pre[i+k-1] - pre[i-1]) % d == 0){
    			if(zero[i+k-1] - zero[i-1]) {
    				maxi = 0;
    				maxi_count = i+1;
    				ans = true;
    			}
    			else{
    				maxi = 1;
    				maxi_count = i+1;
    				ans = true;
    			}
    		}
    		v.push_back(maxi_count);
    	}
    	int mini=INT_MAX;
    	if(maxi) cout << maxi_count << endl;
    	else if(ans) {
    		for (int i = 0; i < v.size(); ++i)
    		{
    			// cout << v[i] << ' ';
    			if(v[i]<mini && v[i] > 0) mini=v[i];
    		}
    		cout << mini << "\n";
    	}
    	else cout << "-1\n";
    	
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1190 Segment Strength
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 18:37:31
Judged At
2025-04-06 18:37:31
Judged By
Score
100
Total Time
18ms
Peak Memory
3.953 MiB