Poj 1328 question

Source: Internet
Author: User
Tags radar

Description

 

Given the number of islands (1 <= n <= 1000) and radar radius D (at first I thought it was an integer, but D was actually a decimal ).... The radar can only be on the shore, in a straight line .. The minimum number of radars required to cover all islands... If the number of radars cannot be overwritten, set it to-1 .......... It is actually the island coordinate X, Y (Y> = 0), Radar Coordinate X, 0

 

Algorithm Analysis

 

If you perform various algorithms on the island, it must be slow because the island is a two-dimensional coordinate. In the guidance of the algorithm, the radar itself can be considered in the coordinates. So I thought about it. The image is the interval. The interval with the most duplicates can overwrite the vertices with the most duplicates .. The painting is not good, but in this way, we first sort the X axis of the point to find the range of the radar corresponding to each island on X... From the left-side interval, a new radar is required if the left interval of the next vertex is greater than the current right interval .. If the next right interval is smaller than the current right interval, you must update the current right interval to a smaller value because all vertices must be overwritten ......... Another radar needs to be built at the last point .. Greedy Algorithm: it uses top-down and iterative methods to make successive greedy choices. Each greedy choice simplifies the problem to a smaller subproblem, through greedy selection at each step, you can obtain an optimal solution of the problem without considering the overall optimization. solve each subproblem and obtain the local optimal solution of the subproblem. Every time a radar is obtained, it is closer to the optimal solution .. Baidu has a classic example to analyze [0-1 knapsack problem] There is a backpack with a capacity of M = 150. There are 7 items, which cannot be divided into any size. Make sure that the total value of the items in the backpack is the maximum, but the total capacity cannot be exceeded. Item a B c d e f g weight 35 30 60 40 10 25 value 10 40 30 50 40 40 analysis: objective function: the maximum constraint of Σ pi is that the total weight of the loaded item cannot exceed the size of the backpack: Σ wi <= m (M = 150) (1) According to the greedy policy, is the best result of loading the most valuable items into a backpack? (2) Can I find the optimal solution for loading the item with the minimum weight? (3) Each time the item with the largest unit weight value is selected, it becomes the solution strategy. It is worth noting that the greedy algorithm is not completely unusable. Once proven, the greedy policy is an efficient algorithm. Greedy algorithms are still one of the most common algorithms. This is because they are easy to implement and it is not very difficult to construct greedy policies. Unfortunately, it must be proved before it can be applied to the question algorithm. In general, the proof of the greedy algorithm is centered around: the optimal solution of the entire problem must be obtained from the optimal solution of the sub-problem existing in the greedy policy. The three greedy strategies in the example cannot be established (cannot be proved), and are explained as follows: (1) greedy strategy: select the value owner.Counterexample: W = 30 items: a B C weight: 28 12 12 value: 30 20 20 according to the policy, first select item A, then you cannot select another item. However, it is better to select B and C. (2) greedy policy: minimum weight.Its counterexamples are similar to those of the first policy. (3) greedy policy: select the item with the largest unit weight value.Counterexample: W = 30 items: a B C weight: 28 20 10 value: 28 20 10 according to strategy, three items have the same weight value, and the program cannot make judgment based on the existing strategy, if a is selected, the answer is incorrect. [NOTE: If an item can be divided into any size, Policy 3 can obtain the optimal solution] for selecting the item with the largest unit weight value, you can add an optimization rule: if the unit weight value is the same, the smaller unit weight is preferred! In this way, the above counterexample is solved. However, if the question is as follows, this policy will not work. W = 40 items: a B C weight: 25 20 15 value: 25 20 15 poj rating: 396 K 0 Ms code:
#include<stdio.h>#include<math.h>float a[1001],b[1001];void qsort(int s,int t){    int i,j;    float mid,tmp;    i=s; j=t; mid=a[i];    while (i <= j)    {        while (a[i] < mid)       i++;        while (a[j] > mid)      j--;        if (i <= j)        {   tmp=a[i];   a[i]=a[j];   a[j]=tmp;   tmp=b[i];   b[i]=b[j];   b[j]=tmp;            ++i;            --j;        }    }    if (s<j)      qsort(s,j);    if (i<t)      qsort(i,t); }int main(){    int n,i,x,y,d,leida,t=0;    float right,h;    scanf("%d %d",&n,&d);    while (n > 0 || d > 0)    {        ++t;  leida=0;        for (i=1;i <= n;++i)        {            scanf("%d %d",&x,&y);            if (y > d)    leida=-1;            h=sqrt((float)d*d-y*y);            a[i]=x-h;   b[i]=x+h;        }  if(leida == -1)  {      printf("Case %d: -1\n",t);   scanf("%d%d",&n,&d);   continue;  }        qsort(1,n);        right=b[1];  i=2;        while (i <= n)        {            if (a[i] > right)            {                ++leida;    right=b[i];            }            if (b[i] < right)                right=b[i];            ++i;        }        ++leida;        printf("Case %d: %d\n",t,leida);        scanf("%d %d",&n,&d);    }}

 

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.