-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarn1.cpp
73 lines (66 loc) · 1.43 KB
/
barn1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
ID: bbsunch2
PROG: barn1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int main()
{
ofstream fout ("barn1.out");
ifstream fin ("barn1.in");
int boardNum = 0;
int stallNum = 0;
int cowStall = 0;
fin >> boardNum >> stallNum >> cowStall;
//cout << boardNum << stallNum << cowStall << endl;
vector <int> stall;
vector<int> gap;
for(int i = 0; i < cowStall; i++)
{
int s;
fin >> s;
stall.push_back(s);
}
sort(stall.begin(), stall.end());
int former = 0;
former = stall[0];
for(int i = 1; i < stall.size(); i++)
{
int latter = stall[i];
if(latter - former > 1)
{
//cout << former << " " << latter << endl;
gap.push_back(latter - former - 1);
}
former = latter;
}
//cout << gap.size() << endl;
//for(int i = 0; i < gap.size(); i++)
//{
// cout << gap[i] << endl;
//}
sort(gap.begin(), gap.end());
//cout << "test" << endl;
//cout << gap.size() << endl;
int gapNum = boardNum - 1;
if(gapNum > gap.size())
{
gapNum = gap.size();
}
for(int i = 0; i < gapNum; i++)
{
gap.erase(gap.end()-1);
}
for(int i = 0; i < gap.size(); i++)
{
cowStall += gap[i];
}
fout << cowStall << endl;
return 0;
}