USACO Section 1.3: Barn Repair

 1 /*

 2 ID: leetcod3

 3 PROG: barn1

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 #include <limits>

21 #include <stack>

22 

23 using namespace std;

24 

25 ofstream fout ("barn1.out");

26 ifstream fin ("barn1.in");

27 

28 int main() {

29     int M, S, C;

30     fin >> M >> S >> C;

31     vector<int> barn(C);

32     for (int i = 0; i < C; i++) fin >> barn[i];

33     sort(barn.begin(), barn.end());

34     int ans = barn[C-1] - barn[0] + 1;

35     vector<int> diff(C-1);

36     for (int i = 0; i < C-1; i++) diff[i] = barn[i+1] - barn[i];

37     sort(diff.begin(), diff.end());

38     if (M >= C) ans = C;

39     else for (int i = C-2; i > C-2-M+1; i--) ans -= diff[i] - 1;

40     fout << ans << endl;

41     return 0;

42 }

 

你可能感兴趣的:(USACO)