The Salesman
来源:互联网 发布:贵州大数据发展现状 编辑:程序博客网 时间:2024/05/22 06:45
题目:https://www.hackerrank.com/contests/world-codesprint-12/challenges/the-salesman
思路:贪心
#include <bits/stdc++.h>using namespace std;int minimumTime(vector <int> x) { // Return the minimum time needed to visit all the houses. sort(x.begin(), x.end()); vector<int> diff; for(int i=1; i<x.size(); i++) { diff.push_back(x[i]-x[i-1]); } sort(diff.begin(), diff.end()); int cost = 0; for(int i=0; i<x.size()-1; i++) { cost += diff[i]; } //cout << cost << endl; return cost;}int main() { int t; cin >> t; for(int a0 = 0; a0 < t; a0++){ int n; cin >> n; vector<int> x(n); for(int x_i = 0; x_i < n; x_i++){ cin >> x[x_i]; } int result = minimumTime(x); cout << result << endl; } return 0;}
阅读全文