/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 1ms 332.0 KiB
#3 Accepted 1ms 540.0 KiB
#4 Accepted 1ms 588.0 KiB
#5 Accepted 2ms 540.0 KiB
#6 Accepted 2ms 328.0 KiB
#7 Accepted 2ms 540.0 KiB
#8 Accepted 1ms 772.0 KiB
#9 Accepted 2ms 540.0 KiB
#10 Accepted 2ms 540.0 KiB
#11 Accepted 10ms 540.0 KiB
#12 Accepted 10ms 600.0 KiB
#13 Accepted 10ms 588.0 KiB
#14 Accepted 11ms 608.0 KiB
#15 Accepted 10ms 788.0 KiB
#16 Accepted 11ms 604.0 KiB
#17 Accepted 16ms 600.0 KiB
#18 Accepted 90ms 956.0 KiB
#19 Accepted 101ms 956.0 KiB
#20 Accepted 127ms 952.0 KiB
#21 Accepted 92ms 956.0 KiB
#22 Accepted 87ms 796.0 KiB
#23 Accepted 96ms 956.0 KiB
#24 Accepted 93ms 956.0 KiB
#25 Accepted 131ms 956.0 KiB
#26 Accepted 89ms 1.078 MiB

Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, K, X;
    cin >> N >> K >> X;
    vector<int> A(N);
    for (int i = 0; i < N; i++){
        cin >> A[i];
    }
    
    int ans = 0;
    // Try every candidate d > X. We use d in [X+1, X+100].
    for (int d = X + 1; d <= X + 100; d++){
        long long sumCost = 0;
        int left = 0;
        // Use a sliding window on the array.
        for (int right = 0; right < N; right++){
            // cost to make A[right] divisible by d
            int r = A[right] % d;
            int cost = (r == 0) ? 0 : (d - r);
            sumCost += cost;
            
            // Shrink the window until the total cost is within K.
            while(sumCost > K && left <= right) {
                int r_left = A[left] % d;
                int cost_left = (r_left == 0) ? 0 : (d - r_left);
                sumCost -= cost_left;
                left++;
            }
            ans = max(ans, right - left + 1);
        }
    }
    
    // If no valid subarray found, print -1.
    cout << (ans == 0 ? -1 : ans) << "\n";
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1043 Longest subarray
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-20 13:50:47
Judged At
2025-02-20 13:50:47
Judged By
Score
99
Total Time
131ms
Peak Memory
1.078 MiB