标签:
题目链接:点击打开链接
思路:我们把元素从大到小排序, 从大到小依次合并区间, 对于第i个数, 如果他相邻左边的数比他大就合并, 相邻右边也一样。这样, 我们就求出了第i个数为最小值的最大区间。 更新答案即可。
细节参见代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include <cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; typedef long long ll; typedef long double ld; const double eps = 1e-6; const double PI = acos(-1); const int mod = 1000000000 + 7; const int INF = 0x3f3f3f3f; const int seed = 131; const ll INF64 = ll(1e18); const int maxn = 2e5 + 10; int T,n,m, a[maxn],p[maxn],cnt[maxn]; struct node { int pos, v; node(int pos=0, int v=0):pos(pos), v(v) {} bool operator < (const node& rhs) const { return v > rhs.v; } }b[maxn]; int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); } int main() { scanf("%d",&n); vector<int> ans; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = node(i, a[i]); p[i] = i; cnt[i] = 1; } sort(b+1, b+n+1); for(int i = 1; i <= n; i++) { int pp = b[i].pos; if(pp > 1 && a[pp] <= a[pp-1]) { int x = _find(pp); int y = _find(pp-1); if(x != y) { p[x] = y; cnt[y] = cnt[x] + cnt[y]; } } if(pp < n && a[pp] <= a[pp+1]) { int x = _find(pp); int y = _find(pp+1); if(x != y) { p[x] = y; cnt[y] = cnt[x] + cnt[y]; } } int y = _find(pp); int len = ans.size(); for(int j = len+1; j <= cnt[y]; j++) { ans.push_back(a[pp]); } } int len = ans.size(); for(int i = 0; i < len; i++) { printf("%d%c", ans[i], i == len-1 ? '\n' : ' '); } return 0; }
Codeforces Round #305 (Div. 1) B. Mike and Feet(并查集)
标签:
原文地址:http://blog.csdn.net/weizhuwyzc000/article/details/51933087