LLL和小猫
Description
LLL有?群貓友,每只?貓都站在坐標(biāo)軸上的某個(gè)位置,這群貓友很聽(tīng)LLL的話,每當(dāng)LLL做個(gè)?勢(shì),每只?貓
都會(huì)移動(dòng)恰好X個(gè)單位的距離,要么向左,要么向右
現(xiàn)在告訴你每只?貓?jiān)谝苿?dòng)前的位置,求移動(dòng)之后最左邊的貓與最右邊的貓的最?距離
Input
?多組測(cè)試數(shù)據(jù) 對(duì)于每組數(shù)據(jù),第??輸??個(gè)整數(shù)n (1<=n<=50 ),表?貓的數(shù)量 第??輸?n個(gè)數(shù)pi (-1e8<=pi<=1e8),表?每只貓的位置 第三?輸??個(gè)整數(shù) X(0<=X<=1e8 )
Output
?對(duì)于每組數(shù)據(jù)輸出?個(gè)整數(shù)
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; }?
總結(jié)