Submission #331051

# Submission time Handle Problem Language Result Execution time Memory
331051 2020-11-27T06:20:01 Z ChrisGe123 Palembang Bridges (APIO15_bridge) C++14
100 / 100
647 ms 17516 KB
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>,
    rb_tree_tag, tree_order_statistics_node_update>;

//g++ -std=c++17 -O2 -fsanitize=undefined /usr/local/Cellar/gcc/10.2.0/include/c++/10.2.0/x86_64-apple-darwin19/bits/stdc++.h
#define maxn 100005
#define mod 1000000007
int n, k;
pair<ll, pair<ll, ll> > interval[maxn]; 
ll ans;


Tree<pair<ll, int> > lendpoints;
Tree<pair<ll, int> > rendpoints;

void setupIO(string s)
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}

ll f(int i, ll bridge)
{
	ll l = interval[i].second.first; ll r = interval[i].second.second;
	if (l <= bridge && bridge <= r)
	{
		return 0;
	}
	else if (bridge < l)
	{
		return 2 * (l-bridge);
	}
	else
	{
		return 2 * (bridge-r);
	}
}
int main()
{
	//setupIO("bridges");
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> k >> n;
	int counter = 0;
	ll pre = 0;
	lendpoints.clear();
	rendpoints.clear();
	for (int i=0; i<n; i++)
	{
		char p, q;
		ll s, t;
		cin >> p >> s >> q >> t;
		if (p == q)
		{
			pre += abs(s - t);
		}
		else
		{
			ll l = min(s, t);
			ll r = max(s, t);
			interval[counter++] = make_pair(s+t, make_pair(l, r));
			pre += r - l + 1;
		}
	}
	//use the below version to make it easier to randomly generate test cases 
	// for (int i=0; i<n; i++)
	// {
	// 	ll s, t;
	// 	cin >> s >> t;
	// 	ll l = min(s, t);
	// 	ll r = max(s, t);
	// 	interval[counter++] = make_pair(s+t, make_pair(l, r));
	// 	pre += r - l + 1;
	// }
	n = counter;
	ans = 0;
	//we just want to find the median of the endpoints 
	sort(interval, interval+n);
	for (int i=0; i<n; i++)
	{
		rendpoints.insert(make_pair(interval[i].second.first, 2*i));
		rendpoints.insert(make_pair(interval[i].second.second, 2*i+1));
	}
	ll xone = rendpoints.find_by_order(n-1)->first;
	//cerr << x << endl;
	for (int i=0; i<n; i++)
	{
		ll l = interval[i].second.first; ll r = interval[i].second.second;
		if (l <= xone && xone <= r)
		{
			continue;
		}
		else if (xone < l)
		{
			ans += 2 * (l-xone);
		}
		else
		{
			ans += 2 * (xone-r);
		} 
	}
	if (k == 2)
	{
		 //sorted in order of increasing midpoint
		// cerr << ans << endl;
		// cerr << endl;
		// cerr << endl;
		ll temp = ans;
		for (int i=0; i<n-1; i++)
		{
			//things 0 to i take the first bridge, i+1 to n-1 take the second bridge
			lendpoints.insert(make_pair(interval[i].second.first, 2*i));
			lendpoints.insert(make_pair(interval[i].second.second, 2*i+1));
			ll x = lendpoints.find_by_order(i)->first;

			rendpoints.erase(make_pair(interval[i].second.first, 2*i));
			rendpoints.erase(make_pair(interval[i].second.second, 2*i+1));
			ll y = rendpoints.find_by_order(n-i-2)->first;
			temp += f(i,x) -f(i, y);
		
			/* 
			To figure out the change from x_prev to x for items 1 to i-1, 

			Claim: each new point added has its midpoint to the right of the current median
				proof is by induction and using the fact that sorted order of midpoints
				in particular, this means that either one point left of median and one right or two right, so the median never moves back
			If the median stays the same, no change
			If the median moves forward by one point
				if it moved to the left endpoint of point i: no change
				if it moved to any other point: it just moved to prevhighx from prevlowx, so no change
			NO CHANGE FOR ITEMS 1 TO i-1

			To figure out the change for item i: just calculate directly f(i, x) - f(i, oldy)

			To figure out the change for items i+1 to n-1: 
				goes down by f(i, newy) - f(i, oldy): let's see why
					if i was both left of old median and both left of new median: sum wouldn't change if you moved from old to new, but now that we don't factor in the f(i, new) - f(i, old), we have to subtract that
					if i was one left one right of old median and both left of new median: same as above except f(i, old) = 0
					if i was one left one right of old median and one left one right of new median: no change at all, this works

			*/
			// cerr << i << endl;
			// cerr << temp << endl;
			// cerr << endl;
			ans = min(ans, temp);
		}
	}
	cout << ans + pre << endl;
}

Compilation message

bridge.cpp: In function 'void setupIO(std::string)':
bridge.cpp:25:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   25 |  freopen((s + ".in").c_str(), "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:26:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   26 |  freopen((s + ".out").c_str(), "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 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 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 163 ms 15212 KB Output is correct
13 Correct 218 ms 15212 KB Output is correct
14 Correct 251 ms 13676 KB Output is correct
15 Correct 120 ms 9196 KB Output is correct
16 Correct 190 ms 15212 KB Output is correct
17 Correct 158 ms 15340 KB Output is correct
18 Correct 148 ms 15212 KB Output is correct
19 Correct 168 ms 15340 KB Output is correct
20 Correct 169 ms 15212 KB Output is correct
21 Correct 175 ms 15320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 376 ms 15340 KB Output is correct
26 Correct 282 ms 16236 KB Output is correct
27 Correct 647 ms 17132 KB Output is correct
28 Correct 618 ms 17516 KB Output is correct
29 Correct 628 ms 17516 KB Output is correct
30 Correct 321 ms 9452 KB Output is correct
31 Correct 441 ms 17004 KB Output is correct
32 Correct 366 ms 17516 KB Output is correct
33 Correct 335 ms 17260 KB Output is correct
34 Correct 359 ms 17516 KB Output is correct
35 Correct 369 ms 17132 KB Output is correct
36 Correct 379 ms 17516 KB Output is correct
37 Correct 362 ms 16108 KB Output is correct