답안 #951179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951179 2024-03-21T10:06:44 Z JakobZorz Palembang Bridges (APIO15_bridge) C++17
100 / 100
363 ms 25324 KB
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#include<random>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;

vector<ll>calc(vector<pair<int,int>>arr){ // res[i] = how much would it cost if i setup 1 bridge for first i people
    set<int>ptss;
    for(auto[i,j]:arr){
        ptss.insert(i);
        ptss.insert(j);
    }
    vector<int>pts(ptss.begin(),ptss.end());
    vector<ll>res(arr.size()+1,1e18);
    multiset<int>points1,points2;
    int mpc=0; // mid points count
    int br=0;
    ll cr=0;
    for(int i=0;i<=(int)arr.size();i++){
        if(i){
            if(arr[i-1].first==pts[br])
                mpc++;
            else if(arr[i-1].first>pts[br])
                points2.insert(arr[i-1].first);
            else
                points1.insert(arr[i-1].first);
            if(arr[i-1].second==pts[br])
                mpc++;
            else if(arr[i-1].second>pts[br])
                points2.insert(arr[i-1].second);
            else
                points1.insert(arr[i-1].second);
            cr+=abs(arr[i-1].first-pts[br]);
            cr+=abs(arr[i-1].second-pts[br]);
        }
        while(points1.size()+mpc<points2.size()&&br<(int)pts.size()-1){
            cr-=1LL*(pts[br+1]-pts[br])*(points2.size()-points1.size()-mpc);
            while(mpc--)
                points1.insert(pts[br]);
            br++;
            mpc=(int)points2.count(pts[br]);
            points2.erase(pts[br]);
        }
        while(points1.size()>points2.size()+mpc&&br>0){
            cr-=1LL*(pts[br]-pts[br-1])*(points1.size()-points2.size()-mpc);
            while(mpc--)
                points2.insert(pts[br]);
            br--;
            mpc=(int)points1.count(pts[br]);
            points1.erase(pts[br]);
        }
        /*for(int br:pts){
            ll cr=0;
            for(int j=0;j<i;j++)
                cr+=abs(br-arr[j].first)+abs(br-arr[j].second);
            res[i]=min(res[i],cr);
        }
        cout<<res[i]<<" "<<cr<<"\n";*/
        res[i]=cr;
    }
    return res;
}

bool cmp(pair<int,int>a,pair<int,int>b){
    return a.first+a.second<b.first+b.second;
}

void solve(){
    int k,n;
    cin>>k>>n;
    vector<pair<int,int>>arr;
    ll res1=0;
    for(int i=0;i<n;i++){
        char c1,c2;
        int a1,a2;
        cin>>c1>>a1>>c2>>a2;
        if(c1==c2){
            res1+=abs(a1-a2);
        }else{
            res1++;
            if(a1>a2)
                swap(a1,a2);
            arr.emplace_back(a1,a2);
        }
    }
    
    sort(arr.begin(),arr.end(),cmp);
    
    vector<ll>r1=calc(arr);
    reverse(arr.begin(),arr.end());
    vector<ll>r2=calc(arr);
    
    ll res2=1e18;
    if(k==1){
        res2=r1.back();
    }else{ // k == 2
        for(int i=0;i<=(int)arr.size();i++)
            res2=min(res2,r1[i]+r2[(int)arr.size()-i]);
    }
    
    if(arr.size()==0)
        res2=0;
    
    cout<<res1+res2<<"\n";
}
 
int main(){
    ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
    //freopen("/Users/jakob/cp_testing/test.txt","r",stdin);//freopen("output.out","w",stdout);
    int t=1;//cin>>t;
    while(t--)solve();
    return 0;
}
 
 
/*

 
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
 
24

2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
 
22


 */
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 456 KB Output is correct
4 Correct 2 ms 444 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 696 KB Output is correct
8 Correct 2 ms 488 KB Output is correct
9 Correct 1 ms 856 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 64 ms 12832 KB Output is correct
13 Correct 316 ms 25324 KB Output is correct
14 Correct 155 ms 13260 KB Output is correct
15 Correct 170 ms 15028 KB Output is correct
16 Correct 50 ms 9664 KB Output is correct
17 Correct 189 ms 25216 KB Output is correct
18 Correct 159 ms 24860 KB Output is correct
19 Correct 191 ms 24760 KB Output is correct
20 Correct 77 ms 11452 KB Output is correct
21 Correct 189 ms 25008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 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 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 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 1 ms 344 KB Output is correct
8 Correct 0 ms 344 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 344 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 480 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 480 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 65 ms 12928 KB Output is correct
26 Correct 146 ms 13760 KB Output is correct
27 Correct 313 ms 23964 KB Output is correct
28 Correct 363 ms 25204 KB Output is correct
29 Correct 338 ms 25232 KB Output is correct
30 Correct 154 ms 13692 KB Output is correct
31 Correct 54 ms 9604 KB Output is correct
32 Correct 191 ms 24896 KB Output is correct
33 Correct 149 ms 24776 KB Output is correct
34 Correct 210 ms 24512 KB Output is correct
35 Correct 80 ms 11192 KB Output is correct
36 Correct 211 ms 24868 KB Output is correct
37 Correct 17 ms 4208 KB Output is correct