Submission #283303

# Submission time Handle Problem Language Result Execution time Memory
283303 2020-08-25T14:08:47 Z NaimSS Palembang Bridges (APIO15_bridge) C++14
100 / 100
195 ms 13360 KB
#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define td(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
typedef long long ll;
typedef pair<int,int> pii;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
    if(b==0LL) return 1LL;
    if(b==1LL) return mod(a,m);
    ll k = mod(exp(a,b/2,m),m);
    if(b&1LL){
        return mod(a*mod(k*k,m),m);
    }
    else return mod(k*k,m);
}


struct BIT{
 int n;
 int MAX;
 vector<ll> bit;
 BIT(){}
 BIT(int _n){
   n = _n;
   MAX = n +2;
   bit.resize(n+10,0);
 }
 
 ll query(int x){
   ll r=0;
   while(x>0){
     r+=bit[x];
     x-=(x&-x);
   }
   return r;
 }
 
 ll query(int l,int r){
  if(l > r){
    return 0;
  }
  assert(l<=r);

   return query(r) - query(l-1);
 }
 
 void update(int x,ll val){
   while(x<MAX){
     bit[x]+=val;
     x+=(x&-x);
   }
 }
 
 ll find_kth(ll k){
   ll sum=0;
   ll pos=0;
   // 20 == log -> trocar conforme necessario
   for(int i=20;i>=0;i--){
     if(pos+(1LL<<i)>n)continue;
     int atual = bit[pos+(1LL<<i)];
     if(sum+atual<k){
       sum+=atual;
       pos+=(1LL<<i);
     }
     
   }
   
   //acha a maior pos com <k;
   return pos+1;
 }
   
};

  vi comp;
struct mediana{
  int n;
  int tot;
  BIT freq,sum;
  mediana(){}
  mediana(int n,int tot):n(n),tot(tot){
    freq = sum = BIT(n+n);
  }
  void insert(int x){
    int pos = lower_bound(all(comp),x) - comp.begin() + 1;
    tot++;
    freq.update(pos,1);sum.update(pos,x);
  }
  ll f(){
    int pos = freq.find_kth(tot/2);
    ll val = comp[pos-1];

    ll r = freq.query(pos) * val - sum.query(pos);
    r+= sum.query(pos + 1,n + n) - freq.query(pos+1,n+n) * val;
    return r; 
  }
  void clear(){
    tot=0;
    freq = sum = BIT(n+n);
  }
};

const int N = 100100;
int S[N],T[N];
char P[N],Q[N];

int pre[N],suf[N];

int32_t main(){
  fastio;
  int k,n;
  cin >> k >> n;
  vector<pii> vec;
  ll bef=0;
  for(int i=1;i<=n;i++){
    cin >> P[i] >> S[i] >> Q[i] >> T[i];
    if(S[i] > T[i])swap(S[i],T[i]);
    if(P[i] == Q[i]){

      bef+=T[i] - S[i];
    }else{
      bef++;
      vec.pb(pii(S[i],T[i]));
      comp.pb(S[i]);comp.pb(T[i]);
    }
  }
  sort(all(comp));
  comp.erase(unique(all(comp)),comp.end());
  sort(all(vec),[&](pii a,pii b){
    return a.ff + a.ss < b.ff + b.ss;
  });
  int tam = sz(vec);
  if(tam == 0){
    cout << bef << endl;
    return 0;
  }
  mediana Med(n,0);


  for(int i=0;i<tam;i++){
    Med.insert(vec[i].ff);Med.insert(vec[i].ss);
    pre[i] = Med.f();
  }
  Med.clear();
  for(int i = tam-1;i>=0;i--){
    Med.insert(vec[i].ff);Med.insert(vec[i].ss);
    suf[i] = Med.f();
  }

  ll ans = pre[tam-1];
  if(k == 2){
  for(int i=0;i<tam-1;i++){
    ans = min(ans,pre[i] + suf[i+1]);
  }
  }
  cout << ans + bef << endl;



  // math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 110 ms 11764 KB Output is correct
13 Correct 194 ms 13272 KB Output is correct
14 Correct 133 ms 11124 KB Output is correct
15 Correct 105 ms 8032 KB Output is correct
16 Correct 124 ms 12548 KB Output is correct
17 Correct 123 ms 13360 KB Output is correct
18 Correct 124 ms 13024 KB Output is correct
19 Correct 137 ms 13272 KB Output is correct
20 Correct 117 ms 12880 KB Output is correct
21 Correct 136 ms 13008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 400 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 640 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 113 ms 11728 KB Output is correct
26 Correct 131 ms 11912 KB Output is correct
27 Correct 192 ms 12760 KB Output is correct
28 Correct 191 ms 13268 KB Output is correct
29 Correct 195 ms 13268 KB Output is correct
30 Correct 92 ms 7264 KB Output is correct
31 Correct 116 ms 12624 KB Output is correct
32 Correct 121 ms 13272 KB Output is correct
33 Correct 120 ms 12896 KB Output is correct
34 Correct 139 ms 13264 KB Output is correct
35 Correct 118 ms 12752 KB Output is correct
36 Correct 131 ms 13016 KB Output is correct
37 Correct 115 ms 11728 KB Output is correct