답안 #682982

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682982 2023-01-17T12:01:07 Z bane Palembang Bridges (APIO15_bridge) C++17
100 / 100
233 ms 13640 KB
#include<iostream>
#include<set>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
           
using ll = long long;
using db = long double;
using llu = unsigned long long;
using str = string;
           
// pairs
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdb = pair<db,db>;
#define mp make_pair
#define fr first
#define sc second
           
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT, size_t SZ> using AR = array<T,SZ>; 
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pll>;
using vpd = V<pdb>;
#define ms0(x) memset(x , 0, sizeof(x))
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
           
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
tcT> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
           
// loops
#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 = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
           
const int MOD = (int)2019201949; // 998244353;
const int MX = (int)2e5+5;
const ll BIG = 1e18; 
const db PI = acos((db)-1);
using ld = long double;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};
 
void setIO(string name = "") {
    cin.tie(0)->sync_with_stdio(0); 
    if (sz(name)) {
        freopen((name + ".in").c_str(), "r", stdin); 
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
multiset<int>lower, upper;
int n, k; 
long long lmas = 0, rmas = 0;
void insertion(int val){
    int a = (lower.empty() ? 1000000000 : *lower.rbegin());
    if (val <= a){
		lower.insert(val);
		lmas+=val;
	}else{
		upper.insert(val);
		rmas+=val;
	}
	
	if (upper.size() + 1 < lower.size()){
		upper.insert(*lower.rbegin());
		lmas-=*lower.rbegin();
		rmas+=*lower.rbegin();
		lower.erase(lower.find(*lower.rbegin()));
	}else if (upper.size() > lower.size()){
		lower.insert(*upper.begin());
		lmas+=*upper.begin();
		rmas-=*upper.begin();
		upper.erase(upper.find(*upper.begin()));
		
	}
}
void solve(){
	cin >> k >> n;
	
	ll prefix[n + 10];
	vector<pii>p;
	ll ans = 0;
	ll dummy = 0;
	for (int i = 0; i < n; i++){
		char a,b;
		int c,d;
		cin >> a >> c >> b >> d;
		if (a == b)dummy+=abs(c - d);
		else p.pb(mp(c, d));
	}
	if (p.size() == 0){
		cout<<dummy<<endl;
		return ;
	}
	sort(p.begin(), p.end(), [](auto &x, auto &y){
		return (x.fr + x.sc < y.fr + y.sc);
	});
	n = (int)p.size();
	dummy+= p.size();
	//memset(prefix, 0, sizeof(prefix));
	for (int i = 0; i < n; i++){
		insertion(p[i].fr);
		insertion(p[i].sc);
		prefix[i] = rmas - lmas;
	}
	ans+=prefix[n - 1];    
	if (k == 2){
		while(!lower.empty())lower.erase(lower.find(*lower.begin()));
		while(!upper.empty())upper.erase(upper.find(*upper.begin()));
		rmas = 0, lmas = 0;
		for (int i = n-1; i > 0; i--){
			insertion(p[i].fr);
			insertion(p[i].sc);
			ans = min(ans, prefix[i - 1] + rmas - lmas);
		}
	}
	cout<<ans + dummy;
	
}
 
int main() {
    setIO("");
///  freopen("input.txt", "r", stdin);
///  freopen("output.txt", "w", stdout);
    solve();
    return 0;   
}

Compilation message

bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 2 ms 428 KB Output is correct
5 Correct 2 ms 332 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 340 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 444 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 332 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 2 ms 328 KB Output is correct
12 Correct 84 ms 11984 KB Output is correct
13 Correct 120 ms 13528 KB Output is correct
14 Correct 101 ms 11296 KB Output is correct
15 Correct 63 ms 8092 KB Output is correct
16 Correct 81 ms 12836 KB Output is correct
17 Correct 84 ms 13508 KB Output is correct
18 Correct 69 ms 13188 KB Output is correct
19 Correct 88 ms 13536 KB Output is correct
20 Correct 89 ms 13056 KB Output is correct
21 Correct 91 ms 13260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 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 0 ms 324 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 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 240 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 0 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 2 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 2 ms 404 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 320 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 316 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 324 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 328 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 332 KB Output is correct
20 Correct 1 ms 352 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 352 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 149 ms 11972 KB Output is correct
26 Correct 219 ms 12224 KB Output is correct
27 Correct 229 ms 12936 KB Output is correct
28 Correct 211 ms 13500 KB Output is correct
29 Correct 233 ms 13640 KB Output is correct
30 Correct 86 ms 7216 KB Output is correct
31 Correct 123 ms 12884 KB Output is correct
32 Correct 141 ms 13536 KB Output is correct
33 Correct 128 ms 13124 KB Output is correct
34 Correct 155 ms 13616 KB Output is correct
35 Correct 147 ms 13140 KB Output is correct
36 Correct 143 ms 13300 KB Output is correct
37 Correct 158 ms 12040 KB Output is correct