NC25084. [USACO 2006 Nov S]Bad Hair Day
描述
输入描述
Line 1: The number of cows, N.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
输出描述
Line 1: A single integer that is the sum of c1 through cN.
示例1
输入:
6 10 3 7 4 12 2
输出:
5
C(clang 3.9) 解法, 执行用时: 22ms, 内存消耗: 1612K, 提交时间: 2019-07-07 16:23:15
#include<stdio.h> int cow[100000],top[100000]; int main(){ int i,j,k,n; long long s=0; scanf("%d",&n); k=0; for(i=1;i<=n;i++){ scanf("%d",&cow[i]); while(cow[i]>=top[k] && k>0)k--; s+=k; k++; top[k]=cow[i]; } printf("%lld\n",s); }
C++11(clang++ 3.9) 解法, 执行用时: 23ms, 内存消耗: 1740K, 提交时间: 2019-07-06 11:01:15
#include<cstdio> long long n,x,s[80005],top,ans; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) {scanf("%d",&x);while(top && s[top]<=x) top--;ans+=top;s[++top]=x;}printf("%lld",ans);return 0;}