NOIP 2013 积木大赛
2017-10-18 13:57
197 查看
评测传送
其实这道题比起其他年份的T1还是算难的。
花费时间:40min+
我的做法是:
用一个栈,来存某一段。
看图理解
我们先将第一段橙色的入栈,因为这是第一段,所以次数是st[top];
然后清空栈,将绿色的入栈,需要的次数是st[top]-st[1]=2;(因为是递增的,所以已经清空的上一段最后一个一定比当前这一段的第一个高,所以在之前就可以将这一段的这个高度给覆盖掉,所以需要的次数是最大值减最小值)
以此类推。
时间复杂度O(n)
检查时发现,宽度为1的时候要单独处理
其实这道题比起其他年份的T1还是算难的。
花费时间:40min+
我的做法是:
用一个栈,来存某一段。
看图理解
我们先将第一段橙色的入栈,因为这是第一段,所以次数是st[top];
然后清空栈,将绿色的入栈,需要的次数是st[top]-st[1]=2;(因为是递增的,所以已经清空的上一段最后一个一定比当前这一段的第一个高,所以在之前就可以将这一段的这个高度给覆盖掉,所以需要的次数是最大值减最小值)
以此类推。
时间复杂度O(n)
检查时发现,宽度为1的时候要单独处理
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int M=100009; int n,a[M],maxn,ans; int st[M],top; bool first; int main() { freopen("block.in","r",stdin); freopen("block.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),maxn=max(maxn,a[i]); if(n==1) {printf("%d",a[1]);return 0;} int top=0; a[0]=-1; for(int i=1;i<=n;i++) { if(a[i]>=a[i-1]) st[++top]=a[i]; else { if(!first) { ans+=st[top]; first=1; } else ans+=st[top]-st[1]; top=0; st[++top]=a[i]; } } ans+=st[top]-st[1]; printf("%d",ans); return 0; } /* 8 2 3 4 1 3 3 0 4 10 */
相关文章推荐
- [noip2013]积木大赛
- luogu P1969积木大赛(NOIP 2013 TG)
- [noip2013tg] 积木大赛
- [NOIP 2013]积木大赛 贪心
- |洛谷|NOIP2013|贪心|P1969 积木大赛
- 【NOIP 2013 Day2 T1】积木大赛(贪心)
- 洛谷 P1969 [NOIP2013 D2T1] 积木大赛
- 【NOIP2013】 积木大赛 (贪心)
- noip2013提高组day201积木大赛
- noip2013 积木大赛 (贪心)
- luogu1969 积木大赛(NOIP2013提高组第4题)
- NOIP 2013 提高组 复赛 day2 block 积木大赛
- [P1969][NOIP2013]积木大赛
- Noip2013 提高组 Day2 T1 积木大赛
- noip2013 积木大赛
- 【NOIP 2013】积木大赛
- [NOIP2013]积木大赛
- 洛谷 P1969 [NOIP2013]积木大赛
- NOIP2013复赛提高组day2(A:积木大赛 B:花匠 C:华容道)
- 贪心 NOIP2013 积木大赛