Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
860ms怎样才能优化到200多ms啊?#include <iostream> using namespace std; struct point { int left; int right; }; __int64 data[100005]; point flag[100005]; //标记每个点的左右范围 int n; int main(int argc, char* argv[]) { cin >> n; data[0] = data[n + 1] = -1; int i; __int64 sum[100005] = {0}; for(i = 1; i <= n; i++) { scanf("%I64d",data + i); sum[i] = sum[i - 1] + data[i]; } flag[1].left = 1; for(i = 1; i <= n; i++) { if(data[i] > data[i - 1]) { flag[i].left = i; } else //data[i] <= data[i - 1] { int j = i - 1; while(flag[j].left >= 1 && data[i] <= data[flag[j].left - 1]) { j = flag[j].left - 1; } flag[i].left = flag[j].left; } } flag[n].right = n; for(i = n; i >= 1; i-- ) { if(data[i] > data[i + 1]) { flag[i].right = i; } else { int j = i + 1; while(flag[j].right <= n && data[i] <= data[flag[j].right + 1]) { j = flag[j].right + 1; } flag[i].right = flag[j].right; } } int k; __int64 max = -1; for(i = 1; i <= n; i++) { __int64 temp = sum[flag[i].right] - sum[flag[i].left - 1]; if(temp * data[i] > max) { max = temp * data[i]; k = i; } } printf("%I64d\n%d %d\n", max, flag[k].left, flag[k].right); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator