結果
| 問題 |
No.615 集合に分けよう
|
| コンテスト | |
| ユーザー |
aim_cpo
|
| 提出日時 | 2017-12-15 18:38:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 21 ms / 2,000 ms |
| コード長 | 1,040 bytes |
| コンパイル時間 | 1,827 ms |
| コンパイル使用メモリ | 176,204 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-14 15:58:00 |
| 合計ジャッジ時間 | 3,082 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 26 |
ソースコード
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define show(x) cout<<#x<<"="<<x<<"\n"
using namespace std;
int n,m;
string s;
ll a[50001];
ll b[50001];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout<<fixed;
#ifdef LOCAL_DEFINE
FILE *stream1;
//FILE *stream2;
stream1=freopen("in", "r", stdin);
//stream2=freopen("out","w",stdout);
if(stream1==NULL)return 0;
//if(stream2==NULL)return 0;
#endif
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
vector<pair<ll,int> > v;
for(int i=1;i<n;i++){
v.push_back(make_pair(a[i]-a[i-1],i));
}
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
for(int i=0;i<m-1;i++){
b[i]=v[i].se;
}
sort(b,b+m-1);
ll ans=0;
int pre=0;
for(int i=0;i<m-1;i++){
ans+=abs(a[pre]-a[b[i]-1]);
pre=b[i];
}
ans+=abs(a[pre]-a[n-1]);
cout<<ans<<"\n";
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
fclose(stream1);
//fclose(stream2);
#endif
return 0;
}
aim_cpo