Rokua P3620 [APIO/CTSC 2007] Data backup [heap, greedy, differential]

Source: Internet
Author: User

Topic Portal

  

Title Description

You are backing up computer data from a large office or office building (offices) in an IT company. However, the work of data backup is tedious, so you want to design a system that allows different offices to back up each other, while you sit at home and enjoy computer games.

The buildings are known to be located on the same street. You decide to pair these office buildings (two groups). Each pair of office buildings can be backed up by laying a network cable between the two buildings.

However, the cost of a network cable is high. Local telecoms companies can only provide you with a K-bar network cable, which means you can only schedule a backup for K-to-Office (or a total of 2K office buildings). Any office building belongs to the unique pairing group (in other words, the 2K office building must be different).

In addition, telecommunications companies are required to charge the length of the network cable (km). Thus, you need to choose this K to office building to make the total length of the cable as short as possible. In other words, you need to choose this K-pair office building, so that the distance between each office building is as small as possible (the total distance).

An example is given below, assuming that you have 5 customers whose office buildings are in a street, as shown in. The 5 offices are located at 1km, 3km, 4km, 6km and 12km from the main street. The telecom company only supplies you with k=2 cables.

The best pairing scheme in the example above is to connect the 1th and 2nd office buildings, 3rd and 4th office blocks. This allows the k=2 cable to be used as required. The length of the 1th cable is 3km―1km = 2km, and the length of the 2nd cable is 6km―4km = 2 km. This pairing scheme requires a total length of 4km of network cable to meet the minimum distance requirements.

input/output format

Input format:

The first line of the input file contains integers n and K, where n (1≤n≤100 000) represents the number of office buildings, and K (1≤K≤N/2) indicates the number of network cables available.

The next n rows contain only one integer per line (0≤s≤1000 000 000), representing the distance from each office building to the beginning of the main street. These integers will appear in order from small to large.

Output format:

The output file should consist of a positive integer that gives the minimum total length of the network cable required to connect a 2K different office building to a K pair.

input/Output sampleInput Sample # #: Copy
Output Example # #: Replication
4
Description

30% of input data satisfies n≤20.

60% of input data satisfies N≤10 000

Analysis: Easy to license, the best Jianzhong pairing of the building must be adjacent, you can each of the two adjacent buildings between the distance di to find out, you can transform the problem into: [There are n-1 elements of the series Di, from which the number of k, so that the K number and the smallest, and the number of adjacent can not be selected simultaneously].

Let's start with an analysis of the simple situation:

If k=1, then select the smallest element directly.

If k=2, then either choose the smallest element d[i] and the smallest element except d[i-1],d[i],d[i+1], or select the elements on either side of D[i] (D[i-1] and d[i+1]

Then from the second case can be analyzed, for the current sequence of elements, or must choose the smallest element d[i], or must choose the smallest element on both sides of the element d[i-1],d[i+1]. In other words, the elements on the left and right sides of the smallest element are either selected at the same time or not selected. You can then select the smallest element in d[] d[i], and then remove d[i-1],d[i],d[i+1] from the sequence and insert a new element D[i-1]+d[i+1]-d[i] in the original position, and then repeat the operation. (because if D[i-1]+d[i+1]-d[i] is selected in the subsequent operation, it is equivalent to removing d[i in the selected result) and d[i-1]+d[i+1], if the element is not selected later, then D[i] is the one-step optimal strategy)

The algorithm can also be deduced: the establishment of a linked list L and a small Gan h,l has n-1 elements, each element and the small root heap each element into a one by one mapping relationship, their elements are d[i], that is, the distance between each two buildings. Remove the heap top, add weights to the answer, and then delete the heap top x, while the left and right side of the x element pre[x],nxt[x] Delete, insert a new element in the same position p,p the weight of the value d[p]=d[pre[x]]+d[nxt[x]]-d[x], Then insert the corresponding node p in the corresponding position of the linked list. Repeat k times.

However, the actual operation of the time there are many details to pay attention to the details of the code bar.

Code:

  

1 //It is made by Holselee on 10th May 20182 //luogu.org P36203#include <bits/stdc++.h>4 #defineFi (I,A,B) for (int i=a;i<=b;i++)5 using namespacestd;6 Const intn=2e5+7;7 intN,k,size,heap[n],fheap[n];8 intNum[n],nxt[n],pre[n],ans;9InlineintRead ()Ten { One   CharCh=getchar ();intnum=0;BOOLflag=false; A    while(ch<'0'|| Ch>'9'){if(ch=='-') flag=true; ch=GetChar ();} -    while(ch>='0'&&ch<='9') {num=num*Ten+ch-'0'; ch=GetChar ();} -   returnflag?-Num:num; the } -InlinevoidDelet (intx) - { -heap[x]=heap[size--]; +Fheap[heap[size+1]]=x; -   intKa=x; +   if(num[heap[ka]]>=num[heap[ka>>1]]){ A     ints=ka<<1; at      while(s<=size) { -       if(s<size&&num[heap[s+1]]<num[heap[s]]) s++; -       if(num[heap[ka]]>Num[heap[s]]) { - swap (Fheap[heap[ka]],fheap[heap[s]]); -Swap (Heap[ka],heap[s]);ka=s;s=ka<<1;} -       Else  Break;}} in   Else{ while(ka>1){if(num[heap[ka]]<num[heap[ka>>1]]){ -Swap (fheap[heap[ka]],fheap[heap[ka>>1]]); toSwap (heap[ka],heap[ka>>1]); ka>>=1;} +       Else  Break;}} - } theInlinevoidInsertintx) * { $Heap[++size]=x;fheap[x]=size;intKa=size;Panax Notoginseng    while(ka>1){ -     if(num[heap[ka]]<num[heap[ka>>1]]) the{Swap (fheap[heap[ka]],fheap[heap[ka>>1]]); +Swap (heap[ka],heap[ka>>1]); ka>>=1;} A     Else  Break;} the } + intMain () - { $N=read (); K=read ();intx1,x2; $X1=read (); Fi (I,2, N) { -nxt[i]=i+1;p re[i]=i-1; x2=read (); -num[i]=x2-x1;x1=X2;insert (i);} thenum[1]=1e9+7; num[++n]=1e9+7; -Insert1), insert (n);WuyiFi (I,1, K) { the     intx=heap[1];d Elet (1); - delet (Fheap[pre[x]]);d Elet (fheap[nxt[x]); Wu     intX1=num[pre[x]],x2=Num[nxt[x]]; -num[++n]=x1+x2-Num[x]; Aboutpre[n]=pre[pre[x]];nxt[n]=Nxt[nxt[x]]; $pre[nxt[n]]=n;nxt[pre[n]]=N;insert (n); -ANS+=NUM[X];} cout<<ans;return 0; -}

Rokua P3620 [APIO/CTSC 2007] Data backup [heap, greedy, differential]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.