hdu 5783——Divide the Sequence
来源:互联网 发布:珍珠控制台模拟软件 编辑:程序博客网 时间:2024/06/06 06:50
题意及思路:
求一个序列的分段个数,使得每一段的前缀和为0,如果正向思维,那么解法是从前往后遍历,每遇到一个负数就向前遍历直到>=0(这样贪心保证了序列尽可能多),但是这样最坏的情况是n^2的,所以要逆过来来考虑,每遇到负数就向前加到>=0即可,然后边统计答案,在n的算法里计算出。(注意:前缀和可能超int,用long long 保存)
code:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=1e6+6;ll v[N];int main(){ int n; while (~scanf("%d",&n)){ for (int i=0;i<n;i++) scanf("%lld",v+i); ll s=0,ans=0; for (int i=n-1;i>=0;i--){ s+=v[i]; if (s>=0) {ans++;s=0;} } cout<<ans<<endl; }}
0 0
- hdu 5783——Divide the Sequence
- HDU 5783 Divide the Sequence——贪心
- hdu 5783 Divide the Sequence
- Divide the Sequence hdu 5783
- hdu 5783 Divide the Sequence
- HDU 5783 Divide the Sequence
- 【HDU】5783 Divide the Sequence
- hdu 5783 Divide the Sequence
- HDU 5783 Divide the Sequence
- HDU 5783 Divide the Sequence
- hdu 5783 Divide the Sequence【水题】
- HDU 5783 Divide the Sequence(水题)
- HDU 5783 Divide the Sequence 【贪心】
- HDU 5783 Divide the Sequence 思维题
- HDU 5783 Divide the Sequence(贪心)
- 【贪心】HDU 5783 Divide the Sequence
- hdu 5783 Divide the Sequence(2016 Multi-University Training Contest 5——水题)
- HDU-5783 Divide the Sequence(贪心水题)
- HDU 1010 Tempter of the Bone (DFS+奇偶剪枝)
- 虚拟机桥接、NAT、host-only的区别
- Fresco中的ImagePipeline
- 1032. Sharing (25)-PAT甲级真题
- Android中自定义组合控件
- hdu 5783——Divide the Sequence
- git管理项目 一些使用语句
- 简单hash[或者是哈希思想]
- Maven创建An internal error occurred during: "Retrieving archetypes:". Java heap space
- 剑指Offer: 扑克牌顺子
- 数据结构实验之求二叉树后序遍历和层次遍历 (sdut oj2137)
- HDU 5806 NanoApe Loves Sequence Ⅱ
- 1052. Linked List Sorting (25)-PAT甲级真题
- iOS-沙盒目录结构分析