ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#196565 | #2476. 隙间妖怪的式神 | tkswls | 100 | 639ms | 29824kb | C++11 | 802b | 2023-10-28 10:52:41 | 2023-10-28 12:06:05 |
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, a[2000006], sum[2000006];
inline int f(int p) {
return p * (p + 1) / 2;
}
inline int solve(int l, int r) {
int mid = (l + r) >> 1;
return a[mid] * (2 * mid - l - r) - sum[mid - 1] + sum[l - 1] + sum[r] - sum[mid] - f(mid - l) - f(r - mid);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
a[i + n] = a[i] + m;
}
for (int i = 1; i <= 2 * n; i++) {
sum[i] = sum[i - 1] + a[i];
}
int ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 1; i <= n; i++) {
ans = min(ans, solve(i, i + n - 1));
// cout << solve(i, i + n - 1) << endl;
}
cout << ans;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1264kb
input:
12 50 30 6 21 48 43 1 31 18 3 32 37 12
output:
104
result:
ok single line: '104'
Test #2:
score: 10
Accepted
time: 0ms
memory: 1264kb
input:
13 50 16 6 23 27 8 20 40 47 36 13 48 15 18
output:
85
result:
ok single line: '85'
Test #3:
score: 10
Accepted
time: 0ms
memory: 1264kb
input:
7 50 43 31 27 2 38 7 26
output:
56
result:
ok single line: '56'
Test #4:
score: 10
Accepted
time: 0ms
memory: 1308kb
input:
1352 3000 661 387 1173 1226 2825 852 1458 358 328 2620 269 2590 845 1182 1042 421 926 2163 2224 1933...
output:
545816
result:
ok single line: '545816'
Test #5:
score: 10
Accepted
time: 0ms
memory: 1308kb
input:
1368 3000 635 404 2417 2263 1459 1414 1821 2380 2356 1880 489 213 41 1839 1196 439 748 2548 776 2475...
output:
534736
result:
ok single line: '534736'
Test #6:
score: 10
Accepted
time: 0ms
memory: 1348kb
input:
2698 3000 1880 2445 1980 1459 2075 92 1079 1883 993 1710 1938 2015 52 255 872 1081 2920 2611 2471 25...
output:
192416
result:
ok single line: '192416'
Test #7:
score: 10
Accepted
time: 154ms
memory: 29824kb
input:
913743 1000000000 908626452 450856467 440516260 577459640 579677068 623667143 146112603 762467446 12...
output:
213770807528435
result:
ok single line: '213770807528435'
Test #8:
score: 10
Accepted
time: 156ms
memory: 29824kb
input:
913769 1000000000 505034837 316491879 396140520 883841461 41756397 635181982 188691933 12537990 6579...
output:
213721581879336
result:
ok single line: '213721581879336'
Test #9:
score: 10
Accepted
time: 162ms
memory: 29824kb
input:
913786 1000000000 118560062 37767132 39522994 404200499 108209161 642403333 752183117 632006224 4555...
output:
213880847967574
result:
ok single line: '213880847967574'
Test #10:
score: 10
Accepted
time: 167ms
memory: 29824kb
input:
913809 1000000000 436534711 114895846 866464205 806510512 845455358 652519777 252528158 313951134 38...
output:
213730703953361
result:
ok single line: '213730703953361'
Extra Test:
score: 0
Extra Test Passed