답안 #14383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
14383 2015-05-13T15:17:13 Z tncks0121 Palembang Bridges (APIO15_bridge) C++14
100 / 100
156 ms 9836 KB
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <time.h>
#include <limits.h>
 
using namespace std;
 
typedef long long ll;
typedef double lf;
 
const int N_ = 100050;
 
int K, N;
 
ll sum_of_same_part;
ll S[N_], T[N_];
 
struct ele {
    int w, x;
    ele (int w = 0, int x = 0): w(w), x(x) { }
    bool operator < (const ele &e) const { return x < e.x; }
} E[N_+N_]; int EN;
 
ll ans;
 
void solve1() {
    int x = E[EN/2].x;
    for(int i = 1; i <= EN; i++) ans += abs(x - E[i].x);
}
 
priority_queue<ll> pq1;
priority_queue<ll, vector<ll>, greater<ll> > pq2;
ll sum_pq1, sum_pq2;
 
 
int A[N_];
bool cmpA (const int &a, const int &b) {
  return S[a] + T[a] < S[b] + T[b];
}
 
void pq_ins (ll v) {
    if(!pq1.empty() && v > pq1.top()) pq2.push(v), sum_pq2 += v;
    else pq1.push(v), sum_pq1 += v;
   
    int md = (pq1.size() + pq2.size() + 1) / 2;
   
    while(pq1.size() > md) {
      sum_pq1 -= pq1.top();
      sum_pq2 += pq1.top();
      pq2.push(pq1.top());
      pq1.pop();
    }
    while(pq1.size() < md) {
      sum_pq2 -= pq2.top();
      sum_pq1 += pq2.top();
      pq1.push(pq2.top());
      pq2.pop();
    } 
}
 
ll tb1[N_];
ll tb2[N_];
 
void solve2() {
  ans = (ll)1e18;
  if(EN == 0) { ans = 0; return; }
   
  for(int i = 1; i <= N; i++) A[i] = i;
  sort(A + 1, A + N + 1, cmpA);
   
  for(int i = 1; i <= N; i++) {
    // pq1 : max heap, pq2 : min heap
    // max(pq1) < min(pq2)
     
    int x = A[i];
     
    pq_ins(S[x]);
    pq_ins(T[x]);
     
    ll m = pq1.top();
    tb1[i] = (m * pq1.size() - sum_pq1) + (sum_pq2 - m * pq2.size());
  }
   
  while(!pq1.empty()) pq1.pop(); sum_pq1 = 0;
  while(!pq2.empty()) pq2.pop(); sum_pq2 = 0;
   
  for(int i = N; i >= 1; i--) {
    // pq1 : max heap, pq2 : min heap
     
    int x = A[i];
     
    pq_ins(S[x]);
    pq_ins(T[x]);
     
    ll m = pq1.top();
    tb2[i] = (m * pq1.size() - sum_pq1) + (sum_pq2 - m * pq2.size());
  }
   
  for(int i = 1; i <= N; i++) {
    ans = min(ans, tb1[i] + tb2[i + 1]);
  }
}
 
int main() {
    int M; scanf("%d%d", &K, &M);
    for(; M--; ) {
        char p[3], q[3]; int s, t;
        scanf("%s%d%s%d", p, &s, q, &t);
 
        if(*p == *q) {
            sum_of_same_part += abs(s - t);
        }else {
            ++N;
            if(s > t) swap(s, t);
            S[N] = s; T[N] = t;
            E[++EN] = ele(-N, s);
            E[++EN] = ele(+N, t);
        }
    }
 
    sort(E+1, E+EN+1);
    E[EN+1].x = (int)2e9;
    if(K == 1) solve1();
    else solve2();
 
    printf("%lld\n", ans + sum_of_same_part + N);
 
    return 0;
}

Compilation message

bridge.cpp: In function 'void pq_ins(ll)':
bridge.cpp:50:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(pq1.size() > md) {
                      ^
bridge.cpp:56:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(pq1.size() < md) {
                      ^
bridge.cpp: In function 'int main()':
bridge.cpp:108:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     int M; scanf("%d%d", &K, &M);
                                 ^
bridge.cpp:111:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s%d%s%d", p, &s, q, &t);
                                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 7104 KB Output is correct
2 Correct 0 ms 7104 KB Output is correct
3 Correct 0 ms 7104 KB Output is correct
4 Correct 0 ms 7104 KB Output is correct
5 Correct 0 ms 7104 KB Output is correct
6 Correct 0 ms 7104 KB Output is correct
7 Correct 0 ms 7104 KB Output is correct
8 Correct 0 ms 7104 KB Output is correct
9 Correct 3 ms 7104 KB Output is correct
10 Correct 0 ms 7104 KB Output is correct
11 Correct 0 ms 7104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 7104 KB Output is correct
2 Correct 0 ms 7104 KB Output is correct
3 Correct 0 ms 7104 KB Output is correct
4 Correct 0 ms 7104 KB Output is correct
5 Correct 0 ms 7104 KB Output is correct
6 Correct 0 ms 7104 KB Output is correct
7 Correct 0 ms 7104 KB Output is correct
8 Correct 0 ms 7104 KB Output is correct
9 Correct 0 ms 7104 KB Output is correct
10 Correct 0 ms 7104 KB Output is correct
11 Correct 0 ms 7104 KB Output is correct
12 Correct 29 ms 7104 KB Output is correct
13 Correct 66 ms 7104 KB Output is correct
14 Correct 49 ms 7104 KB Output is correct
15 Correct 36 ms 7104 KB Output is correct
16 Correct 56 ms 7104 KB Output is correct
17 Correct 53 ms 7104 KB Output is correct
18 Correct 56 ms 7104 KB Output is correct
19 Correct 59 ms 7104 KB Output is correct
20 Correct 43 ms 7104 KB Output is correct
21 Correct 73 ms 7104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 7104 KB Output is correct
2 Correct 0 ms 7104 KB Output is correct
3 Correct 0 ms 7104 KB Output is correct
4 Correct 0 ms 7104 KB Output is correct
5 Correct 0 ms 7104 KB Output is correct
6 Correct 0 ms 7104 KB Output is correct
7 Correct 0 ms 7104 KB Output is correct
8 Correct 0 ms 7104 KB Output is correct
9 Correct 0 ms 7104 KB Output is correct
10 Correct 0 ms 7104 KB Output is correct
11 Correct 0 ms 7104 KB Output is correct
12 Correct 0 ms 7104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 7104 KB Output is correct
2 Correct 0 ms 7104 KB Output is correct
3 Correct 0 ms 7104 KB Output is correct
4 Correct 0 ms 7104 KB Output is correct
5 Correct 0 ms 7104 KB Output is correct
6 Correct 0 ms 7104 KB Output is correct
7 Correct 0 ms 7104 KB Output is correct
8 Correct 0 ms 7104 KB Output is correct
9 Correct 0 ms 7104 KB Output is correct
10 Correct 0 ms 7104 KB Output is correct
11 Correct 0 ms 7104 KB Output is correct
12 Correct 0 ms 7104 KB Output is correct
13 Correct 0 ms 7104 KB Output is correct
14 Correct 0 ms 7104 KB Output is correct
15 Correct 0 ms 7104 KB Output is correct
16 Correct 0 ms 7104 KB Output is correct
17 Correct 0 ms 7104 KB Output is correct
18 Correct 0 ms 7104 KB Output is correct
19 Correct 0 ms 7104 KB Output is correct
20 Correct 0 ms 7104 KB Output is correct
21 Correct 0 ms 7104 KB Output is correct
22 Correct 0 ms 7104 KB Output is correct
23 Correct 0 ms 7104 KB Output is correct
24 Correct 0 ms 7104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 7104 KB Output is correct
2 Correct 0 ms 7104 KB Output is correct
3 Correct 0 ms 7104 KB Output is correct
4 Correct 0 ms 7104 KB Output is correct
5 Correct 0 ms 7104 KB Output is correct
6 Correct 0 ms 7104 KB Output is correct
7 Correct 0 ms 7104 KB Output is correct
8 Correct 0 ms 7104 KB Output is correct
9 Correct 0 ms 7104 KB Output is correct
10 Correct 0 ms 7104 KB Output is correct
11 Correct 0 ms 7104 KB Output is correct
12 Correct 0 ms 7104 KB Output is correct
13 Correct 0 ms 7104 KB Output is correct
14 Correct 0 ms 7104 KB Output is correct
15 Correct 0 ms 7104 KB Output is correct
16 Correct 0 ms 7104 KB Output is correct
17 Correct 0 ms 7104 KB Output is correct
18 Correct 0 ms 7104 KB Output is correct
19 Correct 0 ms 7104 KB Output is correct
20 Correct 0 ms 7104 KB Output is correct
21 Correct 0 ms 7104 KB Output is correct
22 Correct 0 ms 7104 KB Output is correct
23 Correct 0 ms 7104 KB Output is correct
24 Correct 0 ms 7104 KB Output is correct
25 Correct 56 ms 9836 KB Output is correct
26 Correct 119 ms 9752 KB Output is correct
27 Correct 133 ms 9752 KB Output is correct
28 Correct 156 ms 9752 KB Output is correct
29 Correct 153 ms 9752 KB Output is correct
30 Correct 83 ms 8472 KB Output is correct
31 Correct 56 ms 9836 KB Output is correct
32 Correct 116 ms 9836 KB Output is correct
33 Correct 79 ms 9752 KB Output is correct
34 Correct 143 ms 9836 KB Output is correct
35 Correct 83 ms 9836 KB Output is correct
36 Correct 133 ms 9836 KB Output is correct
37 Correct 59 ms 9836 KB Output is correct