cf1066b Heaters (Greedy)

Source: Internet
Author: User

Test Instructions Description:

VovaMister's home can be seen as aNx1The rectangle, and the cold winter came,VovaMr. Wang wants his home to get warmer. Now we give youVovaThe floor plan of the gentleman's house, which111 means this place is a heating furnace, and 0 means there is nothing in this place. All heaters have a heating radiusR, one inAiHeaters can be heated [Ai−r+1,ai+r−1] Range. Right now Mr. Vova wanted his whole family to get warmer, and all the heaters were off at first, so please find out that Mr. Vova has to open a few heaters at least to keep the whole house warm .

Input and output format: input format:

First row: two integers n,r (1≤n,r≤1000) < Span class= "Mrel" > , meaning above

The second row, n integers, represents the map of the Vova home

Output format:

An integer representing Mr. Vova to open at least a few heaters

Ideas:

It's still very water.

Because we want as little as possible, so we have to use greedy thought

We sweep from the far left, sweep to a point that has not been heated, and we sweep right from this point, sweep to a heater that can heat his farthest from him. Open

(The correctness of greed is that the heater is farthest from him and can heat it up, if the closer is not better, choose farther will not heat him)

Then mark the points in the range where the heater can be heated and continue to sweep down

Worst time complexity O (n^2)

Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineRii Register int i#defineRij Register Int Jusing namespacestd;intn,r,jz[4005],jrq[4005],bj[4005],ans;intMain () {intCnt=0; scanf ("%d%d",&n,&R);  for(rii=1; i<=n;i++) {scanf ("%d",&Jz[i]); if(jz[i]==1) {CNT++; JRQ[CNT]=i; }    }     for(rii=1; i<=n;i++)    {        if(bj[i]==0)        {            intwz=0;  for(rij=1; j<=r;j++)            {                if(i+j-1<=N) {if(jz[i+j-1]==1) {wz=max (wz,i+j-1); }                }                if(i-j+1>=1)                {                    if(jz[i-j+1]==1) {wz=max (wz,i-j+1); }                }            }             for(rij=1; j<=r;j++)            {                if(wz-j+1>0) {Bj[wz-j+1]=1; } bj[wz+j-1]=1; } ans++; if(wz==0) {printf ("-1"); return 0; }}} printf ("%d", ans);}

cf1066b Heaters (Greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.