POJ 2796 Feel Good(并查集)

来源:互联网 发布:spss软件中文版 编辑:程序博客网 时间:2024/06/07 06:48

题目链接:点击打开链接   思路:该题转化一下, 就是枚举每一个数, 找到以这个数为最小值的最大区间(因为没有负数)。  那么一个办法是预处理出每一个数左边第一个比他大的数的位置, 和右边第一个比他大的数的位置, 这个可以用构造单调栈的线性算法处理出来: 我们构造一个单调上升栈, 标记栈里每个元素在实际中的位置, 加入一个元素a[i]的时候, 如果栈顶元素大于他, 那么将栈顶元素出队列, i就是这个元素右边大于他的第一个元素。  直到栈顶元素<=a[i],这时,栈顶元素就是a[i]左边第一个>=他的, 把a[i]入队列。 复杂度O(n)             还有一个思维复杂度低的方法, 我们从大到小访问每一个元素, 如果他左边或者右边的元素>=他, 他们将他们合并, 这样, 符合条件的集合就是一个连续区间。 并且, 先访问的一定>=后访问的, 每次更新答案即可。

  1. typedef long long ll;  
  2. typedef long double ld;  
  3. const double eps = 1e-6;  
  4. const double PI = acos(-1);  
  5. const int mod = 1000000000 + 7;  
  6. const int INF = 0x3f3f3f3f;  
  7. const int seed = 131;  
  8. const ll INF64 = ll(1e18);  
  9. const int maxn = 1e5 + 10;  
  10. int T,n,m,p[maxn];  
  11. int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }  
  12. ll sum[maxn],a[maxn];  
  13. struct node {  
  14.     int l, r, v;  
  15.     node(int l=0, int r=0, int v=0):l(l), r(r), v(v) {}  
  16. }res[maxn];  
  17. struct edge {  
  18.     int pos, v;  
  19.     edge(int pos=0, int v=0):pos(pos), v(v) {}  
  20.     bool operator < (const edge& rhs) const {  
  21.         return v > rhs.v;  
  22.     }  
  23. }b[maxn];  
  24. int main() {  
  25.     while(~scanf("%d",&n)) {  
  26.         ll ans = 0, l = 1, r = 1;  
  27.         for(int i = 1; i <= n; i++) {  
  28.             scanf("%I64d", &a[i]);  
  29.             b[i] = edge(i, a[i]);  
  30.             sum[i] = sum[i-1] + a[i];  
  31.             p[i] = i;  
  32.             res[i] = node(i, i, a[i]);  
  33.             if(ans < a[i] * a[i]) {  
  34.                 ans = a[i] * a[i];  
  35.                 l = r = i;  
  36.             }  
  37.         }  
  38.         sort(b+1, b+n+1);  
  39.         for(int i = 1; i <= n; i++) {  
  40.             int pp = b[i].pos;  
  41.             if(pp > 1 && a[pp-1] >= a[pp]) {  
  42.                 int x = _find(pp);  
  43.                 int y = _find(pp-1);  
  44.                 if(x != y) {  
  45.                     p[x] = y;  
  46.                     res[y] = node(min(res[y].l,res[x].l), max(res[y].r,res[x].r), a[pp]);  
  47.                     if(ans < a[pp]*(sum[res[y].r] - sum[res[y].l-1])) {  
  48.                         ans = a[pp]*(sum[res[y].r] - sum[res[y].l-1]);  
  49.                         l = res[y].l;  
  50.                         r = res[y].r;  
  51.                     }  
  52.                 }  
  53.             }  
  54.             if(pp < n && a[pp+1] >= a[pp]) {  
  55.                 int x = _find(pp);  
  56.                 int y = _find(pp+1);  
  57.                 if(x != y) {  
  58.                     p[x] = y;  
  59.                     res[y] = node(min(res[y].l,res[x].l), max(res[y].r,res[x].r), a[pp]);  
  60.                     if(ans < a[pp]*(sum[res[y].r] - sum[res[y].l-1])) {  
  61.                         ans = a[pp]*(sum[res[y].r] - sum[res[y].l-1]);  
  62.                         l = res[y].l;  
  63.                         r = res[y].r;  
  64.                     }  
  65.                 }  
  66.             }  
  67.         }  
  68.         printf("%I64d\n%I64d %I64d\n", ans, l, r);  
  69.     }  
  70.     return 0;  
  71. }  
0 0
原创粉丝点击