Submission #895997

#TimeUsernameProblemLanguageResultExecution timeMemory
895997Andrey전선 연결 (IOI17_wiring)C++14
100 / 100
62 ms13908 KiB
#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;

long long min_total_length(std::vector<int> r, std::vector<int> b) {
    long long n = r.size(),m = b.size();
    vector<pair<long long,long long>> haha(n+m+1,{-1,-1});
    for(long long i = 0; i < r.size(); i++) {
        haha[i] = {r[i],0};
    }
    for(long long i = 0; i < m; i++) {
        haha[i+n] = {b[i],1};
    }
    sort(haha.begin(),haha.end());
    vector<long long> wow(n+m+1);
    vector<long long> wut(n+m+1);
    long long c = 0,sb = 0;
    for(long long i = 1; i <= n+m; i++) {
        if(i > 1 && haha[i].second != haha[i-1].second) {
            c = haha[i].first;
            sb = 0;
        }
        sb+=haha[i].first-c;
        wow[i] = sb;
    }
    c = haha[n+m].first,sb = 0;
    for(long long i = n+m; i >= 1; i--) {
        if(i < n+m && haha[i].second != haha[i+1].second) {
            c = haha[i].first;
            sb = 0;
        }
        sb+=c-haha[i].first;
        wut[i] = sb;
    }
    vector<long long> dp(n+m+1);
    long long l = 0,sm = LLONG_MAX/2;
    priority_queue<pair<long long,long long>> bruh;
    for(long long i = 1; i <= n+m; i++) {
        if(i != 1 && haha[i].second != haha[i-1].second) {
            while(!bruh.empty()) {
                bruh.pop();
            }
            sm = LLONG_MAX/2;
            for(long long j = l; j < i; j++) {
                bruh.push({-(dp[j]+wut[min(j+1,i-1)]+(i-j-1)*(haha[i].first-haha[i-1].first)),i-j-1});
            }
            l = i-1;
        }
        sm+=haha[l+1].first-haha[l].first;
        dp[i] = LLONG_MAX/2;
        while(!bruh.empty() && bruh.top().second < i-l) {
            sm = min(sm,-bruh.top().first+(i-l-bruh.top().second)*(haha[l+1].first-haha[l].first));
            bruh.pop();
        }
        dp[i] = min(sm,dp[i]);
        if(!bruh.empty()) {
            dp[i] = min(dp[i],-bruh.top().first);
        }
        dp[i]+=wow[i];
    }
	return dp[n+m];
}

Compilation message (stderr)

wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:8:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |     for(long long i = 0; i < r.size(); i++) {
      |                          ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...