Submission #1024834

# Submission time Handle Problem Language Result Execution time Memory
1024834 2024-07-16T10:53:15 Z EkinOnal Palembang Bridges (APIO15_bridge) C++17
100 / 100
66 ms 13080 KB
//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#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 MAX 1000007
#define pb push_back
#define mp make_pair 
#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>> 
const int mod = 2019201997;
const int INF = 1e18;
// myMap.begin()->first :  key
// myMap.begin()->second : value
 
int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;/*ans%=mod;a%=mod;*/b>>=1;}return ans/*%mod*/;}
int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
 
 
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
 
struct DSU {
	vi e,sayi; void init(int N) { e = vi(N,-1); sayi = vi(N);}
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } 
	bool sameSet(int a, int b) { return get(a) == get(b); }
	int size(int x) { return -e[get(x)]; }
	bool unite(int x, int y) { // union by size
		x = get(x), y = get(y); if (x == y) return 0;
		if (e[x] > e[y]) swap(x,y);
		e[x] += e[y]; e[y] = x; sayi[x]+=sayi[y]; return 1;
	}
};
 
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
	tree_order_statistics_node_update>ordered_set;
 
 
priority_queue<int,vi,greater<int>> rq;
priority_queue<int> lq;
 
int lsum,rsum;
 
void add(int a)
{
	int medyan=(lq.size()?lq.top():1000000001);
 
	if(a<=medyan) {lq.push(a);lsum+=a;}
	else {rq.push(a);rsum+=a;}
 
//	lq.push(a);
	if(lq.size()>rq.size()+1)
	{
		int sayi=lq.top();
		lq.pop();
		rq.push(sayi);
		rsum+=sayi;
		lsum-=sayi;
	}
	if(lq.size()<rq.size()) //whilea gerek yok gibi
	{
		lq.push(rq.top()); 
		lsum+=rq.top(); rsum-=rq.top();
		rq.pop();
	}
}
 
bool cmp(pair<int, int> a, pair<int, int> b) {
	return a.first + a.second < b.first + b.second;
}
 
void solve() 
{
	int n,k; cin >> k >> n;
 
	vpii v = {{0, 0}};
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int b,d;char a,c; cin>>a>>b>>c>>d;
		if(a==c) ans+=abs(b-d);
		else
		{
		//	add(b); add(d);
			v.pb({b,d});
		}
	}
 
	sort(v.begin(),v.end(),cmp);
	n=v.size()-1;
 
	ans += n;
 
	vi pre(MAX);
 
	for(int i=1;i<=n;i++)
	{
		add(v[i].f); add(v[i].s);
		pre[i]=rsum-lsum;
	}
 
//	for(int i=1;i<=n;i++) cout << pre[i] << " ";cout<<endl;
 
	if(k==1)
	{
		cout << ans+pre[n] << endl;
		return;
	}
 
	while (lq.size()) lq.pop();
	while (rq.size()) rq.pop();
 
	lsum=rsum=0;
 
	int cvp=pre[n];
	for(int i=n;i>0;i--)
	{
		add(v[i].f); add(v[i].s);
		cvp=min(cvp,pre[i-1]+rsum-lsum);
	}
 
 
	cout << cvp+ans << endl;
 	
    return;	
 	
}	
 
 
int32_t main() {
//	freopen("hayfeast.in", "r", stdin);
//	freopen("hayfeast.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(0);   
 
    int t=1;
//    cin >> t;
    while (t--) solve();
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 1 ms 8284 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 2 ms 8284 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 1 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8136 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 2 ms 8284 KB Output is correct
6 Correct 2 ms 8308 KB Output is correct
7 Correct 3 ms 8280 KB Output is correct
8 Correct 2 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 20 ms 12616 KB Output is correct
13 Correct 38 ms 12476 KB Output is correct
14 Correct 32 ms 13012 KB Output is correct
15 Correct 23 ms 10968 KB Output is correct
16 Correct 22 ms 12600 KB Output is correct
17 Correct 30 ms 12756 KB Output is correct
18 Correct 27 ms 13024 KB Output is correct
19 Correct 34 ms 12656 KB Output is correct
20 Correct 31 ms 12804 KB Output is correct
21 Correct 33 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8284 KB Output is correct
2 Correct 2 ms 8172 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 1 ms 8284 KB Output is correct
5 Correct 1 ms 8284 KB Output is correct
6 Correct 1 ms 8284 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 1 ms 8284 KB Output is correct
10 Correct 1 ms 8280 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8284 KB Output is correct
2 Correct 1 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 1 ms 8284 KB Output is correct
5 Correct 1 ms 8284 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 1 ms 8284 KB Output is correct
8 Correct 1 ms 8284 KB Output is correct
9 Correct 1 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 1 ms 8284 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8088 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 1 ms 8284 KB Output is correct
18 Correct 2 ms 8284 KB Output is correct
19 Correct 2 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8284 KB Output is correct
2 Correct 1 ms 8284 KB Output is correct
3 Correct 2 ms 8284 KB Output is correct
4 Correct 2 ms 8284 KB Output is correct
5 Correct 1 ms 8284 KB Output is correct
6 Correct 2 ms 8284 KB Output is correct
7 Correct 2 ms 8280 KB Output is correct
8 Correct 2 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 1 ms 8284 KB Output is correct
13 Correct 2 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 3 ms 8232 KB Output is correct
19 Correct 2 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 2 ms 8284 KB Output is correct
25 Correct 27 ms 12812 KB Output is correct
26 Correct 52 ms 13080 KB Output is correct
27 Correct 64 ms 12724 KB Output is correct
28 Correct 66 ms 12744 KB Output is correct
29 Correct 66 ms 12244 KB Output is correct
30 Correct 33 ms 10540 KB Output is correct
31 Correct 29 ms 12328 KB Output is correct
32 Correct 52 ms 12744 KB Output is correct
33 Correct 48 ms 12240 KB Output is correct
34 Correct 53 ms 12632 KB Output is correct
35 Correct 33 ms 12756 KB Output is correct
36 Correct 51 ms 12744 KB Output is correct
37 Correct 23 ms 12756 KB Output is correct