B. Heaters thinking title greedy interval coverage

B. Heaters

Although only a fraction of this topic of 1500, but I still feel very thought, I did not write it today, then I looked at the problem solution

Rarely do this interval coverage of the subject, is not very good, then talk about the idea after I read the explanations.

Title effect that: give you a number of columns, the number of columns 0 means the place no heaters, 1 representatives, each heater is a range [i-r + 1, i + r-1]

Ask you to use the least number of heaters for the whole series are heated.

We can have a heater into a queue, and with a number represented haves [. 1, haves] range has been covered, then use a few now be expressed according to a point

This is a position the farthest point you can choose, if can not find the farthest point of it means not meet the requirements of coverage.

#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <bitset>
#include <algorithm>
#include <map>
#include <vector>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;

int main()
{
    int n, r;
    queue<int>que;
    scanf("%d%d", &n, &r);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d", &x);
        if (x) que.push(i);
    }
    int haves = 0, now = r, ans = 0;
    while(!que.empty())
    {
        int u = que.front(); que.pop();
        if (haves >= n) break;
        if (u > now) {
            printf("-1\n");
            return 0;
        }
        if(!que.empty())
        {
            int v = que.front();
            if (v <= now) continue;
            ans++;
            haves = u + r - 1;
            now = u + 2 * r - 1;
        }
        else {
            ans++;
            haves = u + r - 1;
            now = u + 2 * r - 1;
        }
    }
    if (haves >= n) printf("%d\n", ans);
    else printf("-1\n");
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/EchoZQN/p/11317280.html