hdu 5783 Divide the Sequence
2016-08-04 16:12
387 查看
题目分析
一道水题,本来我以为n*logn的方法会爆炸,但是一看时间给了2500MS,于是果断搞起了,首先我们在边求前缀和的同时进行操作,然后直接将每一个的结果通过二分查找找到给定元素在数列中的位置,然后替换该位置的元素,同时将长度重新设置为此时查找到的长度,因为如果有负数会使前面若干个必须与该负数组合在一起。这样一来求得的长度就是应该求得到最多分成几个子序列的个数。同事需要注意这道题会爆INT,WA了一发,不过很快发现了。#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 1e6+5; LL a[maxn],sum[maxn],dp[maxn]; int BS(LL *a,int len,LL k){ int left = 1, right = len; while(left <= right){ int mid = (left+right)/2; if(a[mid] <= k) left = mid+1; else right = mid-1; } return left; } int main(){ int N; while(scanf("%d", &N) != EOF){ memset(a, 0, sizeof(a)); memset(sum, 0, sizeof(sum)); memset(dp, 0, sizeof(dp)); for(int i = 1; i <= N; i++) scanf("%I64d", &a[i]); sum[1] = a[1]; dp[1] = a[1]; int len = 1; for(int i = 2; i <= N; i++){ sum[i] = sum[i-1] + a[i]; len = BS(dp, len, sum[i]); dp[len] = sum[i]; } printf("%d\n", len); } return 0; }
相关文章推荐
- hdu_5783_Divide the Sequence(贪心)
- hdu-5783 Divide the Sequence(贪心)
- hdu 5783——Divide the Sequence
- 2016多校5 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-5783 Divide the Sequence(贪心)
- (HDU 5783)Divide the Sequence <思维水题> 多校训练5
- HDU - 5783 Divide the Sequence
- 【HDU】5783 Divide the Sequence
- HDU_5783_DivideTheSequence(贪心)
- 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(贪心水题)