Submission #955157

# Submission time Handle Problem Language Result Execution time Memory
955157 2024-03-29T13:42:18 Z hihihihaw Palembang Bridges (APIO15_bridge) C++17
100 / 100
248 ms 19216 KB
#pragma GCC optimize("O3,unroll-loops")    
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define pb push_back
#define pii pair<int,int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 1223372036854775807
#define MOD 1000000007
#define cint(x) int x;cin>>x;
#define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i];
#define coutarr(a) for (auto d:a) cout<<d<<" "; cout<<endl;
#define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl;
#define AYBERK_SARICA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
 
int t=1;
int usaco=0;
 

void solve(){
    int k,n;
    cin>>k>>n;
    vector<vector<int>> v;
    int ans=0;
    for (int i=0;i<n;i++){
        char c1,c2;
        int x1,x2;
        cin>>c1>>x1>>c2>>x2;
        if (c1==c2) ans+=abs(x1-x2);
        else{
            ans++;
            v.pb({x1+x2,x1,x2});
        }
    }
    if (sz(v)==0){
        cout<<ans<<endl;
        return;
    }
    sort(v.begin(),v.end());
    int pfMed[sz(v)];
    multiset<int> s1;multiset<int> s2;
    int sm1=0,sm2=0;
    for (int i=0;i<sz(v);i++){
        if (i==0){
            if (v[i][1]<v[i][2]){
                s1.insert(v[i][1]);sm1+=v[i][1];
                s2.insert(v[i][2]);sm2+=v[i][2];
            }
            else{
                s2.insert(v[i][1]);sm2+=v[i][1];
                s1.insert(v[i][2]);sm1+=v[i][2];
            }
        }
        else{
            assert(s2.size());
            if (v[i][1]>=*s2.begin()){ s2.insert(v[i][1]);sm2+=v[i][1];}
            else{sm1+=v[i][1];s1.insert(v[i][1]);}
            if (v[i][2]>=*s2.begin()){ s2.insert(v[i][2]);sm2+=v[i][2];}
            else{sm1+=v[i][2];s1.insert(v[i][2]);}
            if (sz(s1)>sz(s2)){
                sm2+=*s1.rbegin();
                s2.insert(*s1.rbegin());
                sm1-=*(prev(s1.end()));
                s1.erase(prev(s1.end()));
            }
            else if (sz(s2)>sz(s1)){
                sm1+=*s2.begin();
                s1.insert(*s2.begin());
                sm2-=*s2.begin();
                s2.erase(s2.begin());
            }
        }
        pfMed[i]=sm2-sm1;
    }
    int sfMed[sz(v)];
    s1.clear();s2.clear();
    sm1=0,sm2=0;
    for (int i=sz(v)-1;i>=0;i--){
        if (i==sz(v)-1){
            if (v[i][1]<v[i][2]){
                s1.insert(v[i][1]);sm1+=v[i][1];
                s2.insert(v[i][2]);sm2+=v[i][2];
            }
            else{
                s2.insert(v[i][1]);sm2+=v[i][1];
                s1.insert(v[i][2]);sm1+=v[i][2];
            }
        }
        else{
            if (v[i][1]>=*s2.begin()){ s2.insert(v[i][1]);sm2+=v[i][1];}
            else{sm1+=v[i][1];s1.insert(v[i][1]);}
            if (v[i][2]>=*s2.begin()){ s2.insert(v[i][2]);sm2+=v[i][2];}
            else{sm1+=v[i][2];s1.insert(v[i][2]);}
            if (sz(s1)>sz(s2)){
                sm2+=*s1.rbegin();
                s2.insert(*s1.rbegin());
                sm1-=*(prev(s1.end()));
                s1.erase(prev(s1.end()));
            }
            else if (sz(s2)>sz(s1)){
                sm1+=*s2.begin();
                s1.insert(*s2.begin());
                sm2-=*s2.begin();
                s2.erase(s2.begin());
            }
        }
        sfMed[i]=sm2-sm1;
    }
    int mn=INF;
    if (sz(v)==1){
        mn=pfMed[0];
    }
    else{
        for (int i=0;i<sz(v)-1;i++){
            mn=min(mn,sfMed[i+1]+pfMed[i]);
        }
    }
    if (k==2)cout<<mn+ans<<endl;
    else cout<<ans+pfMed[sz(v)-1]<<endl;
 
}  
    
 
    
 
 
 
 
 
int32_t main(){
    
    
    //AYBERK_SARICA;
    if (usaco){
        freopen("fortmoo.in", "r", stdin);
        freopen("fortmoo.out", "w", stdout);
    }
    #ifdef Local
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        //freopen("wormsort.out", "w", stdout);
    #endif
    if (t==1) solve();
    else{
        cin>>t;
        while (t--) solve();
    }
    
        
    return 0;
}

Compilation message

bridge.cpp: In function 'int32_t main()':
bridge.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen("fortmoo.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen("fortmoo.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 612 KB Output is correct
7 Correct 2 ms 608 KB Output is correct
8 Correct 1 ms 608 KB Output is correct
9 Correct 2 ms 612 KB Output is correct
10 Correct 2 ms 608 KB Output is correct
11 Correct 1 ms 608 KB Output is correct
12 Correct 114 ms 17556 KB Output is correct
13 Correct 248 ms 19216 KB Output is correct
14 Correct 184 ms 16636 KB Output is correct
15 Correct 116 ms 11256 KB Output is correct
16 Correct 126 ms 19180 KB Output is correct
17 Correct 138 ms 18940 KB Output is correct
18 Correct 122 ms 18448 KB Output is correct
19 Correct 153 ms 19084 KB Output is correct
20 Correct 144 ms 18944 KB Output is correct
21 Correct 152 ms 19196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 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 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 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 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 348 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 608 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 1 ms 604 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 608 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory 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 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 352 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 608 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 448 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 352 KB Output is correct
18 Correct 1 ms 356 KB Output is correct
19 Correct 1 ms 612 KB Output is correct
20 Correct 2 ms 612 KB Output is correct
21 Correct 1 ms 608 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 612 KB Output is correct
24 Correct 1 ms 608 KB Output is correct
25 Correct 111 ms 17412 KB Output is correct
26 Correct 140 ms 17672 KB Output is correct
27 Correct 231 ms 18804 KB Output is correct
28 Correct 224 ms 19072 KB Output is correct
29 Correct 205 ms 18944 KB Output is correct
30 Correct 101 ms 9980 KB Output is correct
31 Correct 115 ms 18184 KB Output is correct
32 Correct 143 ms 18560 KB Output is correct
33 Correct 121 ms 18724 KB Output is correct
34 Correct 158 ms 18952 KB Output is correct
35 Correct 140 ms 18188 KB Output is correct
36 Correct 139 ms 18440 KB Output is correct
37 Correct 113 ms 17348 KB Output is correct