SRM 554 - 500 TheBrickTowerMediumDivOne

题目链接:

http://community.topcoder.com/stat?c=problem_statement&pm=12161


题目大意:

对一个序列做重排列,找出权值最小的一个排列,权值相同的情况下选字典序最小的。

一个排列的权值是任相邻两元素中的较大值的和。


算法:

显然一个排列的权值是有n-1个数相加而成的,每个数最少出现0次,最多出现2次。

但是一个数如果出现0次,必定是由比它大的数代替的。

所以最理想的情况是除最小数外的每个数都出现了一次。

所以最佳的排列是一个先递减再递增的序列。

因为要保证字典序最小,所以要先贪心的找出一个字典序尽量小的递减序列,然后把剩下的数排序放在后面。


代码:

#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <climits>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;

vector<pair<int, int> > tmp;
vector<int> ans;

class TheBrickTowerMediumDivOne
{
public:
    vector <int> find(vector <int> heights)
    {
        ans.clear();
        tmp.clear();
        if (heights.size() == 1)
        {
            ans.push_back(0);
            return ans;
        }
        else if (heights.size() == 2)
        {
            ans.push_back(0);
            ans.push_back(1);
            return ans;
        }
        else if (heights.size() == 3)
        {
            ans.push_back(0);
            ans.push_back(1);
            ans.push_back(2);
            if (heights[1] > heights[0] && heights[1] > heights[2])
            {
                swap(ans[1], ans[2]);
            }
            return ans;
        }
        int lim = INT_MAX;
        for (int i = 0; i < heights.size(); i ++)
        {
            if (heights[i] <= lim)
            {
                lim = heights[i];
                ans.push_back(i);
            }
            else
            {
                tmp.push_back(make_pair(heights[i], i));
            }
        }
        sort(tmp.begin(), tmp.end());
        for (int i = 0; i < tmp.size(); i ++)
        {
            ans.push_back(tmp[i].second);
        }
        return ans;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值