Codeforces Round #305 (Div. 1) B. Mike and Feet(并查集)
2016-07-17 16:53
399 查看
题目链接:点击打开链接
思路:我们把元素从大到小排序, 从大到小依次合并区间, 对于第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;
}
思路:我们把元素从大到小排序, 从大到小依次合并区间, 对于第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;
}
相关文章推荐
- 1611:The Suspects
- 作业四,1001
- 作业四1002
- 作业四1003
- 练习四1005
- 最小权值和
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- HDU-1213-How Many Tables
- Longest Consecutive Sequence,Distinct Subsequences,Interleaving String,Scramble String
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- Codeforces Round #265 (Div. 2)
- 并查集_POJ 1182_食物链