답안 #25325

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
25325 2017-06-21T07:20:13 Z tlwpdus Palembang Bridges (APIO15_bridge) C++14
100 / 100
139 ms 5824 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

struct median {
    int sz;
    ll sum1, sum2;
    priority_queue<int> pq1;
    priority_queue<int,vector<int>,greater<int> > pq2;
    median() {sz = 0; sum1 = sum2 = 0;}
    void cle() {
        sz = 0; sum1 = sum2 = 0;
        while(!pq1.empty()) pq1.pop();
        while(!pq2.empty()) pq2.pop();
    }
    void push(int a) {
        if (sz&1) {
            if (a<pq1.top()) {
                pq1.push(a);
                sum1 += a;
                pq2.push(pq1.top());
                sum2 += pq1.top();
                sum1 -= pq1.top();
                pq1.pop();
            }
            else {
                sum2+=a;
                pq2.push(a);
            }
        }
        else {
            if (pq1.empty()||a<pq1.top()) {
                sum1+=a;
                pq1.push(a);
            }
            else {
                pq2.push(a);
                sum2+=a;
                pq1.push(pq2.top());
                sum1+=pq2.top();
                sum2-=pq2.top();
                pq2.pop();
            }
        }
        sz++;
    }
    ll getmed() {
        if (pq1.empty()) return 0;
        ll med = pq1.top();
        return ((int)pq1.size()-(int)pq2.size())*med-sum1+sum2;
    }
} md;

int k, n, p;
pii arr[100100];
ll val[2][100100];
ll res;

bool cmp(pii a, pii b) {return a.first+a.second<b.first+b.second;}
int main() {
    int i;

    //freopen("input","r",stdin);

    scanf("%d%d",&k,&n);
    for (i=0;i<n;i++) {
        char a[3], c[3]; int b, d;
        scanf("%s%d%s%d",a,&b,c,&d);
        if (a[0]==c[0]) res += abs(d-b);
        else {
            arr[p++] = pii(b,d);
            res++;
        }
    }
    if (k==1) {
        for (i=0;i<p;i++) {
            md.push(arr[i].first);
            md.push(arr[i].second);
        }
        printf("%lld\n",res+md.getmed());
        return 0;
    }
    sort(arr,arr+p,cmp);
    for (i=0;i<p;i++) {
        md.push(arr[i].first);
        md.push(arr[i].second);
        val[0][i+1] = md.getmed();
    }
    md.cle();
    for (i=p-1;i>=0;i--) {
        val[1][i+1] = md.getmed();
        md.push(arr[i].first);
        md.push(arr[i].second);
    }
    val[1][0] = md.getmed();
    ll mini = (1LL<<60);
    for (i=0;i<=p;i++) mini = min(mini,val[0][i]+val[1][i]);
    printf("%lld\n",res+mini);

    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:68:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&k,&n);
                        ^
bridge.cpp:71:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s%d%s%d",a,&b,c,&d);
                                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4372 KB Output is correct
2 Correct 0 ms 4372 KB Output is correct
3 Correct 0 ms 4372 KB Output is correct
4 Correct 0 ms 4372 KB Output is correct
5 Correct 0 ms 4372 KB Output is correct
6 Correct 0 ms 4372 KB Output is correct
7 Correct 0 ms 4372 KB Output is correct
8 Correct 0 ms 4372 KB Output is correct
9 Correct 0 ms 4372 KB Output is correct
10 Correct 0 ms 4372 KB Output is correct
11 Correct 0 ms 4372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4372 KB Output is correct
2 Correct 0 ms 4372 KB Output is correct
3 Correct 0 ms 4372 KB Output is correct
4 Correct 0 ms 4372 KB Output is correct
5 Correct 0 ms 4372 KB Output is correct
6 Correct 0 ms 4372 KB Output is correct
7 Correct 0 ms 4372 KB Output is correct
8 Correct 0 ms 4372 KB Output is correct
9 Correct 0 ms 4372 KB Output is correct
10 Correct 0 ms 4372 KB Output is correct
11 Correct 0 ms 4372 KB Output is correct
12 Correct 36 ms 5824 KB Output is correct
13 Correct 63 ms 5740 KB Output is correct
14 Correct 49 ms 5740 KB Output is correct
15 Correct 36 ms 5096 KB Output is correct
16 Correct 43 ms 5740 KB Output is correct
17 Correct 53 ms 5824 KB Output is correct
18 Correct 59 ms 5804 KB Output is correct
19 Correct 66 ms 5740 KB Output is correct
20 Correct 36 ms 5740 KB Output is correct
21 Correct 63 ms 5804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4372 KB Output is correct
2 Correct 0 ms 4372 KB Output is correct
3 Correct 0 ms 4372 KB Output is correct
4 Correct 0 ms 4372 KB Output is correct
5 Correct 0 ms 4372 KB Output is correct
6 Correct 0 ms 4372 KB Output is correct
7 Correct 0 ms 4372 KB Output is correct
8 Correct 0 ms 4372 KB Output is correct
9 Correct 0 ms 4372 KB Output is correct
10 Correct 0 ms 4372 KB Output is correct
11 Correct 0 ms 4372 KB Output is correct
12 Correct 0 ms 4372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4372 KB Output is correct
2 Correct 0 ms 4372 KB Output is correct
3 Correct 0 ms 4372 KB Output is correct
4 Correct 0 ms 4372 KB Output is correct
5 Correct 0 ms 4372 KB Output is correct
6 Correct 0 ms 4372 KB Output is correct
7 Correct 0 ms 4372 KB Output is correct
8 Correct 0 ms 4372 KB Output is correct
9 Correct 0 ms 4372 KB Output is correct
10 Correct 0 ms 4372 KB Output is correct
11 Correct 0 ms 4372 KB Output is correct
12 Correct 0 ms 4372 KB Output is correct
13 Correct 0 ms 4372 KB Output is correct
14 Correct 0 ms 4372 KB Output is correct
15 Correct 0 ms 4372 KB Output is correct
16 Correct 0 ms 4372 KB Output is correct
17 Correct 0 ms 4372 KB Output is correct
18 Correct 0 ms 4372 KB Output is correct
19 Correct 0 ms 4372 KB Output is correct
20 Correct 0 ms 4372 KB Output is correct
21 Correct 0 ms 4372 KB Output is correct
22 Correct 0 ms 4372 KB Output is correct
23 Correct 0 ms 4372 KB Output is correct
24 Correct 0 ms 4372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4372 KB Output is correct
2 Correct 0 ms 4372 KB Output is correct
3 Correct 0 ms 4372 KB Output is correct
4 Correct 0 ms 4372 KB Output is correct
5 Correct 0 ms 4372 KB Output is correct
6 Correct 0 ms 4372 KB Output is correct
7 Correct 0 ms 4372 KB Output is correct
8 Correct 0 ms 4372 KB Output is correct
9 Correct 0 ms 4372 KB Output is correct
10 Correct 0 ms 4372 KB Output is correct
11 Correct 0 ms 4372 KB Output is correct
12 Correct 0 ms 4372 KB Output is correct
13 Correct 0 ms 4372 KB Output is correct
14 Correct 0 ms 4372 KB Output is correct
15 Correct 0 ms 4372 KB Output is correct
16 Correct 0 ms 4372 KB Output is correct
17 Correct 0 ms 4372 KB Output is correct
18 Correct 0 ms 4372 KB Output is correct
19 Correct 0 ms 4372 KB Output is correct
20 Correct 0 ms 4372 KB Output is correct
21 Correct 0 ms 4372 KB Output is correct
22 Correct 0 ms 4372 KB Output is correct
23 Correct 0 ms 4372 KB Output is correct
24 Correct 0 ms 4372 KB Output is correct
25 Correct 39 ms 5824 KB Output is correct
26 Correct 93 ms 5740 KB Output is correct
27 Correct 113 ms 5740 KB Output is correct
28 Correct 119 ms 5740 KB Output is correct
29 Correct 139 ms 5740 KB Output is correct
30 Correct 83 ms 5100 KB Output is correct
31 Correct 59 ms 5740 KB Output is correct
32 Correct 99 ms 5824 KB Output is correct
33 Correct 116 ms 5740 KB Output is correct
34 Correct 96 ms 5824 KB Output is correct
35 Correct 59 ms 5824 KB Output is correct
36 Correct 109 ms 5824 KB Output is correct
37 Correct 39 ms 5824 KB Output is correct