38 repair bullpen

38  repair bullpen

Author:  xxx Time limit:  1S sections:  one-dimensional array

Problem Description :

In a stormy night, Farmer John roof of the bullpen, the door was blown away. Fortunately, many cattle are on vacation, so the bullpen ( cowshed of the total number of S: 1 <= S <= 200) have not lived. The remaining cattle jostling another was placed in the bullpen lined roof of the night. So some of the cow a cow, some do not.

Cowshed all have the same width, and the width is set to 1. Because some doors missing, Farmer John need to set up a new board as door. His new timber suppliers will supply him any length he wants, but can only provide a limited number of suppliers of wood. Farmer John wants to buy his wood to minimize the total length.

 

Calculating the total length of the minimum required to stop all boards of a cow barn.

Minimum total length of the output board as desired answer.

 

Description: a board stopped length required for a cow, the cow stopped three adjacent planks required length of 3.

For example, there are cows cowshed number:

3 5 8 10 11

And can only use two pieces of wood,

The first piece of wood from 3 to 5, a length of 3,

The second plank from 8 to 11, a length of 4,

Thus, the total length required for the board 7.

 

 

Enter a description:

Line 1: M and C (separated by spaces)

C + 1 to the second line: Each row contains an integer indicating the number of cattle barn occupied.

 

among them:

It may be commercially available wood maximum number of mesh: M (1 <= M < = 50);

Bovine disposed required number C (1 <= C <= S)

After placement of the cowshed where cows number stall_number (. 1 <= stall_number <= S) .

 

Output Description:

It comprises a single line integer required minimum total length of the plank

Input Example:

3 5
2
4
6
8
7

Sample output:

5

 

Problem-solving ideas :( greedy). Beginning no idea, or that there are bad ideas. Violence operation index in the array, feeling confused. Habitually open EXCEL table, look at the number of paintings Photos,

See what the law no. In both cases follows :()

 

 So there is the idea, assume with a long board, a one-time blocked all the doors. It is easy to calculate the required length of the wood. Then we found between some bullpen

There is a large gap, is a waste wood. These statistics out interval, subject of the request in accordance with (a maximum of m is defined using wood, i.e. deleted up to m-1 intervals), greedy

Rule, first interval descending order, each interval of the first big minus, you can obtain a global optimal solution.

 

Complete code:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <ctype.h>
 6 
 7 #define MAXNUM 205
 8 
 9 int rec[MAXNUM] = {0};
10 int interval[MAXNUM] ={0};
11 void mySort(int num[],int len);
12 
13 int main(){
14 
15     int m,c,i,j,n,temp;
16     scanf("%d %d",&m,&c);
17     for(i=0;i<c;i++){
18         scanf("%d",rec+i);
19     }
20     int len = c;
21     //牛棚编号排序
22     mySort(rec,len);
23 
24     int max = rec[len-1] - rec[0] + 1;
25     //最多找到m-1个距离不小于1的间隔,记录找到的间隔并排序
26     int len2=0;//记录数组的长度
27     for(i=1;i<len;i++){
28         temp = rec[i] - rec[i-1]-1;
29         if(temp>0){
30             interval[len2++] = temp;    
31         }
32     }
33     //对间间隔数组排序,排序结果是从小到大,我们要逆序利用
34     
35     mySort(interval,len2);
36     //printf("max= %d\n",max);
37     int count = m-1;//最多可以利用的间隔数
38     //计算最终结果
39     for(i=len2-1;i>=0&&count!=0;i--,count--){
40         max = max - interval[i];
41     }
42 
43     printf("%d\n",max);
44     return 0;
45 }
46 
47 void mySort(int num[],int len){
48     int temp,i,j;
49     for(i=len-2;i>=0;i--){
50         for(j=0;j<=i;j++){
51             if(num[j]>num[j+1]){
52                 temp = num[j];
53                 num[j] = num[j+1];
54                 num[j+1] = temp;
55             }
56         }
57     }
58 }

 

Guess you like

Origin www.cnblogs.com/ManOK/p/12527016.html
38