zoukankan      html  css  js  c++  java
  • [noip2013]积木大赛

    春春幼儿园举办了一年一度的“积木大赛”。今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1且高度不定的积木组成,第i块积木的最终高度需要是hi。
    在搭建开始之前,没有任何积木(可以看成n块高度为 0 的积木)。接下来每次操作,小朋友们可以选择一段连续区间[L, R],然后将第L块到第R块之间(含第L块和第R块)所有积木的高度分别增加1。
    小M是个聪明的小朋友,她很快想出了建造大厦的最佳策略,使得建造所需的操作次数最少。但她不是一个勤于动手的孩子,所以想请你帮忙实现这个策略,并求出最少的操作次数。

    这题我最初想的是二分;

    二分写完后看题解发现是贪心做法;

    核心代码是:

     ans=ans-min(a[i],a[i-1])+a[i]; 

    这个需要理解,自己多看看;

    完整代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<iomanip>
    #include<map>
    #include<set>
    #include<vector>
    #include<ctime>
    #include<cmath>
    #define LL long long 
    using namespace std;
    #define LL long long 
    #define up(i,j,n) for(int i=(j);(i)<=(n);(i)++)
    #define max(x,y) ((x)<(y)?(y):(x))
    #define min(x,y) ((x)<(y)?(x):(y))
    #define FILE "1"
    const int maxn=101000,mod=99999997;
    int read(){
        int x=0;bool flag=0;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')flag=1;ch=getchar();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
        return flag?-x:x;
    }
    int n,ans=0;
    int a[maxn];
    int main(){
        n=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            ans=ans-min(a[i],a[i-1])+a[i];
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    Study Plan The Twelfth Day
    Study Plan The Fifteenth Day
    Study Plan The Seventeenth Day
    Study Plan The Tenth Day
    Study Plan The Eighth Day
    Study Plan The Eleventh Day
    Study Plan The Sixteenth Day
    Study Plan The Thirteenth Day
    Study Plan The Fourteenth Day
    Study Plan The Ninth Day
  • 原文地址:https://www.cnblogs.com/chadinblog/p/5945463.html
Copyright © 2011-2022 走看看