답안 #979869

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
979869 2024-05-11T14:14:52 Z ByeWorld Palembang Bridges (APIO15_bridge) C++14
100 / 100
1408 ms 19276 KB
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int MAXN = 2e5+10;
const int INF = 4e18+10;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
 
int K, n, MX;
int ans, MN = INF;
vector <pii> cc;
vector <int> v;
 
int cek(int idx){
    /*int res = INF;
    for (int x : v) {
        if (x == idx) break;
        int t = 0, a, b;
        for (auto in : cc) {
            a = abs(in.fi - idx) + abs(in.se - idx);
            b = abs(in.fi - x) + abs(in.se - x);
            t += min(a,b);
        }
        res = min(res,t);
    }
    return res;*/
    
    int ret = 0, m = 0, c = 0;
    int mn = 0;
    vector <pii> vec;
    for(auto in : cc){
        if(in.fi <= idx && idx <= in.se){
            ret += in.se-in.fi;
        } else if(idx<=in.fi && idx<=in.se){
            ret += in.se-idx+in.fi-idx;
        } else {
            int a = in.fi, b = in.se;
            vec.pb({a+b-idx, 0});
            vec.pb({a, 1});
            vec.pb({b, 1});
            c += 2*idx-a-b;
        }
    }
    mn = c;
    sort(vec.begin(), vec.end());
    
    for(int i=0; i<vec.size(); i++){
        int nw = vec[i].fi;
        /*while(i+1<vec.size() && vec[i+1].fi == vec[i].fi){
            // upd i
            if(vec[i].se){
                m += 2;
                c += -2*nw;
            } else {
                m -= 2;
                c += 2*nw;
            }
            i++;
        }*/
        // upd i
        if(vec[i].se){
            m += 2;
            c += -2*nw;
        } else {
            m -= 2;
            c += 2*nw;
        }
        
        mn = min(mn, m*nw+c);
    }
    return ret+mn;
    
    // <a[i]+b[i]-idx,<-2,2*a[i]+2*b[i]-2*idx>>
    // <a[i],<2,-2*a[i]>>
    // <b[i],<2,-2*b[i]>>
    
    // <a[i]+b[i]-idx,0>
    // <a[i],1>
    // <b[i],1>
    
    // idx-b[i] <= a[i]-x
    // x <= a[i]+b[i]-idx
    // 2*idx-a[i]-b[i]
    
    // a[i]+b[i]-idx <= x <= a[i]
    // a[i]+b[i]-2x
    
    // a[i] <= x <= b[i]
    // b[i]-a[i]
    
    // b[i] <= x
    // 2x-a[i]-b[i]
    
    // <a[i],i>
    // <b[i],i>
}
void solve(){
	cin >> K >> n;
    if(K==1){
        vector <int> vec;
		vec.pb(-1);
		for(int i=1; i<=n; i++){
			char x, y; int a, b;
			cin >> x >> a >> y >> b; a++; b++;
			if(x==y) ans += abs(a-b);
			else {
				vec.pb(a); vec.pb(b);
			}
		}
		sort(vec.begin(), vec.end()); 
 
		int MN = INF, siz = vec.size()-1;
		for(int i=1; i<=siz; i++){
			ans += abs(vec[(siz+1)/2]-vec[i]);
		}
		ans += siz/2;
		cout << ans << '\n';
        exit(0);
	}
    
    v.pb(-1);
    for(int i=1; i<=n; i++){
        char x, y; int a, b;
        cin >> x >> a >> y >> b; a++; b++;
        if(x==y){
            ans += abs(a-b); continue;
        }
        if(a > b) swap(a, b);
        cc.pb({a, b});
        v.pb(a); v.pb(b);
    }
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end()) - v.begin());
    
    
    int l=1, r=v.size()-1, mid1=0, mid2=0;
    while(r-l+1 >= 40){
        mid1 = (l+l+r)/3; mid2 = (l+r+r+2)/3;
        int num1 = cek(v[mid1]), num2 = cek(v[mid2]);
        // cout << mid1 << " " << mid2 << " " << cek(mid1) << " " << cek(mid2) << endl;
        MN = min(MN, min(num1, num2));
        if(num1 < num2) r = mid2;
        else l = mid1;
    }
    // cout << l << ' ' << r << " lr\n";
    for(int mid=l; mid<=r; mid++){
        // cout << mid << " " << cek(mid) << endl;
        MN = min(MN, cek(v[mid]));
    }
    if(cc.size() == 0) MN = 0;
    cout << ans + MN + (int)cc.size() << '\n';
    
    ans = 0; MN = INF;
    cc.clear(); v.clear();
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    // int T = 1;
    // while(T--){
    //     solve();
    // }
    solve();
}

Compilation message

bridge.cpp: In function 'long long int cek(long long int)':
bridge.cpp:51:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i=0; i<vec.size(); i++){
      |                  ~^~~~~~~~~~~
bridge.cpp: In function 'void solve()':
bridge.cpp:116:7: warning: unused variable 'MN' [-Wunused-variable]
  116 |   int MN = INF, siz = vec.size()-1;
      |       ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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
# 결과 실행 시간 메모리 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 Correct 1 ms 344 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 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 500 KB Output is correct
11 Correct 1 ms 400 KB Output is correct
12 Correct 15 ms 2520 KB Output is correct
13 Correct 33 ms 2604 KB Output is correct
14 Correct 24 ms 2520 KB Output is correct
15 Correct 21 ms 1500 KB Output is correct
16 Correct 21 ms 2520 KB Output is correct
17 Correct 22 ms 2520 KB Output is correct
18 Correct 26 ms 2520 KB Output is correct
19 Correct 29 ms 2520 KB Output is correct
20 Correct 21 ms 2520 KB Output is correct
21 Correct 27 ms 2424 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 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 348 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 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 500 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 0 ms 452 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 4 ms 592 KB Output is correct
15 Correct 6 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 5 ms 604 KB Output is correct
21 Correct 6 ms 852 KB Output is correct
22 Correct 9 ms 600 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 6 ms 348 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 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 4 ms 344 KB Output is correct
15 Correct 5 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 3 ms 348 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 6 ms 600 KB Output is correct
21 Correct 6 ms 600 KB Output is correct
22 Correct 7 ms 604 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 7 ms 592 KB Output is correct
25 Correct 19 ms 6588 KB Output is correct
26 Correct 385 ms 11832 KB Output is correct
27 Correct 1159 ms 11908 KB Output is correct
28 Correct 1008 ms 13316 KB Output is correct
29 Correct 1000 ms 13272 KB Output is correct
30 Correct 513 ms 6824 KB Output is correct
31 Correct 24 ms 5308 KB Output is correct
32 Correct 939 ms 14064 KB Output is correct
33 Correct 1096 ms 19276 KB Output is correct
34 Correct 1408 ms 13776 KB Output is correct
35 Correct 34 ms 12044 KB Output is correct
36 Correct 1121 ms 13508 KB Output is correct
37 Correct 137 ms 18520 KB Output is correct