zoukankan      html  css  js  c++  java
  • Tallest cow(差分数组)

    首先翻译一下题面,其实就是给定两个位置a和b,中间的数都要比a和b小,那么我们可以想到用一个差分数组,a+1到b-1都-1,就可以更新身高,最后再一起输出

    #include<bits/stdc++.h>
    using namespace std;
    int n,k,h,r,a,b;
    int d[10005],c[10005];
    map<pair<int,int>,bool> mp;//用pair可能会快
    int main(){
        scanf("%d %d %d %d",&n,&k,&h,&r);
        while(r--){
            scanf("%d %d",&a,&b);
            if(a>b)swap(a,b);//注意a可能会>b
            pair<int,int> tp(a,b);
            if(mp[tp])continue;
            mp[tp]=1;//把a之后的到b之前的都-1
            d[a+1]--;d[b]++;//记录差分数组
        }
        for(int i=1;i<=n;i++){
            c[i]=c[i-1]+d[i];//变为前缀和
            printf("%d
    ",h+c[i]);
        }
    }
  • 相关阅读:
    【leetcode】修剪二叉搜索树
    053-621
    053-620
    053-619
    053-618
    053-617
    053-616
    053-615
    053-614
    053-613
  • 原文地址:https://www.cnblogs.com/coder-cjh/p/11568889.html
Copyright © 2011-2022 走看看