甲級-1007 Maximum Subsequence Sum (25 分)

題目:

Given a sequence of K integers { N1,N2,...,NK }. A continuous subsequence is defined to be { Ni,Ni+1,...,Nj } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

解題思路:

思路參考了先前刷過的另外一道題各薇,動態(tài)規(guī)劃——買賣股票的最佳時(shí)機(jī)
這里將輸入序列的{ N1,N2,...,NK }轉(zhuǎn)化為了序列A{ 0,N1,N1+N2,...,N1+N2+...+NK }的形式,只需算出最大的A[m]-A[j] (0≤j<m≤K)即可眼坏,此時(shí)對于原序列 [j,m)即為最大子序列所在的區(qū)間茴恰。
注意點(diǎn)——需要考慮輸入序列全為負(fù)數(shù)或全為0的特殊情況荠诬。

代碼:

編譯器:C++(g++)

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

pair<int,int> findSubseq(const vector<int> &srce)
{
    //數(shù)組全部為0的特殊情況
    for(int i=0,zero=-1;i<srce.size();++i)
    {
        if(0==srce[i]&&-1==zero)
        {
            zero=i;
        }
        if(srce[i]>0)
        {
            break;
        }
        if(i==srce.size()-1)
        {
            return make_pair(zero,zero+1);
        }
    }
    //轉(zhuǎn)換為tmp{0, srce[0], srce[0]+srce[1],..., srce[0]+...+srce[n-1]}的形式
    //若tmp[m]-tmp[j]為最大值澡为,則索引[j,m)為最大子序列
    vector<int> tmp(1,0);
    tmp.push_back(srce[0]);
    for(int i=1,n=srce[0];i<srce.size();++i)
    {
        n+=srce[i];
        tmp.push_back(n);
    }
    int minIndex=0,maxIndex=0,value=0,tmpMin=0,tmpMax=0;
    for(int i=1;i!=tmp.size();++i)
    {
        if(tmp[i]>tmp[tmpMax])
        {
            tmpMax=i;
        }
        if(tmp[i]<tmp[tmpMin]||i==tmp.size()-1)
        {
            if(tmp[tmpMax]-tmp[tmpMin]>value)
            {
                maxIndex=tmpMax;
                minIndex=tmpMin;
                value=tmp[tmpMax]-tmp[tmpMin];
            }
            tmpMax=i;
            tmpMin=i;
        }
    }
    return make_pair(minIndex,maxIndex);
}
int main()
{
    int k;
    cin>>k;
    vector<int> ivec;
    for(int i=0;i!=k;++i)
    {
        int t;
        cin>>t;
        ivec.push_back(t);
    }
    //考慮全為負(fù)值的特殊情況
    for(int i=0;i!=k;++i)
    {
        if(ivec[i]>=0)
        {
            break;
        }
        if(i==k-1)
        {
            cout<<"0 "<<ivec[0]<<" "<<ivec[i]<<endl;
            return 0;
        }
    }
    pair<int,int> ret=findSubseq(ivec);
    int sum=0;
    for(int i=ret.first;i!=ret.second;++i)
    {
        sum+=ivec[i];
    }
    cout<<sum<<" "<<ivec[ret.first]<<" "<<ivec[ret.second-1]<<endl;
    return 0;   
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末升筏,一起剝皮案震驚了整個(gè)濱河市外构,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌嗜闻,老刑警劉巖蜕依,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異琉雳,居然都是意外死亡样眠,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進(jìn)店門翠肘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來檐束,“玉大人,你說我怎么就攤上這事束倍”簧ィ” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵绪妹,是天一觀的道長甥桂。 經(jīng)常有香客問我,道長邮旷,這世上最難降的妖魔是什么黄选? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮婶肩,結(jié)果婚禮上办陷,老公的妹妹穿的比我還像新娘。我一直安慰自己律歼,他們只是感情好民镜,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著险毁,像睡著了一般制圈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上畔况,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天鲸鹦,我揣著相機(jī)與錄音,去河邊找鬼问窃。 笑死亥鬓,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的域庇。 我是一名探鬼主播嵌戈,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼覆积,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了熟呛?” 一聲冷哼從身側(cè)響起宽档,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎庵朝,沒想到半個(gè)月后吗冤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡九府,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年椎瘟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片侄旬。...
    茶點(diǎn)故事閱讀 40,438評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡肺蔚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出儡羔,到底是詐尸還是另有隱情宣羊,我是刑警寧澤,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布汰蜘,位于F島的核電站仇冯,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏族操。R本人自食惡果不足惜苛坚,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望坪创。 院中可真熱鬧炕婶,春花似錦姐赡、人聲如沸莱预。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽依沮。三九已至,卻和暖如春枪狂,著一層夾襖步出監(jiān)牢的瞬間危喉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工州疾, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留辜限,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓严蓖,卻偏偏與公主長得像薄嫡,于是被迫代替她去往敵國和親氧急。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 題目 Maximum Subsequence Sum (25)Given a sequence of K inte...
    某翁閱讀 218評論 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,345評論 0 10
  • 想好怎么過這一生了嗎? 這個(gè)問題真的好難哑蔫,何為人生钉寝?就是我在這浩瀚宇宙的一角慢慢走出的每一步,快不了闸迷,也慢不了嵌纲。就...
    醉夏之初閱讀 150評論 0 1
  • 你到底是誰 我們擁有過的生命不足以書寫青春,但我們此時(shí)的生命都是書下的字字青春腥沽。 所愛隔山海疹瘦,山海不可平。 海有舟...
    fxxku閱讀 202評論 0 0
  • 剛查了下日歷巡球,今天是2月7號星期四言沐,農(nóng)歷大年初三,在這個(gè)全國人民都不事生產(chǎn)的喜慶節(jié)日里酣栈,最近不事學(xué)習(xí)的我险胰,就來...
    Sarah_Tang閱讀 407評論 2 3