#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e3+5;
const ll INF=1e15+9;
vector<pair<ll, ll> > v[2];
int n;
ll dist(int a, int b, ll x) {
if(b>=v[a].size()) return INF;
ll resp=0;
auto ponto=v[a][b];
resp+=abs(ponto.first-x);
resp+=min(abs(ponto.second-2), abs(ponto.second-1));
return resp;
}
int main() {
scanf("%d", &n); n*=2;
for(int i=1; i<=n; i++) {
int a, b; scanf("%d %d", &a, &b);
if(b>=2) v[0].push_back({a, b});
else v[1].push_back({a, b});
}
sort(v[0].begin(), v[0].end());
sort(v[1].begin(), v[1].end());
ll respf=0; int a=0; int b=0;
for(int i=1; i<=n/2; i++) {
ll val=dist(0, a, i)+dist(1, b, i);
ll val2=dist(0, a, i)+dist(0, a+1, i)+1;
ll val3=dist(1, b, i)+dist(1, b+1, i)+1;
// printf("%d %d %d >> %lld %lld %lld\n", i, a, b, val, val2, val3);
if(val<=val2&&val<=val3) {
respf+=val;
a++; b++;
}
else if(val2<=val&&val2<=val3) {
respf+=val2;
a+=2;
}
else {
respf+=val3;
b+=2;
}
}
printf("%lld\n", respf);
}
Compilation message
joi2019_ho_t4.cpp: In function 'll dist(int, int, ll)':
joi2019_ho_t4.cpp:11:6: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(b>=v[a].size()) return INF;
~^~~~~~~~~~~~~
joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n); n*=2;
~~~~~^~~~~~~~~~
joi2019_ho_t4.cpp:22:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b; scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
396 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
396 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
396 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |