AcWing data backup

AcWing data backup

Description

  • You are an IT company as large office buildings or office computer data to do a backup.

    However, data backup is tedious work, so you want to design a system to allow different building mutual backup between each other, while you sit at home and enjoy the fun of computer games.

    Known offices are located in the same street, you decided to give these buildings pairing (two a group).

    Each of the building such that they can pass between these two buildings laying cable network can be backed up to each other.

    However, the high cost of network cable.

    Local telecommunications company can only provide you with the K cable network, which means that you can only to K of buildings (a total of 2K office) schedule backups.

    Any of an office building belong to a unique set of pairs (in other words, 2K of a building must be different).

    In addition, Telecom Company was required length of the network cable (Kilometers) charges.

    Thus, you need to choose which K for office makes the total length of the cable as short as possible.

    In other words, you need to choose the K of office, making the distance between each pair of office buildings and (total distance) as small as possible.

    Here's an example, suppose you have five clients, which are office buildings in a street, as shown in FIG.

    The five offices are located at the starting point from the high street 1km, 3km, 4km, 6km and 12km office.

    Telecom companies only provide you with K = 2 cables.

    The best example is the first pairing scheme and a second office connected, is connected to the third and fourth building.

    Thus K = 2 can require the use of cables.

    Article 1 cable length is 3km-1km = 2km, the cable length of the bar 2 is 6km-4km = 2km.

    This scheme requires the total length of 4km pair network cable, and satisfy the minimum distance requirements.

Input

  • The first line of the input integer n and k, where n represents the number of the building, k is the number of available network cable.

    The next n lines contains only one integer s, each represents the distance at the start of the street to the premises.

    These integers will in turn appear in the order from small to large.

Output

  • Should output a positive integers, to give a 2K office connected to a distinct minimum total length k of the network cable required.

Sample Input

5 2 
1
3
4
6
12

Sample Output

4

answer:

  • Binary heap.

  • Wonderful title, fantastic.

  • At the same time, I started writing this question with a pointer tune the two days I have not tune out.(I eat lemons do I Lv. Then the array pointer over analog.

  • First of all I can think of is sure to match the adjacent office building. So the first step to find the distance between two adjacent office, remember d1, d2, d3, ..., d (n - 1). The question then is converted to: make the number of the selected D K and a minimum, and adjacent two numbers can not be simultaneously selected.

  • This title is a scaling problem skills. Think about the case K is the minimum data size. When K = 1, the answer is clearly the value D min. When K = 2, the answer is clear that there are two scenarios:

    1. Selecting a minimum value di, and except that d (i - 1), d (i), min value of the number (i + 1) d is other than
    2. Select two numbers d (i - 1) and on both sides of the minimum di d (i + 1)
    • Reason: It is very easy to understand, I do not know how to explain.
  • So if K = K it? Use problem scaling techniques and mathematical induction , we discovered that a small law is to meet the large-scale data. So we devised an algorithm such: to select the value D min, then remove the d (i - 1), d (i), d (i + 1), and then inserted into d (i - 1) + d (i + 1) - d (i) to a position just. Then Solution "D selected from the new K - 1 Number."

  • Such a design, and if took d (i - 1) + d (i + 1) - d (i) if this number is the second case. I did not get to that first case. This is exactly achieved our above-mentioned two cases.

  • "Select the D min value" This step may be implemented in the stack. But I'm lazy is set instead of the heap.

#include <iostream>
#include <cstdio>
#include <set>
#define N 100005
#define int long long
using namespace std;

struct Node
{
    int val, pos;
    friend bool operator < (Node x, Node y) {
        if(x.val == y.val) return x.pos < y.pos;
        else return x.val < y.val;
    }
};
int n, k, ans;
int d[N], pre[N], nex[N];
set<Node> st;

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

signed main()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i++) d[i] = read();
    for(int i = 1; i < n; i++) pre[i] = i - 1, nex[i] = i + 1;
    pre[n] = n - 1, nex[0] = 1;
    for(int i = 1; i < n; i++)
    {
        d[i] = d[i + 1] - d[i];
        st.insert((Node){d[i], i});
    }
    d[0] = d[n] = (int)1e9;
    for(int i = 1; i <= k; i++)
    {
        int pos = (*st.begin()).pos;
        ans += d[pos], st.erase(st.begin());
        st.erase((Node){d[pre[pos]], pre[pos]});
        st.erase((Node){d[nex[pos]], nex[pos]});
        d[pos] = d[pre[pos]] + d[nex[pos]] - d[pos];
        st.insert((Node){d[pos], pos});
        pre[pos] = pre[pre[pos]], nex[pos] = nex[nex[pos]];
        nex[pre[pos]] = pos, pre[nex[pos]] = pos;
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11361329.html