zoukankan      html  css  js  c++  java
  • 贪心:SPOJ Backup Files

    BACKUP - Backup Files

    no tags 

    You run an IT company that backs up computer data for large offices. Backing up data is not fun, and so you design your system so that the different offices can back up each others' data while you sit at home and play computer games instead.

    The offices are all situated along a single street. You decide to pair up the offices, and for each pair of offices you run a network cable between the two buildings so that they can back up each others' data.

    However, network cables are expensive. Your local telecommunications company will only give you k network cables, which means you can only arrange backups for k pairs of offices (2k offices in total). No office may belong to more than one pair (that is, these 2k offices must all be different). Furthermore, the telecommunications company charges by the kilometre. This means that you need to choose these k pairs of offices so that you use as little cable as possible. In other words, you need to choose the pairs so that, when the distances between the two offices in each pair are added together, the total distance is as small as possible.

    As an example, suppose you had five clients with offices on a street as illustrated below. These offices are situated 1 km, 3 km, 4 km, 6km and 12km from the beginning of the street. The telecommunications company will only provide you with k = 2 cables.

    The best pairing in this example is created by linking the first and second offices together, and linking the third and fourth offices together. This uses k = 2 cables as required, where the first cable has length 3km - 1km = 2 km, and the second cable has length 6km - 4km = 2 km. This pairing requires a total of 4km of network cables, which is the smallest total possible.

    Input

    Multiple test cases, the number of them will be given at the very first line.

    For each test case:

    The first line of input will contain the integers n and k, representing the number of offices on the street (2 <= n <= 100 000) and the number of available network cables (1 <= k <= n/2).

    The following n lines will each contain a single integer (0 <= s <= 1 000 000 000), representing the distance of each office from the beginning of the street. These integers will appear in sorted order from smallest to largest. No two offices will share the same location.

    Output

    Output should consist of a single positive integer, giving the smallest total length of network cable required to join 2k distinct offices into k pairs.

    Example

    Input:
    1
    5 2
    1
    3
    4
    6
    12
    
    Output:
    4
    
    Explanation

    The sample input above represents the example scenario described earlier.

    Warning: large input/output data,be careful with certain languages

    Blue Mary's Note: test data has been modified on Dec. 5, 2007. All the solutions have been rejudged.

      这题有个性质,就是你要匹配的任意两对点的连线不能重合, 否则就不是最优的了。利用这个性质去贪心解题。

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <queue>
     5 #include <vector>
     6 using std::priority_queue;
     7 using std::vector;
     8 typedef long long LL;
     9 struct A{
    10     int l, r, pos;
    11        LL dis;
    12 };
    13 const int Maxn = 100005;
    14 int s[Maxn], mark[Maxn], tot;
    15 A l[Maxn];
    16 struct cmp{
    17     bool operator ()(A a,A b){
    18         return a.dis > b.dis;
    19     }
    20 };
    21 int main(){
    22 #ifndef ONl_JUDGE
    23     freopen ("backup.in", "r", stdin);
    24     freopen ("backup.out", "w", stdout);
    25 #endif
    26     int n, k;
    27     scanf ("%d%d", &n, &k);
    28     priority_queue<A, vector<A>, cmp> q;
    29     for (int i = 1; i <= n; ++i)
    30         scanf ("%d", s+i);
    31     for(int i=1;i<n;i++)
    32     {
    33         l[i].dis = s[i+1]-s[i];
    34         l[i].pos = i;
    35         l[i].l = i-1;
    36         l[i].r = i+1;
    37     }
    38     l[n-1].r=0;
    39     for(int i=1;i<n;i++)
    40         q.push(l[i]);
    41     LL ans = 0;
    42     while (!q.empty()){
    43         A a = q.top();
    44         q.pop();
    45         if (mark[a.pos]) continue;
    46         ans += l[a.pos].dis;
    47         if (--k == 0) break;
    48         LL w = -l[a.pos].dis;
    49         if (l[a.pos].l){
    50             w += l[l[a.pos].l].dis;
    51             mark[l[l[a.pos].l].pos] = 1;
    52             l[a.pos].l = l[l[a.pos].l].l;
    53             if(l[a.pos].l)
    54                 l[l[a.pos].l].r = a.pos;
    55         }
    56         else
    57             mark[a.pos] = 1;   
    58         if (l[a.pos].r){
    59             w += l[l[a.pos].r].dis;
    60             mark[l[l[a.pos].r].pos] = 1;
    61             l[a.pos].r = l[l[a.pos].r].r;
    62             if(l[a.pos].r)
    63                 l[l[a.pos].r].l = a.pos;
    64         }
    65         else
    66             mark[a.pos] = 1;
    67         if (mark[a.pos] == 1){
    68             if (l[a.pos].r) l[l[a.pos].r].l = 0;
    69             if (l[a.pos].l) l[l[a.pos].l].r = 0;
    70         }
    71         else{
    72             l[a.pos].dis = w;
    73             q.push(l[a.pos]);
    74         }
    75     }
    76     printf ("%lld", ans);
    77     return 0;
    78 }

    尽最大的努力,做最好的自己!
  • 相关阅读:
    Android
    十大基础有用算法之迪杰斯特拉算法、最小生成树和搜索算法
    【随想】android是个什么东西,andorid机制随想
    【Unity3D】【NGUI】Atlas的动态创建
    Java集合01----ArrayList的遍历方式及应用
    JAVA线程
    VC++的project文件
    selector的button选中处理问题
    单元測试和白盒測试相关总结
    leetCode(40):Path Sum
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5251206.html
Copyright © 2011-2022 走看看