Submission #76786

# Submission time Handle Problem Language Result Execution time Memory
76786 2018-09-17T20:32:02 Z born2rule Palembang Bridges (APIO15_bridge) C++14
100 / 100
555 ms 34608 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int,int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i,n) for (int i = 1;i <= (int)(n); ++i)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repv(i,a,b) for(int i=b-1;i>=a;i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int ,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set ;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int N=100005;
int st[N],en[N],cnt,ind[N];
void adjust(multiset<int> &x,multiset<int> &y,ll &sum1,ll &sum2)
{
  while(sz(y)>sz(x))
    {
      int tmp=*y.begin(); sum2-=tmp;
      y.erase(y.begin()); x.insert(tmp); sum1+=tmp;
    }
  while(sz(x)>sz(y)+1)
    {
      int tmp=*x.rbegin(); sum1-=tmp;
      x.erase(x.find(tmp)); y.insert(tmp); sum2+=tmp;
    }
}
void insert(multiset<int> &x,multiset<int> &y,ll &sum1,ll &sum2,ll val)
{
  if(!sz(x))
    {
      x.insert(val); sum1+=val;
      return;
    }
  if(*x.rbegin()>=val) x.insert(val),sum1+=val;
  else y.insert(val),sum2+=val;
  adjust(x,y,sum1,sum2);
}
void erase(multiset<int> &x,multiset<int> &y,ll &sum1,ll &sum2,ll val)
{
  if(*x.rbegin()>=val) x.erase(x.find(val)),sum1-=val;
  else y.erase(y.find(val)),sum2-=val;
  adjust(x,y,sum1,sum2);
}
int main()
{
  std::ios::sync_with_stdio(false);
  cin.tie(NULL) ; cout.tie(NULL) ;
  int k,n;
  cin>>k>>n;
  ll sum=0;
  rep(i,1,n+1)
    {
      char x,y;
      int st,en;
      cin>>x>>st>>y>>en;
      if(st>en) swap(st,en);
      if(x==y) sum+=en-st;
      else
	{
	  cnt++;
	  ::st[cnt]=st; ::en[cnt]=en;
	  ind[cnt]=cnt;
	}
    }
  sort(ind+1,ind+cnt+1,[](int i,int j){ return ((st[i]+en[i]!=st[j]+en[j])?((st[i]+en[i])<(st[j]+en[j])):(st[i]<st[j]));});
  ll ans=LLONG_MAX;
  multiset<int> s1,s2,s3,s4;
  ll sum1=0,sum2=0,sum3=0,sum4=0;
  rep(i,1,cnt+1)
    {
      insert(s3,s4,sum3,sum4,st[ind[i]]);
      insert(s3,s4,sum3,sum4,en[ind[i]]);
    }
  rep(i,0,cnt+1)
    {
      ll curr=0;
      if(sz(s1))
	{
	  auto it=*s1.rbegin();
	  int sz1=sz(s1),sz2=sz(s2);
	  auto it2=*s2.begin();
	  ll curr1=(ll)sz1*it-sum1+sum2-(ll)sz2*it;
	  ll curr2=(ll)sz1*it2-sum1+sum2-(ll)sz2*it2;
	  curr+=min(curr1,curr2);
	}
      if(sz(s3))
	{
	  auto it=*s3.rbegin();
	  int sz1=sz(s3),sz2=sz(s4);
	  auto it2=*s4.begin();
	  ll curr1=(ll)sz1*it-sum3+sum4-(ll)sz2*it;
	  ll curr2=(ll)sz1*it2-sum3+sum4-(ll)sz2*it2;
	  curr+=min(curr1,curr2);
	}
      curr+=cnt;
      ans=min(ans,curr);
      if(k==1) break;
      if(i==cnt) continue;
      erase(s3,s4,sum3,sum4,st[ind[i+1]]);
      erase(s3,s4,sum3,sum4,en[ind[i+1]]);
      insert(s1,s2,sum1,sum2,st[ind[i+1]]);
      insert(s1,s2,sum1,sum2,en[ind[i+1]]);
    }
  if(ans==LLONG_MAX) ans=0;
  ans+=sum;
  cout<<ans<<endl;
  return 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 584 KB Output is correct
4 Correct 3 ms 708 KB Output is correct
5 Correct 3 ms 708 KB Output is correct
6 Correct 2 ms 708 KB Output is correct
7 Correct 3 ms 708 KB Output is correct
8 Correct 3 ms 820 KB Output is correct
9 Correct 3 ms 820 KB Output is correct
10 Correct 3 ms 820 KB Output is correct
11 Correct 3 ms 820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 820 KB Output is correct
2 Correct 2 ms 820 KB Output is correct
3 Correct 3 ms 820 KB Output is correct
4 Correct 3 ms 820 KB Output is correct
5 Correct 3 ms 820 KB Output is correct
6 Correct 3 ms 820 KB Output is correct
7 Correct 3 ms 820 KB Output is correct
8 Correct 3 ms 820 KB Output is correct
9 Correct 3 ms 820 KB Output is correct
10 Correct 3 ms 820 KB Output is correct
11 Correct 3 ms 820 KB Output is correct
12 Correct 95 ms 11156 KB Output is correct
13 Correct 196 ms 11156 KB Output is correct
14 Correct 164 ms 11156 KB Output is correct
15 Correct 97 ms 11156 KB Output is correct
16 Correct 90 ms 11180 KB Output is correct
17 Correct 129 ms 11188 KB Output is correct
18 Correct 120 ms 11188 KB Output is correct
19 Correct 143 ms 11196 KB Output is correct
20 Correct 119 ms 11196 KB Output is correct
21 Correct 131 ms 11200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11200 KB Output is correct
2 Correct 2 ms 11200 KB Output is correct
3 Correct 2 ms 11200 KB Output is correct
4 Correct 3 ms 11200 KB Output is correct
5 Correct 2 ms 11200 KB Output is correct
6 Correct 2 ms 11200 KB Output is correct
7 Correct 2 ms 11200 KB Output is correct
8 Correct 2 ms 11200 KB Output is correct
9 Correct 2 ms 11200 KB Output is correct
10 Correct 2 ms 11200 KB Output is correct
11 Correct 2 ms 11200 KB Output is correct
12 Correct 2 ms 11200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11200 KB Output is correct
2 Correct 2 ms 11200 KB Output is correct
3 Correct 2 ms 11200 KB Output is correct
4 Correct 3 ms 11200 KB Output is correct
5 Correct 2 ms 11200 KB Output is correct
6 Correct 2 ms 11200 KB Output is correct
7 Correct 2 ms 11200 KB Output is correct
8 Correct 2 ms 11200 KB Output is correct
9 Correct 2 ms 11200 KB Output is correct
10 Correct 2 ms 11200 KB Output is correct
11 Correct 2 ms 11200 KB Output is correct
12 Correct 2 ms 11200 KB Output is correct
13 Correct 3 ms 11200 KB Output is correct
14 Correct 4 ms 11200 KB Output is correct
15 Correct 4 ms 11200 KB Output is correct
16 Correct 2 ms 11200 KB Output is correct
17 Correct 3 ms 11200 KB Output is correct
18 Correct 3 ms 11200 KB Output is correct
19 Correct 3 ms 11200 KB Output is correct
20 Correct 4 ms 11200 KB Output is correct
21 Correct 3 ms 11200 KB Output is correct
22 Correct 3 ms 11200 KB Output is correct
23 Correct 3 ms 11200 KB Output is correct
24 Correct 3 ms 11200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11200 KB Output is correct
2 Correct 2 ms 11200 KB Output is correct
3 Correct 2 ms 11200 KB Output is correct
4 Correct 2 ms 11200 KB Output is correct
5 Correct 2 ms 11200 KB Output is correct
6 Correct 2 ms 11200 KB Output is correct
7 Correct 2 ms 11200 KB Output is correct
8 Correct 3 ms 11200 KB Output is correct
9 Correct 2 ms 11200 KB Output is correct
10 Correct 3 ms 11200 KB Output is correct
11 Correct 2 ms 11200 KB Output is correct
12 Correct 2 ms 11200 KB Output is correct
13 Correct 4 ms 11200 KB Output is correct
14 Correct 4 ms 11200 KB Output is correct
15 Correct 4 ms 11200 KB Output is correct
16 Correct 3 ms 11200 KB Output is correct
17 Correct 3 ms 11200 KB Output is correct
18 Correct 3 ms 11200 KB Output is correct
19 Correct 3 ms 11200 KB Output is correct
20 Correct 3 ms 11200 KB Output is correct
21 Correct 3 ms 11200 KB Output is correct
22 Correct 5 ms 11200 KB Output is correct
23 Correct 5 ms 11200 KB Output is correct
24 Correct 5 ms 11200 KB Output is correct
25 Correct 206 ms 12968 KB Output is correct
26 Correct 254 ms 13968 KB Output is correct
27 Correct 544 ms 15748 KB Output is correct
28 Correct 526 ms 18152 KB Output is correct
29 Correct 555 ms 20488 KB Output is correct
30 Correct 412 ms 20488 KB Output is correct
31 Correct 201 ms 23256 KB Output is correct
32 Correct 247 ms 25688 KB Output is correct
33 Correct 290 ms 27628 KB Output is correct
34 Correct 272 ms 29828 KB Output is correct
35 Correct 242 ms 31824 KB Output is correct
36 Correct 257 ms 33956 KB Output is correct
37 Correct 259 ms 34608 KB Output is correct