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;}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 促进胃肠蠕动穴位 女儿跨坐吞吐蠕动 魔妇耸弄夹吸蠕动采补 第章女儿跨坐吞吐蠕动 母亲娇嫩紧窄抽搐蠕动 腿上感觉里面有东西在蠕动 瘟疫公司蠕虫 死亡蠕虫图片 蠖怎么读 蠖怎么读音 蠡湖 蠡沟 蠡怎么读 以蠡测海 蠡沟穴 蠡湖股份股吧 蠡读音 蠡湖股份 蠡湖中央公园旅游 蠡湖公园 蠡的读音 300694蠡湖股份 秦蠡论股 蠡湖怎么读 蠡湖中央公园 蠡玉35 蠡怎么读音 龙湖蠡湖天著 蠡湖瑞仕花园 蠡湖香樟园 蠡湖院子 蠡湖8号 蠡怎么读音是什么 蠡县 蠡县招聘 蠡县吧 蠡县职教 蠡县邮编 蠡县中学