BZOJ 1150 Data Backup

https://cn.vjudge.net/problem/HYSBZ-1150

topic

You are an IT company as large office buildings or office buildings (offices) of computer data for 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 to office (or 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 row contains an integer n and k where n (2≤n≤100000) indicates the number of buildings, k (1≤k≤n / 2) represents the number of available network cable. The next n lines contains a single integer (0≤s≤1000000000), to represent each building from the street at the starting point. 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

Greedy, certainly selected adjacent floor, adjacent to the distance thus calculated, into a bunch of numbers selected number K is not adjacent to the minimum and

How have considered two

1. The first minimum is selected, then the remaining time is selected which is not adjacent to a small

2. Do not choose the smallest, the election of two adjacent, if not chose a + next to the other, certainly not as good as other selected minimum +

 

To go back, then you can choose to choose the smallest number becomes $ left + right side - his own $, then the election changed after the election is equivalent to the number of adjacent

 

Since the stack and put together with the list, you can use a set of computer-stack number, the data stored in the heap number, list number directly, and then open a forwarding number subscripted array Duixia

 

AC Code

#include<cstdio>
#include<cctype>
#include<algorithm>
#define REP(r,x,y) for(register int r=(x); r<(y); r++)
#define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...) void(0)
#endif
using namespace std;

int _s; char _c;
template <class T>
void read(T&x) {
	x=0; do _c=getchar(); while(!isdigit(_c) && _c!='-'); _s=1; if(_c=='-') _s=-1, _c=getchar(); while(isdigit(_c)) { x=x*10+_c-'0'; _c=getchar();} x*=_s;
}

#define MAXN 100007
#define MAXK 50007

int dt[MAXN];
int hp[MAXN], sh;
nxt int [MAXN], first [MAXN];





int main() {
	int n,k; read(n); read(k);
	int now,lst; read(lst);
	REP(i,1,n) {
		read(now); int t=now-lst; lst=now;
		dt[i]=t; hp[++sh]=i; th[i]=sh; nxt[i]=i+1, prv[i]=i-1; up(sh);
	}
	int ans=0;
	REP(c,0,k) {
		int x=hp[1];
		ans+=dt[x]; DBG("!%d\n", dt[x]);
		if(prv[x]==0 && nxt[x]==n) break;
		if(prv[x] && nxt[x]!=n) {
			int nd=dt[prv[x]]+dt[nxt[x]]-dt[x];
			remove(th[prv[x]]); remove(th[nxt[x]]); lk(prv[prv[x]],x); lk(x, nxt[nxt[x]]);
			remove(th[x]);
			dt[x]=nd; hp[++sh]=x; th[x]=sh; up(sh);
		} else if(prv[x]) {
			remove(th[prv[x]]); nxt[prv[prv[x]]]=n;
			remove(th[x]);
		} else {
			remove(th[nxt[x]]); prv[nxt[nxt[x]]]=0;
			remove(th[x]);
		}
	}
	printf("%d\n", ans);
}

 

Guess you like

Origin www.cnblogs.com/sahdsg/p/11244471.html