HDU - 5783 Divide the Sequence
2017-09-02 15:16
288 查看
保证有解的情况下,对于每一个负数,前面都存在几个正数使之前缀和大于零
从后往前找一遍,遇到负数往前加
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cctype>
#include <stack>
#include <sstream>
#include <list>
#include <map>
#include <assert.h>
#define debug() puts("************")
#define MS(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double eps = 1e-15;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
const int maxn = 1000000 + 7, maxd = 100000 + 7;
int n;
ll a[maxn];
int main() {
while(scanf("%d", &n) != EOF && n) {
for(int i = 0; i < n; ++i)
scanf("%lld", &a[i]);
int ans = 0;
for(int i = n-1; i >= 0; --i) {
if(a[i] >= 0) ans++;
else a[i-1] += a[i];
}
printf("%d\n", ans);
}
return 0;
}
从后往前找一遍,遇到负数往前加
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cctype>
#include <stack>
#include <sstream>
#include <list>
#include <map>
#include <assert.h>
#define debug() puts("************")
#define MS(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double eps = 1e-15;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
const int maxn = 1000000 + 7, maxd = 100000 + 7;
int n;
ll a[maxn];
int main() {
while(scanf("%d", &n) != EOF && n) {
for(int i = 0; i < n; ++i)
scanf("%lld", &a[i]);
int ans = 0;
for(int i = n-1; i >= 0; --i) {
if(a[i] >= 0) ans++;
else a[i-1] += a[i];
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 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 <思维水题> 多校训练5
- 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_DivideTheSequence(贪心)
- 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
- 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(贪心)