hdu1003

//c++ //https://github.com/zzdxfei/hduacm/blob/master/1003.cpp #include  #include

using namespace std;

void ComputeMaxSubstr(const vector& sets, int& max_sum, int& max_left, int& max_right) { max_sum = sets[0]; max_left = 0; max_right = 0; int current_left = 0; int current_sum = sets[0]; for(int i = 1; i < sets.size(); ++i) { if(current_sum >= 0) { current_sum += sets[i]; } else { current_left = i; current_sum = sets[i]; } if(current_sum > max_sum) { max_sum = current_sum; max_left = current_left; max_right = i; } } }

int main(void) { int max_sum, left, right; int num_tests; int num_nums; scanf("%d", &num_tests); for(int i = 0; i != num_tests; ++i) { scanf("%d", &num_nums); vector nums(num_nums); for(int j = 0; j != num_nums; ++j) { scanf("%d", &(nums[j])); } ComputeMaxSubstr(nums, max_sum, left, right); printf("Case %d:\n%d %d %d\n", i + 1, max_sum, left + 1, right + 1); if(i != num_tests - 1) { printf("\n"); } } return 0; }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券