3 solutions
-
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, x[N], y[N]; int main() { ios::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> n; for (int i = 1;i <= n;i++) { cin >> x[i]; } for (int i = 1;i <= n;i++) { cin >> y[i]; } sort (x, x + n + 1); sort (y, y + n + 1); int xmid = x[(n + 1) / 2]; int ymid = y[(n + 1) / 2]; long long ans = 0; for (int i = 1;i <= n;i++) { ans += abs(x[i] - xmid) + abs(y[i] - ymid); } cout << ans; return 0; } -
-2
using namespace std; const int N = 1e5 + 5; int n, x[N], y[N]; int main() { ios::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> n; for (int i = 1;i <= n;i++) { cin >> x[i]; } for (int i = 1;i <= n;i++) { cin >> y[i]; } sort (x, x + n + 1); sort (y, y + n + 1); int xmid = x[(n + 1) / 2]; int ymid = y[(n + 1) / 2]; long long ans = 0; for (int i = 1;i <= n;i++) { ans += abs(x[i] - xmid) + abs(y[i] - ymid); } cout << ans; return 0; } -
-3
```cpp 输入加速,n=1e5; 两次循环; 实现中位数贪心; 输出; #include<bits/stdc++.h> using namespace std; const int N=1e5+5; int x[N],y[N]; int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; for(int i=1;i<=n;i++)cin>>x[i]; for(int i=1;i<=n;i++)cin>>y[i]; sort(x+1,x+n+1);sort(y+1,y+n+1); int xmid =x[(n+1)/2]; int ymid =y[(n+1)/2]; long long ans=0; for(int i=1;i<=n;i++)ans+=abs(x[i]-xmid)+abs(y[i]-ymid); cout<<ans; return 0;}
- 1
Information
- ID
- 1090
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 6
- Tags
- # Submissions
- 48
- Accepted
- 14
- Uploaded By