답안 #405055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
405055 2021-05-15T16:17:12 Z Hazem Palembang Bridges (APIO15_bridge) C++14
31 / 100
2000 ms 5108 KB
#include <bits/stdc++.h>
using namespace std;
 
#define LL long long
#define F first
#define S second
#define pii pair<int,int>
#define piii pair<pair<int,int>,int>

const int N = 3e5+10;
const int M = 3e2+10;
const LL INF = 1e9;
const LL LINF = 2e18;
const LL MOD = 1e9+7;   
const double PI = 3.141592653589793;

vector<LL>l,r;
LL pr[N],pr1[N];
vector<pair<LL,LL>>vec;

LL calc(LL p){

    LL ret = 0;
    LL idx = upper_bound(r.begin(),r.end(),p)-r.begin()-1;
    if(idx>=0)
        ret += p*(idx+1)-pr1[idx];
    
    idx = upper_bound(l.begin(),l.end(),p)-l.begin();
    if(idx!=l.size())
        ret += pr[idx]-p*(l.size()-idx);
    
    return ret;
}

LL get_dis(pair<LL,LL>p,LL p1){

    if(p1<p.F)
        return p.F-p1;
    if(p1>p.S)
        return p1-p.S;
    return 0;
}

LL calc(LL p,LL p1){

    LL ret = 0;
    for(auto x:vec)
        ret += min(get_dis(x,p),get_dis(x,p1));

    return ret;
}

void ans1(LL ans){


    sort(l.begin(),l.end());
    sort(r.begin(),r.end());

    for(int i=l.size()-1;i>=0;i--)
        pr[i] = pr[i+1]+l[i];

    for(int i=0;i<r.size();i++)
        pr1[i] = (i?pr1[i-1]:0)+r[i];

    LL mn = l.size()?LINF:0;
    for(auto x:l)
        mn = min(mn,calc(x));
    for(auto x:r)
        mn = min(mn,calc(x));
    
    printf("%lld\n",ans+mn*2);

}

void ans2(LL ans){

    vector<LL>points;
    for(auto x:vec)
        points.push_back(x.F),points.push_back(x.S);

    sort(points.begin(),points.end());
    LL mn = points.size()?LINF:0;
    for(int i=0;i<points.size();i++)
        for(int j=i;j<points.size();j++)
            mn = min(mn,calc(points[i],points[j]));
    
    printf("%lld\n",ans+mn*2);
}

int main(){

    //freopen("out.txt","w",stdout);

    int k,n;
    scanf("%d%d",&k,&n);

    LL ans = 0;
    for(int i=1;i<=n;i++){
        char c1,c2;
        LL d1,d2;
        cin>>c1>>d1>>c2>>d2;
        ans += abs(d1-d2);
        if(c1==c2)continue;
        ans++;
        l.push_back({min(d1,d2)});
        r.push_back(max(d1,d2));
        vec.push_back({min(d1,d2),max(d1,d2)});
    }

    if(k==1)ans1(ans);
    else ans2(ans);
}   

Compilation message

bridge.cpp: In function 'long long int calc(long long int)':
bridge.cpp:29:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     if(idx!=l.size())
      |        ~~~^~~~~~~~~~
bridge.cpp: In function 'void ans1(long long int)':
bridge.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for(int i=0;i<r.size();i++)
      |                 ~^~~~~~~~~
bridge.cpp: In function 'void ans2(long long int)':
bridge.cpp:83:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for(int i=0;i<points.size();i++)
      |                 ~^~~~~~~~~~~~~~
bridge.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         for(int j=i;j<points.size();j++)
      |                     ~^~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     scanf("%d%d",&k,&n);
      |     ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 3 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 70 ms 5020 KB Output is correct
13 Correct 144 ms 5052 KB Output is correct
14 Correct 95 ms 4620 KB Output is correct
15 Correct 88 ms 3260 KB Output is correct
16 Correct 97 ms 5060 KB Output is correct
17 Correct 132 ms 5032 KB Output is correct
18 Correct 115 ms 5032 KB Output is correct
19 Correct 145 ms 5108 KB Output is correct
20 Correct 119 ms 5044 KB Output is correct
21 Correct 134 ms 5028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 4 ms 204 KB Output is correct
4 Correct 4 ms 204 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 4 ms 204 KB Output is correct
8 Correct 6 ms 296 KB Output is correct
9 Correct 5 ms 296 KB Output is correct
10 Correct 8 ms 308 KB Output is correct
11 Correct 8 ms 332 KB Output is correct
12 Correct 6 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 3 ms 204 KB Output is correct
4 Correct 4 ms 212 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 4 ms 292 KB Output is correct
8 Correct 5 ms 204 KB Output is correct
9 Correct 5 ms 300 KB Output is correct
10 Correct 7 ms 296 KB Output is correct
11 Correct 6 ms 204 KB Output is correct
12 Correct 6 ms 204 KB Output is correct
13 Execution timed out 2075 ms 332 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 3 ms 204 KB Output is correct
4 Correct 4 ms 292 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 4 ms 204 KB Output is correct
8 Correct 6 ms 304 KB Output is correct
9 Correct 5 ms 204 KB Output is correct
10 Correct 7 ms 204 KB Output is correct
11 Correct 5 ms 204 KB Output is correct
12 Correct 9 ms 204 KB Output is correct
13 Execution timed out 2072 ms 332 KB Time limit exceeded
14 Halted 0 ms 0 KB -