1. 程式人生 > >51 Nod 1065 最小正子段和(前綴和)

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

sort lin 組成 name turn nod color main tmp

題目鏈接: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 最小正子段和(前綴和)