• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
镂空的行尸
   首页       联系   管理    订阅  订阅

Divide the Sequence (贪心)

  题意:求将一串数据尽可能多分成所有前缀和大于0的连续子串。

  思路:由于是要求所有前缀和大于0,那么只要从后往前推就好了。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 7;
long long arr[maxn], n;

int main(){
    while(~scanf("%d", &n)){
        for(int i = 0; i < n; i ++) scanf("%lld", &arr[i]);
        int ans = 0;
        long long sum = 0;
        for(int i = n - 1; i >= 0; i --) {
            sum += arr[i];
            if(sum >= 0){
                ans ++;
                sum = 0;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

more crazy more get!
posted @ 2018-10-31 10:47  镂空的行尸  阅读(169)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3