1066B - Heaters - CodeForces Solution


greedy two pointers *1500

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main(){
    int n, r;
    int a;
    cin >> n >> r;
    int arr[n];
    int ans = 0;
    int aux;
    vector<int>calentador;
    for(int i=0; i<n; i++){
        cin >> a;
        arr[i] = -1;
        if(a == 1){
            calentador.push_back(i);
        }
    }
    for(int i=0; i<calentador.size(); i++){
        aux = r;
        while(aux > 0){
            if(calentador[i]-aux+1 >= 0){
                arr[calentador[i]-aux+1] = calentador[i];
            }
            if(calentador[i]+aux-1 <= n-1){
                arr[calentador[i]+aux-1] = calentador[i];
            }
            aux--;
        }
        arr[calentador[i]] = calentador[i];
    }
    int ui = 0;
    while(ui<n){
        if(arr[ui] == -1){
            ans = -1;
            break;
        }
        ans++;
        ui = arr[ui] + r;

    }
    /*for(int j=0; j<n; j++){
        if(arr[j] == -1){
            ans = arr[j];
            break;
        }
        ans++;

        for(int i=0; i<calentador.size(); i++){
            if(j < calentador[i]){
                break;
            }
            if(j == calentador[i]){
                ans++;
                j += (r-1);
            }
        }
    }*/
    cout << ans;
}
	 	 				         			 	   				 	


Comments

Submit
0 Comments
More Questions

80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture