Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-19

SkipRope

| 17:48

問題文, SRM 172

身長が最も近い2人を選ぶ。コードは全要素をわざわざソートするという無駄な処理を行っている。41.67%と結構Sucess Rateが低い。

228.19/250

bool compDiff(const pair<int,int>& a, const pair<int,int>& b) {
    if (a.second < b.second) {
        return true;
    } else if (a.second > b.second) {
        return false;
    } else {
        return a.first > b.first;
    }
}

class SkipRope {
public:
    vector <int> partners(vector <int> candidates, int height) {
        vector<pair<int,int> > diff(candidates.size());
        for (int i = 0; i < candidates.size(); i++)
            diff[i] = make_pair(candidates[i], abs(candidates[i]-height));
        sort(diff.begin(), diff.end(), compDiff);
        vector <int> result;
        for (int i = 0; i < 2; i++) result.push_back(diff[i].first);
        if (result[0] > result[1]) swap(result[0], result[1]);
        return result;
    }
};

RenaRena2011/07/22 11:40Articles like this really grease the shftas of knowledge.

mtosqkksxfnmtosqkksxfn2011/07/22 18:19nGge95 <a href="http://htbsbsjhateo.com/">htbsbsjhateo</a>

vouvotvouvot2011/07/24 22:10AbhEFB <a href="http://udtjnaqmvoon.com/">udtjnaqmvoon</a>

lraetvllraetvl2011/07/25 02:03Ueilew , [url=http://ofkchvqjbqph.com/]ofkchvqjbqph[/url], [link=http://mhzhqovckhxx.com/]mhzhqovckhxx[/link], http://kgxfqnawhoqk.com/