Submission #554716

# Submission time Handle Problem Language Result Execution time Memory
554716 2022-04-29T09:23:34 Z josanneo22 Palembang Bridges (APIO15_bridge) C++17
100 / 100
92 ms 5692 KB
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<stdlib.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int> > vpii;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (a); i > (b); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define mp make_pair
#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second
#define out(x) cout<<x;
#define in(x) cin>>x;
#define inarr(a,x,y) for(int i=x;i<y;i++){cin>>a[i];}
#define incor(a,x,y) for(int i=x;i<y;i++){cin>>a[i].f>>a[i].s;}
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
const int mod = 1e9 + 7;

bool cmp(pii p1, pii p2)//ascending
{
	return p1.f + p1.s < p2.f + p2.s;
}

void normal()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
}
ll ex(int base, int power)
{
	if (power == 0)
		return 1;
	ll result = ex(base, power / 2);
	if (power % 2 == 1)
		return(((result * result) % mod) * base) % mod;
	else return (result * result) % mod;
}
int k, n;
priority_queue<int> lft;//descending 
priority_queue<int, vi, greater<int> > rgt;// ascending
ll lsum, rsum;

void add(int x) 
{
	int median;
	if (lft.empty())
		median = 1e9 + 1;
	else
		median = lft.top();
	if (x <= median) 
	{
		lft.push(x);
		lsum += x;
	}
	else 
	{
		rgt.push(x);
		rsum += x;
	}

	if (rgt.size() + 1 < lft.size()) 
	{
		int nxt = lft.top();
		lft.pop();
		rgt.push(nxt);
		lsum -= nxt;
		rsum += nxt;
	}
	else if (lft.size() < rgt.size()) 
	{
		int nxt = rgt.top();
		rgt.pop();
		lft.push(nxt);
		rsum -= nxt;
		lsum += nxt;
	}
}

ll pref[100001];

int main() 
{
	normal();
	ll cnt = 0;
	vpii vect;
	cin >> k >> n;
	FOR(i, 0, n) 
	{
		char a, b;
		int x, y;
		cin >> a >> x >> b >> y;
		if (a == b) 
			cnt += abs(x - y);// as same zone does require the citizen to cross a bridge, we just need to add distance between x and y
		else
			vect.pb({ x, y });
	}
	sort(all(vect), cmp);
	cnt += vect.size();// adds because requires 1 step to go throught the bridge
	FOR(i, 0, vect.size())
	{
		add(vect[i].f);
		add(vect[i].s);
		pref[i] = rsum - lsum;// distance from this vect[i to 0] to bridge 1
	}
	ll ans = pref[vect.size()-1];
	if (k == 2) 
	{
		while (lft.size()) lft.pop();// reset to calculate distance from the current vect[i to n] to bridge 2
		while (rgt.size()) rgt.pop();
		lsum = rsum = 0;

		ROF(i,vect.size()-1, 0)
		{
			add(vect[i].f);
			add(vect[i].s);
			ans = min(ans, (ll)rsum - lsum + pref[i - 1]);//if all the citizens from (i to n) all picked bridge 2 then just add pref[i-1]
			// because pref[i-1] keeps the distance from vect[i-1 to 0] to bridge 1 and we assume taht all before i goes to bridge 1
		}
	}
	cout << cnt + ans;
	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:14:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
bridge.cpp:114:2: note: in expansion of macro 'FOR'
  114 |  FOR(i, 0, vect.size())
      |  ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 356 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 27 ms 4300 KB Output is correct
13 Correct 52 ms 5480 KB Output is correct
14 Correct 43 ms 4300 KB Output is correct
15 Correct 32 ms 3508 KB Output is correct
16 Correct 35 ms 4936 KB Output is correct
17 Correct 44 ms 5364 KB Output is correct
18 Correct 41 ms 5036 KB Output is correct
19 Correct 50 ms 5396 KB Output is correct
20 Correct 36 ms 5312 KB Output is correct
21 Correct 49 ms 5152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 2 ms 388 KB Output is correct
25 Correct 38 ms 3872 KB Output is correct
26 Correct 67 ms 4308 KB Output is correct
27 Correct 83 ms 5040 KB Output is correct
28 Correct 87 ms 5692 KB Output is correct
29 Correct 92 ms 5444 KB Output is correct
30 Correct 46 ms 3096 KB Output is correct
31 Correct 45 ms 4680 KB Output is correct
32 Correct 64 ms 5448 KB Output is correct
33 Correct 68 ms 5452 KB Output is correct
34 Correct 71 ms 5452 KB Output is correct
35 Correct 47 ms 5324 KB Output is correct
36 Correct 85 ms 5188 KB Output is correct
37 Correct 30 ms 3872 KB Output is correct