4 条题解
-
2梁晨熙 (rexliang) LV 9 @ 2022-8-22 20:37:23
#include <iostream> #include <algorithm> using namespace std; int id[210], gap[210]; bool cmp(int x, int y) { return x > y; } int main() { int m, s, c; cin >> m >> s >> c; for (int i = 1; i <= c; i++) { cin >> id[i]; } sort(id+1,id+c+1); // 计算间隔,共c-1个间隔 for (int i = 1; i <= c - 1; i++) { gap[i] = id[i+1] - id[i] - 1; // 两头都不空,所以要 - 1 } sort(gap+1,gap+(c-1)+1,cmp); int len = id[c] - id[1] + 1; // 两头都要覆盖,所以要 + 1 for (int i = 1; i <= m - 1; i++) // 减去间隔 { len -= gap[i]; } cout << len << endl; return 0; }
-
12023-3-12 17:14:46@
-
02024-10-22 16:47:30@
#include <bits/stdc++.h> using namespace std; int id[210], gap[210]; bool cmp(int x, int y) { return x > y; } int main() { int m, s, c; cin >> m >> s >> c; for (int i = 1; i <= c; i++) { cin >> id[i]; } sort(id+1,id+c+1); for (int i = 1; i <= c - 1; i++) { gap[i] = id[i+1] - id[i] - 1; } sort(gap+1,gap+(c-1)+1,cmp); int len = id[c] - id[1] + 1; for (int i = 1; i <= m - 1; i++) len -= gap[i]; } cout << len << endl; return 0; }
-
02023-1-31 17:10:38@
对应:
#include <iostream> #include <algorithm> #include<cstdio> using namespace std; int id[210], gap[210]; bool cmp(int x,int y) { return x>y?true:false; } int main() { int m,s,c; cin>>m>>s>>c; for(int i=1; i<=c; i=i+1)scanf("%d",&id[i]); sort(id+1,id+c+1); for(int i=1; i<c; i=i+1)gap[i]=id[i+1]-id[i]-1; sort(gap+1,gap+c,cmp); int l=id[c]-id[1]+1; for(int i=1; i<m; i=i+1)l=l-gap[i]; cout<<l; return 0; }
- 1
信息
- ID
- 554
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 91
- 已通过
- 41
- 上传者