101 Hack 39 B Goodland Electricity locked (线段上的最少点覆盖 贪心)

本文介绍了一个算法问题,即如何利用特定范围内的电力塔为所有城市提供电力覆盖。通过贪心策略确定最少数量的电力塔开启以确保每个城市都能获得电力供应。

 Goodland Electricity locked

Goodland is a country with cities, and each city is sequentially numbered from to . These cities are connected by roads, and each road connects city to its neighboring city, . The distance between any two cities and is .

Goodland's government started a project to improve the country's infrastructure and bring electricity to its citizens. It builtat most one electrical tower in every city, but they haven't turned any of them on yet. Once switched on, each tower produces enough power to provide electricity to all neighboring cities at a distancefrom the tower.

Help the goverment by finding and printing the minimum number of towers they must switch on to ensure that all Goodland's cities have electricity. If this task is not possible for the given value ofand configuration of towers, print .

Input Format

The first line contains two space-separated integers denoting the respective number of cities in Goodland,, and the tower's range constant, .
The second line contains space-separated binary integers where each integer denotes the number of electrical towers in city. Recall that the number of towers in a city will always be either or .

Constraints


  • It is guaranteed that the number of electrical towers in each city will be eitheror .

Subtask

  • forof the maximum score.

Output Format

Print a single integer denoting the minimum number of changes the government must make so that all Goodland's cities have electricity; if this is not possible for the given value of, print .

Sample Input

6 2
0 1 1 1 1 0

Sample Output

2

Explanation

Cities , , , and have towers that can be switched on, and each tower will have a range of once switched on. If we switch on the towers in cities and , then all cities will have electricity. Thus, we print as our answer.


题目链接:https://www.hackerrank.com/contests/101hack39/challenges/pylons

题目大意:打开一个1的点可向左向右覆盖k-1的范围,问最少需要打开几个1的点可以覆盖全部

题目分析:类似POJ 3069那个经典贪心题,本题不是每个点都可以作为圆心,只有值为1的才可以,对于最左边的1若其左边还有大于等于k个0那肯定无解,先贪心得到第一个需要打开的1的点,显然它是第一个点右边且可以覆盖第一个点的最远点,设为r,然后第二个点的取值范围是[r+1, r+k+k-1],因为第一个点可向右覆盖k-1,第二个点可向左覆盖k-1,加上第二点自己,故最右边界为r+k+k-1,后面以此类推,当在这个范围内无解则说明该情况无解,直到当前点r满足r+k-1>=n-1表示全部可以覆盖到即可退出循环

#include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = 1e5 + 5;
int a[MAX], n, k;

int main()
{
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    int r = -1, cnt = 0;
    for(int p = k - 1; p >= 0; p--)
    {
        if(a[p])
        {
            r = p;
            cnt ++;
            break;
        }
    }
    if(r == -1)
    {
        printf("-1\n");
        return 0;
    }
    while(r + k < n)
    {
        bool flag = false;
        for(int p = min(r + k + k, n) - 1; p > r; p--)
        {
            if(a[p])
            {
                flag = true;
                r = p;
                cnt ++;
                break;
            }
        }
        if(!flag)
            break;
    }
    printf("%d\n", r + k >= n ? cnt : -1);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值