이 제출은 이전 버전의 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 dp[205][205];
ll minA[205],minB[205];
ll solve(ll i,ll j){
if(i == n && j == m){
return 0;
}
if(dp[i][j] != -1){
return dp[i][j];
}
ll ans = INF;
if(i != n && j != m){
ll pos = i+1;
ll posa = 0;
while(pos < n && A[pos] < max(A[i],B[j])){
posa += minA[pos];
pos++;
}
ll posi = j+1;
while(posi < m && B[posi] < max(A[i],B[j])){
posa += minB[posi];
posi++;
}
ans = posa + solve(pos,posi) + abs(A[i] - B[j]);
}
if(i != n){
ans = min(ans,solve(i + 1,j) + minA[i]);
}
if(j != m){
ans = min(ans,solve(i,j + 1) + minB[j]);
}
return dp[i][j] = 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 pos = 0;
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]));
}
}
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]));
}
}
return solve(0,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:69: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]
69 | while(pos < B.size() && B[pos] < A[i]){
| ~~~~^~~~~~~~~~
wiring.cpp:73: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]
73 | if(pos != B.size()){
| ~~~~^~~~~~~~~~~
wiring.cpp:82: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]
82 | while(pos < A.size() && A[pos] < B[i]){
| ~~~~^~~~~~~~~~
wiring.cpp:86: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]
86 | 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... |