标签:
题目链接:
Time Limit: 5000/2500 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
题意:
问把这个序列分成任何前缀和都非负的子序列最多能分成多少个;
思路:
贪心,求出前缀和,从后往前对一个和找到它前边最近的小于等于它的分段就好;
AC代码:
/************************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ ┏━┛ ┆
┆ ┃ ┃ ┆
┆ ┃ ┗━━━┓ ┆
┆ ┃ AC代马 ┣┓┆
┆ ┃ ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + ‘0‘);
putchar(‘\n‘);
}
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+10;
const int maxn=2e3+14;
const double eps=1e-8;
int a[N];
LL sum[N];
inline void solve()
{
}
int main()
{
int n;
while(cin>>n)
{
For(i,1,n)read(a[i]),sum[i]=sum[i-1]+a[i];
int ans=0,cur=n,i=n-1;
LL cursum=sum[cur];
while(cur)
{
if(sum[i]<=cursum)
{
ans++;
cur=i;
cursum=sum[cur];
}
i--;
}
printf("%d\n",ans);
}
return 0;
}
hdu-5783 Divide the Sequence(贪心)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5732118.html