이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "wiring.h"
ll n,m;
vector<int>A,B;
ll minA[100005],minB[100005];
ll dp[200005];
ll Next[200005];
ll pre[200005] = {0};
ll Pre[200005] = {0};
ll solve(ll idx){
if(idx == n+m){
return 0;
}
if(dp[idx] != -1){
return dp[idx];
}
ll ans = solve(idx + 1);
ll posa = Next[idx] - idx;
if(Next[idx] + posa <= n + m){
ans = max(ans,solve(Next[idx] + posa) + (pre[Next[idx] + posa] - pre[idx]) + (Pre[Next[idx]] - Pre[idx]) - (Pre[Next[idx] + posa] - Pre[Next[idx]]));
}
return dp[idx] = ans;
}
long long min_total_length(std::vector<int> r, std::vector<int> b){
memset(dp,-1,sizeof dp);
A = r;
B = b;
n = A.size();
m = B.size();
ll ans = 0;
ll pos = 0;
vector<iii>ola;
f(i,0,n){
while(pos < B.size() && B[pos] < A[i]){
pos++;
}
minA[i] = INF;
if(pos != B.size()){
minA[i] = abs(A[i] - B[pos]);
}
if(pos != 0){
minA[i] = min(minA[i],(ll)abs(A[i] - B[pos - 1]));
}
ans += minA[i];
ola.pb(iii(ii(A[i],0),minA[i]));
}
pos = 0;
f(i,0,m){
while(pos < A.size() && A[pos] < B[i]){
pos++;
}
minB[i] = INF;
if(pos != A.size()){
minB[i] = abs(B[i] - A[pos]);
}
if(pos != 0){
minB[i] = min(minB[i],(ll)abs(B[i] - A[pos - 1]));
}
ans += minB[i];
ola.pb(iii(ii(B[i],1),minB[i]));
}
sort(all(ola));
Next[n + m - 1] = n + m;
for(ll i = n + m - 2;i >= 0;i--){
Next[i] = Next[i+1];
if(ola[i].F.S != ola[i+1].F.S){
Next[i] = i+1;
}
}
f(i,0,n+m){
Pre[i+1] = Pre[i] + ola[i].F.F;
pre[i+1] = pre[i] + ola[i].S;
}
return ans - solve(0);
}
/*
int main() {
int n, m;
assert(2 == scanf("%d %d", &n, &m));
vector<int> r(n), b(m);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &r[i]));
for(int i = 0; i < m; i++)
assert(1 == scanf("%d", &b[i]));
long long res = min_total_length(r, b);
printf("%lld\n", res);
return 0;
}
4 5
1 2 3 7
0 4 5 9 10
*/
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:58:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(pos < B.size() && B[pos] < A[i]){
| ~~~~^~~~~~~~~~
wiring.cpp:62:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if(pos != B.size()){
| ~~~~^~~~~~~~~~~
wiring.cpp:73:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | while(pos < A.size() && A[pos] < B[i]){
| ~~~~^~~~~~~~~~
wiring.cpp:77:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if(pos != A.size()){
| ~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |