zoukankan      html  css  js  c++  java
  • POJ 3253 Fence Repair 贪心

    Fence Repair
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 76946   Accepted: 25167

    Description

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

    FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

    Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

    Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

    Input

    Line 1: One integer N, the number of planks 
    Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

    Output

    Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

    Sample Input

    3
    8
    5
    8

    Sample Output

    34

    Hint

    He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
    The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

    Source

     
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 typedef long long LL;
     8 
     9 const int max_n = 2e4+10;
    10 
    11 int n;
    12 LL a[max_n];
    13 
    14 void solve()
    15 {
    16     // sort(a,a+n);
    17 
    18     int left=0;
    19     LL ans=0;
    20     int first_min,second_min;
    21     while(left<n-1)
    22     {
    23         // 初始化最小的数和次小数
    24         first_min=left;
    25         second_min=left+1;
    26         if(a[left]>a[left+1])
    27         {
    28             swap(a[left],a[left+1]);
    29         }
    30 
    31         /*printf("left: %d 
    ",left);
    32         for(int i=0;i<n;++i)
    33         {
    34             printf("%lld ",a[i]);
    35         }
    36         printf("
    ");
    37 
    38         printf("%lld %lld  ",a[left],a[left+1]);*/
    39 
    40         for(int i=left+2;i<n;++i)
    41         {
    42             // 更新最小数
    43             if(a[i]<a[first_min])
    44             {
    45                 //printf("没有");
    46                 second_min=first_min;
    47                 first_min=i;
    48             }
    49             // 跟新次小数
    50             else if(a[i]<a[second_min])
    51             {
    52                 //printf("NO");
    53                 second_min=i;
    54             }
    55         }
    56 
    57         // 进行叠加操作
    58         LL t=a[first_min]+a[second_min];
    59         ans+=t;
    60         //printf("%lld %lld %lld
    ",a[first_min],a[second_min],t);
    61 //        if(first_min==left)
    62 //        {
    63 //            a[second_min]=t;
    64 //            ++left;
    65 //        }
    66 //        else
    67 //        {
    68 //            a[first_min]=a[left];
    69 //            a[second_min]=t;
    70 //            ++left;
    71 //        }
    72         if(second_min==left)
    73         {
    74             swap(first_min,second_min);
    75         }
    76         a[first_min]=a[left];
    77         a[second_min]=t;
    78         ++left;
    79     }
    80     printf("%lld
    ",ans);
    81 }
    82 
    83 int main()
    84 {
    85     scanf("%d",&n);
    86     for(int i=0;i<n;++i)
    87     {
    88         scanf("%lld",&a[i]);
    89     }
    90     solve();
    91     return 0;
    92 }
    93 
    94 /*
    95 5
    96 1 2 3 4 5
    97 
    98 */
  • 相关阅读:
    iOS 键盘类型定制归纳
    CocoaPods安装第三方出错:XCode7.3
    NSIntger CGFloat NSNumber
    iOS 关于使用xib创建cell的两种初始化方式
    自定义导航栏--用法一
    CocoaPods的安装[转载]
    pch和info.plist初探
    iOS_XCode7_Launch Image 的初使用
    nginx四层负载及动静分离
    Nginx负载均衡
  • 原文地址:https://www.cnblogs.com/jishuren/p/12253258.html
Copyright © 2011-2022 走看看