CF1110B

Original title link: https: //codeforces.com/problemset/problem/1110/B tdsourcetag = s_pcqq_aiomsg?

You have a long stick, consisting of mm segments enumerated from 11 to mm . Each segment is 11centimeter long. Sadly, some segments are broken and need to be repaired.

You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length tt placed at some position ss will cover segments s, s+1, \ldots, s+t-1s,s+1,,s+t1 .

You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.

Time is money, so you want to cut at most kk continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?

Input and output formats

Input formats:

The first line contains three integers nn , mm and kk ( 1 \le n \le 10^51n105 , n \le m \le 10^9nm109 , 1 \le k \le n1kn ) — the number of broken segments, the length of the stick and the maximum number of pieces you can use.

The second line contains nn integers b_1, b_2, \ldots, b_nb1,b2,,bn ( 1 \le b_i \le m1bim ) — the positions of the broken segments. These integers are given in increasing order, that is, b_1 < b_2 < \ldots < b_nb1<b2<<bn .

Output formats:

Print the minimum total length of the pieces.

Sample input and output

Input Sample # 1:
4 100 2
20 30 75 80
Output Sample # 1:
17
Input Sample # 2:
5 100 3
1 2 4 60 87
Output Sample # 2:
6 


questions intended to summarize:

Give you three numbers n, m, k, denote the number of points, the overall length of the tape, give you n points, to ensure that the number is increasing. Seeking to cover all the points required for the minimum length of tape (tape number <= k)

 

Ideas:

First, it is a question of each dot needs to be covered with
adhesive tape number  K = a case where n, just to cover each point, then the minimum length is n
Otherwise, each will be less representative of a tape two adjacent points use the same tape to cover the need to cover the interval before the two points ( not inclusive, i.e., open interval ) length of  B [ I ] - B [ I - . 1 ] - . 1

Here we will be able to greedy a
difficult to find, as long as each taking the smallest interval until the number of tapes to meet the requirements of
the number of intervals shall be required to take a nk (two consecutive intervals may, for example 1,2,3,10. then take the interval 1-3, and then add the point 10, i.e., the minimum length is 4, since the calculation section is open, so no plus sections before the section is discontinuous points, see the specific code interpreter)

Finally, you can add points n

#include <bits / STDC ++ H.> 
the using namespace STD; 
typedef Long Long LL; 
const int N = 1e6 + 10; 
const LL INF = 0x3f3f3f3f; 
LL n-, m, K, SUM = 0; 
LL A [N] = { } 0; 
LL B [N] = {0}; 

int main () { 
    CIN >> >> m >> n-K; 
    K = NK; // K is the number of intervals required 
    for (int i = 1; i < n-=; I ++) { 
        CIN >> A [I]; 
        B [I-. 1] = A [I] -a [-I. 1] -1; // calculate the length of each interval 
    } 
    Sort (B +. 1 , b + n); // in ascending order (note that only n-1 intervals) 
    for (int I =. 1; I <= K; I ++) { 
		SUM = B + [I]; // covering section 
	} 
    cout << endl << SUM + the n-; // add the final is the final answer to all endpoints 
    return 0; 
}

  

 

 

Guess you like

Origin www.cnblogs.com/clb123/p/10994355.html