zoukankan      html  css  js  c++  java
  • P1969 积木大赛

    P1969 积木大赛

    题解

    solution 1

    我们举个例子

    然后我们画个折线图:

     然后我们大胆猜想,把上边的凸点加起来,减去凹点之和,得到结果

    代码

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 #include<string>
     6 #include<cstring>
     7 #include<queue>
     8 #include<cmath>
     9 using namespace std;
    10 typedef long long ll;
    11 
    12 inline int read()
    13 {
    14     int ans=0;
    15     char last=' ',ch=getchar();
    16     while(ch<'0'||ch>'9') last=ch,ch=getchar();
    17     while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    18     if(last=='-') ans=-ans;
    19     return ans;
    20 }
    21 
    22 const int maxn=1e5+10;
    23 int n,minh=1e5,ans=0;
    24 int h[maxn],tot=0;
    25 queue<int>q;
    26 
    27 int main()
    28 {
    29     n=read();
    30     h[0]=h[n-1]=0;
    31     for(int i=1;i<=n;i++){
    32         h[++tot]=read();
    33         if(h[tot]==h[tot-1]) tot--;
    34     } 
    35     for(int i=1;i<=n;i++){
    36         if(h[i-1]<h[i]&&h[i]>h[i+1]) ans+=h[i];
    37         if(h[i-1]>h[i]&&h[i]<h[i+1]) ans-=h[i];
    38     }     
    39     printf("%d
    ",ans);
    40     return 0;
    41 }

    Solution 2

    我们一个个输入,如果发现前一个数字比后一个小,那么就说明要再多来一步操作,所以加上它与前一个数的差

    代码

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 #include<string>
     6 #include<cstring>
     7 #include<queue>
     8 #include<cmath>
     9 using namespace std;
    10 typedef long long ll;
    11 
    12 inline int read()
    13 {
    14     int ans=0;
    15     char last=' ',ch=getchar();
    16     while(ch<'0'||ch>'9') last=ch,ch=getchar();
    17     while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    18     if(last=='-') ans=-ans;
    19     return ans;
    20 }
    21 
    22 const int maxn=1e5+10;
    23 int n,ans=0;
    24 int h[maxn];
    25 
    26 int main()
    27 {
    28     n=read();
    29     for(int i=1;i<=n;i++){
    30         h[i]=read();
    31         if(h[i-1]<h[i]) ans+=(h[i]-h[i-1]);
    32     }     
    33     printf("%d
    ",ans);
    34     return 0;
    35 }

    双倍经验

    P5019 铺设道路

  • 相关阅读:
    Linux Shell脚本编程实用技巧
    Transmission在Pandorabox(Openwrt)下的交叉编译
    Pandorabox下关于vsftpd匿名访问的设置
    PandoraBox下部署阿里云(aliyun)DDNS动态域名更新(shell脚本)
    win7系统封装小记
    CF R#295 (DIV.2) E. Pluses everywhere
    CF R#295 (DIV.2) D. Cubes
    CF R#295 (DIV.2) C. DNA Alignment
    winform在线操作office--dsoframerocx第三方控件
    Dapper操作
  • 原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/11785286.html
Copyright © 2011-2022 走看看