답안 #1028705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1028705 2024-07-20T07:24:35 Z vjudge1 Palembang Bridges (APIO15_bridge) C++17
22 / 100
89 ms 8512 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<ll> a, b, pa, pb;

ll query(ll x, ll y)
{
  int idx = upper_bound(b.begin(), b.end(), x) - b.begin();
  ll ans = idx * x - pb[idx];

  idx = upper_bound(a.begin(), a.end(), y) - a.begin();
  ans += (pa.back() - pa[idx]) - (a.size() - idx) * y;
  return ans;
}
int main()
{
  int k, n;
  cin >> k >> n;
  ll ans = 0;
  vector<pair<int,int> > p;
  for(int i = 0; i < n; i ++)
    {
      char P, Q;
      int s, t;
      cin >> P >> s >> Q >> t;
      if(t < s) swap(s, t);
      if(P == Q)
	ans += t - s;
      else
	{
	  a.push_back(s);
	  b.push_back(t);
	  p.push_back({s, t});
	}
    }

  pa.resize(1 + a.size());
  pb.resize(1 + b.size());
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  
  pa[0] = pb[0] = 0;
  for(int i = 0; i < a.size(); i++)
    {
      pa[i + 1] = pa[i] + a[i];
      pb[i + 1] = pb[i] + b[i];
    }
  
  ans += p.size();
  
  if(k == 1)
    {
      vector<ll> v;
      for(auto [x, y] : p)
	{
	  v.push_back(x);
	  v.push_back(y);
	}
      
      sort(v.begin(), v.end());
      ll testans = 1e18 * (!v.empty()), suf = 0, sz = v.size();
      for(ll i : v) suf += i;
      ll prf = 0, cnt = 0;
      for(ll i : v)
	{
	  testans = min(testans, cnt * i - prf + suf - (sz - cnt) * i);
	  prf += i;
	  suf -= i;
	  cnt++;
	}
      
      ans += testans;
    }
  else
    {
      ll testans = 1e18 * (!p.empty());
      
      vector<ll> v;
      for(auto [x, y] : p)
	v.push_back(y), v.push_back(x), ans += y - x;

      sort(v.begin(), v.end());
      v.resize(unique(v.begin(), v.end()) - v.begin());

      int cnt = 0;
      map<int, vector<pair<int, int> > > mp;
      for(auto [x, y] : p)
	mp[y].push_back({x, cnt++});
      
      for(int i = 0; i < v.size(); i ++)
	{
	  set<pair<int, int> > st;
	  ll cost = 0, costp = 0;
	  for(int j = i; j < v.size(); j++)
	    {
	      ll temp = query(v[i], v[j]);

	      if(st.size())
		cost += 1ll * st.size() * (v[j] - v[j - 1]);
	      
	      while(st.size() && st.begin()->first <= v[j])
		{
		  int idx = st.begin()->second;
		  costp += p[i].first - v[i];
		  cost -= v[j] - p[i].second;
		  st.erase(st.begin());
		}
	      
	      if(j - 1 > i)
		{
		  for(auto [s, idx] : mp[v[j - 1]])
		    if(v[i] < s)
		      {
			if(s - v[i] <= v[j] - v[j - 1])
			  costp += s - v[i];
			else
			  {
			    cost += v[j] - v[j - 1];
			    st.insert({v[j - 1] + s - v[i], idx});
			  }
		      }
		}
	      //  cerr << i << ' ' << j << ' ' << temp << ' ' << cost << ' ' << costp << endl;
	      testans = min(testans, temp + cost + costp);
	    }
	}
      ans += testans;
    }
  cout << ans << endl;
  return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:46:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |   for(int i = 0; i < a.size(); i++)
      |                  ~~^~~~~~~~~~
bridge.cpp:93:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |       for(int i = 0; i < v.size(); i ++)
      |                      ~~^~~~~~~~~~
bridge.cpp:97:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |    for(int j = i; j < v.size(); j++)
      |                   ~~^~~~~~~~~~
bridge.cpp:106:9: warning: unused variable 'idx' [-Wunused-variable]
  106 |     int idx = st.begin()->second;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 480 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 36 ms 7136 KB Output is correct
13 Correct 89 ms 8512 KB Output is correct
14 Correct 54 ms 7232 KB Output is correct
15 Correct 49 ms 5192 KB Output is correct
16 Correct 64 ms 8000 KB Output is correct
17 Correct 64 ms 8420 KB Output is correct
18 Correct 61 ms 8256 KB Output is correct
19 Correct 83 ms 8500 KB Output is correct
20 Correct 59 ms 8252 KB Output is correct
21 Correct 73 ms 8248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 2 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 1 ms 344 KB Output isn't correct
5 Halted 0 ms 0 KB -