답안 #919418

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919418 2024-01-31T17:54:37 Z phamducminh Palembang Bridges (APIO15_bridge) C++17
100 / 100
338 ms 26936 KB
//******************/
//*   I<3   C++    */
//*  I WANT ANY AC */
//* I LOVE PROGRAM!*/
//*IT'S long longERESTING*/
//* I LOVE PROGRAM!*/
//*  IN CONTESTS   */
//*   GET SCORE    */
//*    AC CODE     */
//*     LET'S      */
//*      GO        */
//*  Written by:   */
//*   Duc Minh     */






#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <string>
#include <queue>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <deque>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define  file(name)  freopen(name".inp", "r", stdin);\
                     freopen(name".out", "w", stdout);
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define TIME        (1.0 * clock() / CLOCKS_PER_SEC)
#define all(a)      a.begin(),a.end()
#define endl        "\n"
#define all1(a)     a+1,a+n+1
// #define unordered_map  map
// #define push_back   emplace_back
// #define gcd(a,b)    __gcd(a,b);
// #define lcm(a,b)    (a*b)/gcd(a,b);

const long long INF = (long long)1e18;
const long long MOD = (long long)1e9+7;
const long long MODD = 1e9; /// 998244353
const long long maxN = 25009;






///--------------------------------


void solve();

signed main(){

    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);


    // file("tbrackets");







    long long t;


    // cin >> t;

    t=1;



    while (t--){




        solve();


    }





    cerr << "Time elapsed: " << TIME << "s.\n";
    cerr << "ducminh" << "\n";
    return 0;
}



///--------------------[PROBLEM SOLUTION]--------------------///


multiset<long long> ss;
struct Compare {
    bool operator() (const long long& a, const long long& b) const {
        return a > b;
    }
};
 
long long n,k,a[200009],pre[200009],suf[200009];
multiset<long long,Compare> down;
multiset<long long> up;
long long sum=0,sum1=0,ans=0,sum2=0;
vector<pair<long long,long long>> pp;






void sub1(){

    ans=0;
    auto it=ss.begin();
 
    if (ss.size()==0) return cout << sum2, void();
 
    advance(it, ss.size()/2);
 
    long long tam=*it;
        
    for (long long x: ss){
     
        ans+=abs(tam-x);
    }
     
    ans+=(ss.size()/2);
    ans+=sum2;
     
    cout << ans;


}

long long suml=0, sumr=0;

inline void push(long long val){

    if (val<*up.begin() || up.size()==0) down.insert(val),suml+=val;
    else up.insert(val),sumr+=val;
 
    if (down.size()-up.size()==3){
 
        long long tam=*down.begin();
        down.erase(down.find(tam)); suml-=tam; sumr+=tam;
        up.insert(tam);
    }
    if (down.size()-up.size()==2) {
        long long tam=*down.begin();
        up.insert(*down.begin()); suml-=tam; sumr+=tam;
        down.erase(down.find(tam));
    }
    if (up.size()>down.size()){
 
        long long tam=*up.begin();
        up.erase(up.find(tam)); suml+=tam; sumr-=tam;
        down.insert(tam);
    }
}

long long get(){

    long long tam=*down.begin();

    return (sumr - 1LL*tam*up.size()) + (1LL*tam*down.size() - suml);
}

void sub2(){

        if (ss.size()==0) return cout << sum2, void();
 
        vector<long long> luu;
        for (long long x: ss) luu.push_back(x);
 


        for (long long i=0; i<pp.size(); i++){
            push(pp[i].first); push(pp[i].second);
            pre[i]=get();
        }
 

 
        down.clear(); up.clear();
        suml=0; sumr=0;


        for (long long i=pp.size()-1; i>=0; i--){

            push(pp[i].first); push(pp[i].second);
            suf[i]=get();
        }
 


        long long pos=0;
 
        ans=INF;
        for (long long i=0; i<pp.size()-1; i++){
 
            ans=min(ans,pre[i]+suf[i+1]);
        }

        // cout << ans << ' ' << suf[pos] << "\n";
 
        ans+=(luu.size()/2);
        ans+=sum2;
     
        cout << ans;

 
    
}


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

void solve(){

    
    
    

    long long k,n;
 
    cin >> k >> n;
 
    for (long long i=1; i<=n; i++){
 
        char ch1,ch2;
        long long x,y;
 
        cin >> ch1 >> x >> ch2 >> y;
 
 
        if (ch1!=ch2)  {
            ss.insert(x); ss.insert(y);  pp.push_back({x, y});
            sum+=x+y;
        }
        else sum1+=2,sum2+=abs(y-x);
 
        // pp.push_back({x, y});
    }

    sort(all(pp),cmp);
 
    if (k==1) sub1();
    else sub2();
 


}








Compilation message

bridge.cpp: In function 'void sub2()':
bridge.cpp:197:30: 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]
  197 |         for (long long i=0; i<pp.size(); i++){
      |                             ~^~~~~~~~~~
bridge.cpp:219:30: 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]
  219 |         for (long long i=0; i<pp.size()-1; i++){
      |                             ~^~~~~~~~~~~~
bridge.cpp:216:19: warning: unused variable 'pos' [-Wunused-variable]
  216 |         long long pos=0;
      |                   ^~~
# 결과 실행 시간 메모리 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 344 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 51 ms 11452 KB Output is correct
13 Correct 100 ms 11700 KB Output is correct
14 Correct 102 ms 10300 KB Output is correct
15 Correct 44 ms 6916 KB Output is correct
16 Correct 54 ms 11196 KB Output is correct
17 Correct 56 ms 11344 KB Output is correct
18 Correct 61 ms 11200 KB Output is correct
19 Correct 75 ms 11364 KB Output is correct
20 Correct 58 ms 11664 KB Output is correct
21 Correct 83 ms 11420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2400 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 2552 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 2 ms 2648 KB Output is correct
14 Correct 2 ms 2648 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 2 ms 2648 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 2 ms 2648 KB Output is correct
23 Correct 2 ms 2648 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 500 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2648 KB Output is correct
15 Correct 2 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 2 ms 2648 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2648 KB Output is correct
23 Correct 2 ms 2852 KB Output is correct
24 Correct 2 ms 2648 KB Output is correct
25 Correct 153 ms 26936 KB Output is correct
26 Correct 232 ms 26612 KB Output is correct
27 Correct 289 ms 26424 KB Output is correct
28 Correct 293 ms 26516 KB Output is correct
29 Correct 338 ms 26584 KB Output is correct
30 Correct 108 ms 14224 KB Output is correct
31 Correct 140 ms 26428 KB Output is correct
32 Correct 159 ms 26432 KB Output is correct
33 Correct 124 ms 26484 KB Output is correct
34 Correct 201 ms 26732 KB Output is correct
35 Correct 166 ms 26480 KB Output is correct
36 Correct 180 ms 26588 KB Output is correct
37 Correct 151 ms 26480 KB Output is correct