zoukankan      html  css  js  c++  java
  • [DP地狱训练]Pascal山脉

    OJ题号:ZHOJ1055

    思路:树状数组。

    首先将数据离散化,然后用线段树维护小于当前高度的山峰已经出现过的数量。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 const int N=30000,root=1;
     5 int n;
     6 class FenwickTree {
     7     private:
     8         int val[N<<2];
     9     public:
    10         FenwickTree() {
    11             memset(val,0,sizeof val);
    12         }
    13         int lowbit(const int x) {
    14             return x&-x;
    15         }
    16         int query(int p) {
    17             int ans=0;
    18             while(p) {
    19                 ans+=val[p];
    20                 p-=lowbit(p);
    21             }
    22             return ans;
    23         }
    24         void modify(int p) {
    25             while(p<=n) {
    26                 val[p]++;
    27                 p+=lowbit(p);
    28             }
    29         }
    30 };
    31 FenwickTree tree;
    32 int main() {
    33     scanf("%d",&n);
    34     int a[n+1],b[n+1];
    35     for(int i=1;i<=n;i++) {
    36         scanf("%d",&a[i]);
    37         b[i]=a[i];
    38     }
    39     std::sort(&b[1],&b[n+1]);
    40     int ans[n+1];
    41     for(int i=1;i<=n;i++) {
    42         int t=std::lower_bound(&b[1],&b[n+1],a[i])-&b[0];
    43         ans[i]=tree.query(t-1);
    44         tree.modify(t);
    45     }
    46     for(int i=1;i<=n;i++) printf("%d ",ans[i]);
    47     printf("
    ");
    48     return 0;
    49 }
  • 相关阅读:
    BZOJ 2957: 楼房重建
    模积和(bzoj 2956)
    Four-tuples(2018山东省赛 F)
    Best Rational Approximation( 法里数列)
    K
    Now Loading!!!(ZOJ Problem Set
    Treasure Map(Southeast USA ICPC 2017)
    三角形的内点
    小b和灯泡
    不降的数字
  • 原文地址:https://www.cnblogs.com/skylee03/p/6941808.html
Copyright © 2011-2022 走看看