답안 #953624

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
953624 2024-03-26T10:54:33 Z PieArmy Palembang Bridges (APIO15_bridge) C++17
100 / 100
160 ms 14116 KB
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}

multiset<int>s1,s2;

void dengele(){
    while(s2.size()>s1.size()){
        s1.insert(*s2.begin());
        s2.erase(s2.begin());
    }
    while(ll(s1.size())-1>s2.size()){
        s2.insert(*s1.rbegin());
        s1.erase(--s1.end());
    }
}

void code(){
    int k,n;cin>>k>>n;
    ll ans=0;
    vector<pair<int,int>>v={{sonsuz,sonsuz}};
    for(int i=0;i<n;i++){
        char a,b;
        int x,y;
        cin>>a>>x>>b>>y;
        if(a==b){
            ans+=abs(x-y);
        }
        else{
            ans++;
            v.pb({min(x,y),max(x,y)});
        }
    }
    n=v.size()-1;
    if(k==1){
        vector<int>arr;
        for(pair<int,int>x:v){
            if(x.fr==sonsuz)continue;
            arr.pb(x.fr);
            arr.pb(x.sc);
        }
        sort(arr.begin(),arr.end());
        for(int x:arr)
            ans+=abs(arr[n]-x);
        cout<<ans;
        return;
    }
    sort(v.begin(),v.end(),[&](const pair<int,int>&x,const pair<int,int>&y){return x.fr+x.sc<y.fr+y.sc;});
    ll dp[n+1];dp[0]=0;
    int med=-1;
    for(int i=1;i<=n;i++){
        if(s1.size()&&v[i].fr>*s1.rbegin())
            s2.insert(v[i].fr);
        else s1.insert(v[i].fr);
        if(s1.size()&&v[i].sc>*s1.rbegin())
            s2.insert(v[i].sc);
        else s1.insert(v[i].sc);
        dp[i]=dp[i-1];
        dp[i]+=abs(v[i].fr-med)+abs(v[i].sc-med);
        dengele();
        if(*s1.rbegin()>med){
            dp[i]+=abs(med-(*s1.rbegin()))*(s1.size()-s2.size()-2);
        }
        med=*s1.rbegin();
    }
    s1.clear();s2.clear();
    ll cur=0;
    med=-1;
    for(int i=n;i;i--){
        if(s1.size()&&v[i].fr>*s1.rbegin())
            s2.insert(v[i].fr);
        else s1.insert(v[i].fr);
        if(s1.size()&&v[i].sc>*s1.rbegin())
            s2.insert(v[i].sc);
        else s1.insert(v[i].sc);
        cur+=abs(v[i].fr-med)+abs(v[i].sc-med);
        dp[i-1]+=cur;
        dengele();
        if(*s1.rbegin()>med){
            cur+=abs(med-(*s1.rbegin()))*(s1.size()-s2.size()-2);
        }
        med=*s1.rbegin();
    }
    ll ek=inf;
    for(ll x:dp){
        ek=min(ek,x);
    }
    cout<<ans+ek;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
    int t=1;
    if(!t)cin>>t;
    while(t--){code();cout<<endl;}
    return 0;
}

Compilation message

bridge.cpp: In function 'void dengele()':
bridge.cpp:21:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::multiset<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     while(ll(s1.size())-1>s2.size()){
      |           ~~~~~~~~~~~~~~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:102:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:102:60: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |     bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 468 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 348 KB Output is correct
3 Correct 0 ms 460 KB Output is correct
4 Correct 1 ms 472 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 600 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 496 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 20 ms 3528 KB Output is correct
13 Correct 34 ms 4996 KB Output is correct
14 Correct 24 ms 3800 KB Output is correct
15 Correct 20 ms 2944 KB Output is correct
16 Correct 18 ms 4264 KB Output is correct
17 Correct 23 ms 4824 KB Output is correct
18 Correct 25 ms 4564 KB Output is correct
19 Correct 32 ms 5064 KB Output is correct
20 Correct 22 ms 4548 KB Output is correct
21 Correct 28 ms 4608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 460 KB Output is correct
8 Correct 0 ms 344 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 1 ms 348 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 1 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 0 ms 348 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 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 1 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 0 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 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 2 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 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 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 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 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 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 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 468 KB Output is correct
20 Correct 1 ms 584 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 90 ms 11980 KB Output is correct
26 Correct 160 ms 12232 KB Output is correct
27 Correct 155 ms 12944 KB Output is correct
28 Correct 149 ms 14116 KB Output is correct
29 Correct 138 ms 13516 KB Output is correct
30 Correct 64 ms 7120 KB Output is correct
31 Correct 73 ms 12752 KB Output is correct
32 Correct 99 ms 13260 KB Output is correct
33 Correct 63 ms 13040 KB Output is correct
34 Correct 100 ms 13048 KB Output is correct
35 Correct 94 ms 12744 KB Output is correct
36 Correct 104 ms 13004 KB Output is correct
37 Correct 94 ms 11984 KB Output is correct