码迷,mamicode.com
首页 > 其他好文 > 详细

51 Nod 1065 最小正子段和(前缀和)

时间:2017-10-07 22:47:58      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:子序列   clu   并且   names   题意   sort   ble   nbsp   online   

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#problemId=1065&noticeId=348062

题意:N个整数组成的序列a[1],a[2],a[3],…,a[n],从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的。

例如:4,-1,5,-2,-1,2,6,-2序列和为1,是最小的。

题解:水题...(吖)
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 typedef long long LL;
 6 const LL INF=1e18;
 7 const int N=50000+10;
 8 struct TnT{
 9     LL num;
10     LL idx;    
11 }T[N]; 
12 
13 bool cmp(TnT a,TnT b){
14     if(a.num==b.num) return a.idx<b.idx;
15     return a.num<b.num;    
16 }
17 
18 int main(){
19     LL n,tmp;
20     LL ans=INF;
21     cin>>n;
22     T[0].num=0;
23     for(int i=1;i<=n;i++){
24         cin>>tmp;
25         if(tmp>0) ans=min(ans,tmp);
26         T[i].num=T[i-1].num+tmp;
27         if(T[i].num>0) ans=min(ans,T[i].num);
28         T[i].idx=i;
29     }
30     sort(T+1,T+1+n,cmp);
31     for(int i=2;i<=n;i++){
32         if(T[i].idx>T[i-1].idx){
33             tmp=T[i].num-T[i-1].num;
34             if(tmp>0) ans=min(ans,tmp);
35         }
36     }
37     cout<<ans<<endl;
38     return 0;
39 }

 

51 Nod 1065 最小正子段和(前缀和)

标签:子序列   clu   并且   names   题意   sort   ble   nbsp   online   

原文地址:http://www.cnblogs.com/Leonard-/p/7635962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!