LLL和小猫
Description
LLL有?群貓友,每只?貓都站在坐標軸上的某個位置,這群貓友很聽LLL的話,每當LLL做個?勢,每只?貓
都會移動恰好X個單位的距離,要么向左,要么向右
現在告訴你每只?貓在移動前的位置,求移動之后最左邊的貓與最右邊的貓的最?距離
Input
?多組測試數據 對于每組數據,第??輸??個整數n (1<=n<=50 ),表?貓的數量 第??輸?n個數pi (-1e8<=pi<=1e8),表?每只貓的位置 第三?輸??個整數 X(0<=X<=1e8 )
Output
?對于每組數據輸出?個整數
Sample Input
3 -3 0 1 3 3 4 7 -7 5 2 -100000000 100000000 100000000 9 3 7 4 6 -10 7 10 9 -5 7 4 -4 0 4 0 4 1 7 0Sample Output
3 4 0 7 4 0HINT
?
?
題解:
?
#ifndef debuging #define FIN ; #define FOUT ; #define OUT(x) ; #define ERR(x) ; #endif #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <vector> #include <map> #include <set> #include <queue> using namespace std ; long long a[100]; class TaroFriends { public: int getNumber(vector <int> coordinates, int X) { sort(coordinates.begin(),coordinates.end()); int n = coordinates.size(); long long ans = coordinates[n-1] - coordinates[0]; for(int i = 0;i < n-1;i++) { for(int j = 0;j <= i;j++) a[j] = coordinates[j] + X; for(int j = i+1;j < n;j++) a[j] = coordinates[j] - X; sort(a,a+n); ans = min(ans, a[n-1] - a[0] ); } return (int)ans; } }; int main () {int n;while (cin >> n) {vector <int> a(n);for (int i = 0; i < n; i++) {cin >> a[i];}int d; cin >> d;TaroFriends test;cout << test.getNumber(a, d) << endl;}return 0; }?
總結