SRM 554 - 500 TheBrickTowerMediumDivOne
2014-07-02 17:48
513 查看
题目链接:
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; } };
相关文章推荐
- [SRM554-500]TheBrickTowerMediumDivOne
- SRM 552 - 500 FoxAndFlowerShopDivOne
- TopCoder SRM 646 Div2 Problem 500 - TheGridDivTwo (BFS + 优先队列)
- TopCoder SRM 651 Div2 Problem 500 - FoxAndSouvenirTheNext (DP)
- SRM500 DIV Ⅰ
- TopCoder SRM 644 Div2 Problem 500 - LostCharacter (贪心)
- SRM 510 Div1 1000 TheLuckyBasesDivOne
- [博弈论][凸包][复杂度分析] SRM 597 Div1 Medium ConvexPolygonGame
- SRM 500 DIV1 B
- SRM500 DIV2 - SRMCards
- Single Round Match 479 Round 1 - Division I, Level Two TheAirTripDivOne
- SRM 606 div2 500 EllysNumberGuessing
- CSS+DIV布局,一个列宽影响另一个列宽,使总宽度自适应(CSS+DIV Layout, one column's width depend on the other's)
- SRM 649 div2 500(dp)
- Topcoder SRM 582 DIV2 500
- 有向图判环+拆解图求参会期望 SRM 660 Div1 Medium: Privateparty
- Topcoder SRM 549 DIV2 1000 OrderOfTheHats
- SRM 608 div2 500 MysticAndCandiesEasy
- SRM 572 Div 1 500 题解
- 【TP SRM 703 div2 500】 GCDGraph