이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 2000000
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[MAX_N<<1], p;
long long cost, pc[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;
/* left bridge*/
cost1 += (i/2ll)*c[i/2] - pc[i/2];
cost1 += (pc[i] - pc[i/2]) - c[i/2]*1ll*(i-i/2);
/* right bridge*/
int j = (p-i)/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);
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.c: In function 'main':
bridge.c:14:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d%d", &k, &n);
| ^~~~~~~~~~~~~~~~~~~~~
bridge.c:18:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf(" %c%d %c%d",&bb,&s,&cc,&t);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |