# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
972954 | 2024-05-01T10:43:51 Z | sleepntsheep | Palembang Bridges (APIO15_bridge) | C | 43 ms | 1944 KB |
#include <stdio.h> #include <stdlib.h> #define MAX_N 100000 int min(int a,int b){return a<b?a:b;} int max(int a,int b){return a>b?a:b;} int cmpr(const void*a,const void*b){return *(const int*)a - *(const int*)b;} int n, k, c[1+MAX_N<<1], p; long long cost, pc[1+MAX_N<<1], z = 1e18; int main() { scanf("%d%d", &k, &n); for (int s, t, i = 0; i < n; ++i) { char bb, cc; scanf(" %c%d %c%d",&bb,&s,&cc,&t); if(bb==cc) cost += abs(t-s); else { c[++p] = s; c[++p] = t; ++cost; } } qsort(c+1, p, sizeof*c, cmpr); if(k==1) { for(int j=1;j<=p;++j) cost += abs(c[p/2] - c[j]); printf("%lld\n", cost); return 0; } /* partial sum on c */ for (int i = 1; i <= p; ++i) pc[i] = pc[i-1] + c[i]; /* some prefix of points will choose left bridge and the rest will choose right bridge * * - fix prefix and find optimal cost for that prefix */ for (int i = 1; i <= p; ++i) { /* c[1..i] will use left bridge, the rest using right bridge */ long long cost1 = 0, j; /* left bridge*/ j = (i+1)/2ll; cost1 += j*c[j] - pc[j]; cost1 += (pc[i] - pc[j]) - c[j]*(i-j); /* right bridge*/ j = (p-i+1)/2+i; cost1 += c[j]*1ll*(j-i) - (pc[j] - pc[i]); cost1 += (pc[p] - pc[j]) - c[j]*1ll*(p-j); if(cost1<z)z=cost1; } printf("%lld",z+cost); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 1 ms | 348 KB | Output is correct |
6 | Correct | 1 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 1 ms | 592 KB | Output is correct |
9 | Correct | 0 ms | 348 KB | Output is correct |
10 | Correct | 1 ms | 348 KB | Output is correct |
11 | Correct | 1 ms | 348 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 344 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 1 ms | 348 KB | Output is correct |
5 | Correct | 1 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Correct | 0 ms | 348 KB | Output is correct |
8 | Correct | 0 ms | 348 KB | Output is correct |
9 | Correct | 1 ms | 440 KB | Output is correct |
10 | Correct | 1 ms | 344 KB | Output is correct |
11 | Correct | 1 ms | 348 KB | Output is correct |
12 | Correct | 24 ms | 1620 KB | Output is correct |
13 | Correct | 43 ms | 1944 KB | Output is correct |
14 | Correct | 33 ms | 1628 KB | Output is correct |
15 | Correct | 24 ms | 1116 KB | Output is correct |
16 | Correct | 28 ms | 1544 KB | Output is correct |
17 | Correct | 26 ms | 1364 KB | Output is correct |
18 | Correct | 26 ms | 1868 KB | Output is correct |
19 | Correct | 35 ms | 1860 KB | Output is correct |
20 | Correct | 28 ms | 1628 KB | Output is correct |
21 | Correct | 28 ms | 1876 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 0 ms | 344 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 0 ms | 348 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 0 ms | 344 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |