답안 #973041

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973041 2024-05-01T12:48:29 Z sleepntsheep Palembang Bridges (APIO15_bridge) C
100 / 100
126 ms 11552 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 n, k, s[MAX_N], t[MAX_N], e[MAX_N], o, temp;long long pref[MAX_N];
int cmpr2(const void*a,const void*b){int i=*(const int*)a,j=*(const int*)b;long long dir=(s[i]+t[i]+0ll)-(0ll+s[j]+t[j]);return dir<0?-1:dir>0?1:0;}
long long cost, z = 1e18;

#define MM 800003
int ha[MM],hl[MM],hr[MM],halc=0;char hs[MM];
int lt(int a,int b){return a<b;}
int gt(int a,int b){return a>b;}
int hmerge(int l,int r,int(*lt)(int,int))
{
    if(!l||!r)return l^r;
    if(lt(ha[l],ha[r]))return hmerge(r,l,lt);
    hr[l]=hmerge(hr[l],r,lt);
    if(hs[hr[l]]>hs[hl[l]])temp=hr[l],hr[l]=hl[l],hl[l]=temp;
    hs[l]=hs[hr[l]]+1;
    return l;
}
int hpop(int v,int(*lt)(int,int)) { return hmerge(hl[v],hr[v], lt); }
int hnode(int k){int p=++halc;hs[p]=1;hl[p]=hr[p]=0;ha[p]=k;return p;}
int hinsert(int v,int k,int(*lt)(int,int)){return hmerge(v,hnode(k),lt);}

/* sliding median */
int lo, hi, losz, hisz;
long long losum,hisum;

void ins(int x)
{
    if(losz>0&&ha[lo]<x)
        hi=hinsert(hi,x,gt), ++hisz,hisum+=x;
    else
        lo=hinsert(lo,x,lt), ++losz,losum+=x;
    if(losz<hisz)
        hisum-=ha[hi],losum+=ha[hi],lo=hinsert(lo,ha[hi],lt),hi=hpop(hi,gt),--hisz,++losz;
    else if(hisz+1<losz)
        losum-=ha[lo],hisum+=ha[lo],hi=hinsert(hi,ha[lo],gt),lo=hpop(lo,lt),--losz,++hisz;
}

int main()
{
    scanf("%d%d", &k, &n);
    for (int ss, tt, i = 0; i < n; ++i)
    {
        char bb, cc;
        scanf(" %c%d %c%d",&bb,&ss,&cc,&tt);
        if(bb==cc) cost += abs(tt-ss);
        else s[o]=min(ss,tt),t[o]=max(ss,tt),e[o]=o,++cost,++o;
    }

    qsort(e,o,sizeof*e,cmpr2);

    for(int i=0;i<o;++i)
        ins(s[e[i]]),ins(t[e[i]]), pref[i] = hisum-losum;
    if(k==1){printf("%lld\n",hisum-losum+cost);return 0;}

    hi=lo=hisz=losz=hisum=losum=0;
    for(int i=o-1;i>=1;--i)
    {
        ins(s[e[i]]),ins(t[e[i]]);
        if(hisum-losum+pref[i-1]<z)z=hisum-losum+pref[i-1];
    }
    if(pref[o-1]<z)z=pref[o-1];

    printf("%lld",z+cost);
}

Compilation message

bridge.c: In function 'main':
bridge.c:47:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     scanf("%d%d", &k, &n);
      |     ^~~~~~~~~~~~~~~~~~~~~
bridge.c:51:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         scanf(" %c%d %c%d",&bb,&ss,&cc,&tt);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 45 ms 11032 KB Output is correct
13 Correct 70 ms 10952 KB Output is correct
14 Correct 61 ms 10748 KB Output is correct
15 Correct 40 ms 10088 KB Output is correct
16 Correct 40 ms 10896 KB Output is correct
17 Correct 75 ms 11140 KB Output is correct
18 Correct 26 ms 11160 KB Output is correct
19 Correct 79 ms 11136 KB Output is correct
20 Correct 50 ms 11036 KB Output is correct
21 Correct 68 ms 11100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 4552 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4544 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4552 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 2 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 56 ms 11456 KB Output is correct
26 Correct 83 ms 11284 KB Output is correct
27 Correct 103 ms 11196 KB Output is correct
28 Correct 111 ms 11380 KB Output is correct
29 Correct 106 ms 11196 KB Output is correct
30 Correct 53 ms 10240 KB Output is correct
31 Correct 54 ms 11148 KB Output is correct
32 Correct 110 ms 11552 KB Output is correct
33 Correct 44 ms 11364 KB Output is correct
34 Correct 126 ms 11508 KB Output is correct
35 Correct 71 ms 11516 KB Output is correct
36 Correct 111 ms 11344 KB Output is correct
37 Correct 46 ms 11452 KB Output is correct