zoukankan      html  css  js  c++  java
  • USACO 1.3 Barn Repair

    Barn Repair

    It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full.

    The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width.

    Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.

    Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.

    Print your answer as the total number of stalls blocked.

    PROGRAM NAME: barn1

    INPUT FORMAT

    Line 1: M, S, and C (space separated) Lines 2-C+1: Each line contains one integer, the number of an occupied stall.

    SAMPLE INPUT (file barn1.in)

    4 50 18
    3
    4
    6
    8
    14
    15
    16
    17
    21
    25
    26
    27
    30
    31
    40
    41
    42
    43
    

    OUTPUT FORMAT

    A single line with one integer that represents the total number of stalls blocked.

    SAMPLE OUTPUT (file barn1.out)

    25

    思路: 先按照每个牛棚之间的距离排序,即按照相邻两数之差从大到小排序,然后按照可提供的木块数去掉距离比较大的,看代码

    View Code
     1 /*
     2 ID: shuyang1
     3 PROG: barn1
     4 LANG: C++
     5 */
     6 #include <iostream>
     7 #include <stdio.h>
     8 #include <string.h>
     9 #include <algorithm>
    10 using namespace std;
    11 
    12 struct Node{
    13     int dist;
    14     int value;
    15     int flag;
    16     int no;
    17 };
    18 
    19 struct Node2{
    20     int dist;
    21     int flag;
    22     int no;
    23 };
    24 
    25 bool cmp1(Node a,Node b)
    26 {
    27     return a.value<b.value;
    28 }
    29 
    30 bool cmp2(Node2 a,Node2 b)
    31 {
    32     return a.dist>b.dist;
    33 }
    34 
    35 int main()
    36 {
    37     freopen("barn1.in","r",stdin);
    38     freopen("barn1.out","w",stdout);
    39     Node sc[205];
    40     Node2 comp[205];    //多建一个,是为了排序而不变动原来的结构体组考虑 
    41     int beg,end,mins;
    42     int M,S,C;
    43     int i,j,k;
    44     cin>>M>>S>>C;
    45     for(i=0;i<C;i++)
    46     {
    47         cin>>sc[i].value;
    48         sc[i].flag=0;
    49     }
    50     sort(sc,sc+C,cmp1);
    51     for(i=0;i<C-1;i++)
    52         sc[i].dist=sc[i+1].value-sc[i].value;
    53     sc[i].dist=0;
    54     for(i=0;i<C;i++)
    55     {
    
    56         comp[i].dist=sc[i].dist;
    57         comp[i].no=i;
    58     }
    59     sort(comp,comp+C,cmp2);
    60     M=M<C?M:C;                             //此处卡了好长时间,错在第4个样例; 
    61     for(i=0;i<M-1;i++) sc[comp[i].no].flag=1;
    62     if(sc[C-1].flag!=1) sc[C-1].flag=1;
    63     //for(i=0;i<C;i++) cout<<sc[i].value<<"..."<<sc[i].dist<<"..."<<sc[i].flag<<endl;
    64     beg=sc[0].value;
    65     mins=0;
    66     for(i=0;i<C;i++)
    67     {
    68         if(sc[i].flag==1)
    69         {
    70             mins+=sc[i].value-beg;
    71             beg=sc[i+1].value;
    72         }
    73     }
    74     cout<<mins+M<<endl;       // 最后记得加M; 
    75     return 0;
    76 }
  • 相关阅读:
    我的SWT与数字图像处理总结(1)—重点Image类的介绍
    我的SWT与数字图像处理总结(2)—使用JFreeChart生成灰度直方图
    使用Wiz编写和发布博客园(cnblogs)博客
    RCP之病人信息系统开发总结(2):项目结构分析和重要类分析
    Java与XML(一):采用DOM操作XML文件
    Java实例开发0502 简单的HTTP服务器端
    RCP之病人信息系统开发总结(1):数据库设计
    我的SWT与数字图像处理总结(3)—SWT如何得到图像某个位置的像素值和相应的RGB的值
    对象的保存和载入
    Java与XML(二):使用Dom4j 操作 XML 文件
  • 原文地址:https://www.cnblogs.com/shenshuyang/p/2502655.html
Copyright © 2011-2022 走看看