zoukankan      html  css  js  c++  java
  • CodeForces Round #515 Div.3 B. Heaters

    http://codeforces.com/contest/1066/problem/B

    Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The ii-th element of the array is 11 if there is a heater in the position ii, otherwise the ii-th element of the array is 00.

    Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position pospos can warm up all the elements in range [posr+1;pos+r1][pos−r+1;pos+r−1].

    Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.

    Vova's target is to warm up the whole house (all the elements of the array), i.e. if n=6n=6, r=2r=2 and heaters are at positions 22 and 55, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 elements will be warmed up by the second heater).

    Initially, all the heaters are off.

    But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.

    Your task is to find this number of heaters or say that it is impossible to warm up the whole house.

    Input

    The first line of the input contains two integers nn and rr (1n,r10001≤n,r≤1000) — the number of elements in the array and the value of heaters.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai10≤ai≤1) — the Vova's house description.

    Output

    Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

    Examples
    input
    Copy
    6 2
    0 1 1 0 0 1
    output
    Copy
    3
    input
    Copy
    5 3
    1 0 0 0 1
    output
    Copy
    2
    input
    Copy
    5 10
    0 0 0 0 0
    output
    Copy
    -1
    input
    Copy
    10 3
    0 0 1 1 0 1 0 0 0 1
    output
    Copy
    3

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int N, R;
    int pos[1010];
    
    int main() {
      scanf("%d%d", &N, &R);
      for(int i = 0; i < N; i ++)
        scanf("%d", &pos[i]);
      
      vector<int> T;
      int st = -1;
      for(int i = N - 1; i >= 0; i --) {
        if(pos[i] == 1 && i < R) {
          st = i;
          break;
        }
      }
      
      if(st == -1) {
        printf("-1
    ");
        return 0;
      }
      
      if(N - st <= R) {
        printf("1
    ");
        return 0;
      }
      
      T.push_back(st);
      
      while(true) {
       // cout << "!!!" << endl;
        bool flag = false;
        for(int i = N - 1; i >= st + 1; i --) {
          if(i - st + 1 <= 2 * R && pos[i]) {
            st = i;
            flag = true;
            break;
          }
        }
        if(!flag) {
          break;
        }
        
        T.push_back(st);
        
        if(N - st <= R) {
          printf("%d
    ", T.size());
          return 0;
        }
      }
      
      printf("-1
    ");
      
      return 0;
    }
    

      

  • 相关阅读:
    原生js封装二级城市下拉列表
    jsonp帮助你知道你关注的他或她喜欢什么歌曲
    sql 进制转换,支持93内的进制相互转换
    linux下批量删除文件
    Ubuntu环境下golang环境搭建
    centos6.5 修改网络配置
    elasticsearch(二)
    Redis
    Spring Data JPA
    elasticsearch(一)
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9943156.html
Copyright © 2011-2022 走看看