竹園論壇

標題: a313 - B 街道國 [打印本頁]

作者: Sylveon    時間: 2014-4-27 11:31
標題: a313 - B 街道國
原文:http://zerojudge.tw/ShowProblem?problemid=a313
AC   :http://zerojudge.tw/Submissions? ... mp;account=lfs92002
ACCODE:http://ideone.com/DOMrSQ
懷舊系列,國中時寫的code。Greedy 分割就好

/**********************************************************************************/
/*  Problem: a313 "B 街道國" from 2011 NPSC 國中組初賽                    */
/*  Language: CPP (512 Bytes)                                                     */
/*  Result: AC(0.5s, 400KB) judge by this@ZeroJudge                               */
/*  Author: lfs92002 at 2011-12-06 12:34:55                                       */
/**********************************************************************************/


#include<iostream>
using namespace std;
int main()
{
        int T,n,x;
        short road[10001];
        int total,sum;
        cin>>T;
        while (T--)
        {
                cin>>n;
                for(x=0;x<n;x++)
                        cin>>road[x];
                total=sum=0;
                for(x=0;x<n;x++)
                {
                        if(road[x]>0)
                        {
                                sum+=road[x];
                                if(sum==0)
                                        total++;

                        }
                        else
                        {
                                if(sum==-road[x])
                                {
                                        total++;
                                        sum=0;
                                }
                                else
                                        sum+=road[x];
                        }
                }
                if(total!=0)
                cout<<total<<endl;
                else
                        cout<<"1\n";

        }
        return 0;
}





歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2