/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 4ms 532.0 KiB
#5 Accepted 5ms 532.0 KiB
#6 Accepted 5ms 532.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 5ms 532.0 KiB
#9 Accepted 4ms 532.0 KiB
#10 Accepted 4ms 532.0 KiB
#11 Accepted 17ms 580.0 KiB
#12 Accepted 7ms 532.0 KiB
#13 Accepted 7ms 532.0 KiB
#14 Accepted 7ms 532.0 KiB
#15 Accepted 7ms 532.0 KiB
#16 Accepted 23ms 532.0 KiB
#17 Accepted 21ms 532.0 KiB
#18 Accepted 56ms 1.27 MiB
#19 Accepted 55ms 1.27 MiB
#20 Accepted 8ms 1.184 MiB
#21 Accepted 63ms 1.27 MiB
#22 Accepted 60ms 1.305 MiB
#23 Accepted 15ms 1.305 MiB
#24 Accepted 12ms 1.27 MiB
#25 Accepted 13ms 1.18 MiB
#26 Accepted 12ms 1.27 MiB

Code

#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int N, X;
    long long K;
    cin >> N >> K >> X;
    vector<int> A(N);
    for (int i = 0; i < N; i++){
        cin >> A[i];
    }
    
    // Choose candidate divisors d. Since A[i] <= 100 and X <= 100,
    // if X < 100, try d from X+1 to 100.
    // If X == 100, then d must be > 100; in that case we try d = 101.
    int d_low = X + 1;
    int d_high = (X < 100 ? 100 : 101);
    
    int ans = 0;
    // Try each candidate divisor d.
    for (int d = d_low; d <= d_high; d++){
        // Build cost array: cost[i] is the operations needed to make A[i] divisible by d.
        vector<int> cost(N, 0);
        for (int i = 0; i < N; i++){
            int r = A[i] % d;
            // if already divisible, cost is 0; otherwise cost is (d - r)
            cost[i] = (r == 0 ? 0 : d - r);
        }
 
        // Use two pointers (sliding window) to find the longest subarray with total cost <= K.
        long long currentSum = 0;
        int left = 0;
        int best = 0;
        for (int right = 0; right < N; right++){
            currentSum += cost[right];
            // Shrink window while cost sum exceeds K.
            while(currentSum > K && left <= right){
                currentSum -= cost[left];
                left++;
            }
            best = max(best, right - left + 1);
        }
        ans = max(ans, best);
    }
 
    cout << ans << "\n";
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1043 Longest subarray
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 15:04:33
Judged At
2025-02-17 15:04:33
Judged By
Score
100
Total Time
63ms
Peak Memory
1.305 MiB