[贪心] Jzoj P4249 游戏
题解
- 贪心做就好了,设mx[i]为从i到n最大值为多少
- 那么O(n)跑,如果当前mx[i]==a[i],就直接记录答案,指针指向当前位置就好了
- 通俗易懂
代码
1 #include <cstdio> 2 #include <iostream> 3 #define N 100010 4 using namespace std; 5 int n,ans,l,a[N],mx[N]; 6 int main() 7 { 8 freopen("game.in","r",stdin),freopen("game.out","w",stdout),scanf("%d",&n); 9 for (int i=1;i<=n;i++) scanf("%d",&a[i]),mx[n]=a[n]; 10 for (int i=n-1;i;i--) mx[i]=max(mx[i+1],a[i]); 11 for (int i=1;i<=n;i++) if (mx[i]==a[i]) ans+=a[i]*(i-l),l=i; 12 printf("%d",ans); 13 }