/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);
using namespace std;
int a[200001];
signed main(){
int n,k, ans = 0; cin >> n >> k;
for(int i = 0; i < n; i++) cin >> a[i];
sort(a,a+n);
vector<pair<int,int>>d;
set<int>x;
for(int i = 0; i < n - 1; i++){
d.push_back({a[i + 1] - a[i], i});
}
sort(d.begin(), d.end());
reverse(d.begin(), d.end());
for(int i = 0; i < k - 1; i++){
x.insert(d[i].second);
}
int beg = INT_MAX, ed = 0;
for(int i = 0; i < n; i++){
beg = min(beg,a[i]);
ed = max(ed,a[i]);
if(x.count(i)){
ans += (ed - beg + 1);
beg = INT_MAX; ed = 0;
}
}
ans += (ed - beg + 1);
cout << ans;
return 0;
}
Compilation message
stove.cpp:13: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
13 | #pragma GCC optimization ("O3")
|
stove.cpp:14: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
14 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
49 ms |
4168 KB |
Output is correct |
17 |
Correct |
49 ms |
4160 KB |
Output is correct |
18 |
Correct |
50 ms |
4168 KB |
Output is correct |
19 |
Correct |
56 ms |
4196 KB |
Output is correct |
20 |
Correct |
73 ms |
5916 KB |
Output is correct |
21 |
Correct |
75 ms |
7820 KB |
Output is correct |
22 |
Correct |
93 ms |
8364 KB |
Output is correct |
23 |
Correct |
82 ms |
8196 KB |
Output is correct |
24 |
Correct |
96 ms |
8248 KB |
Output is correct |