#4004: TLE......該如何讓程式變快??


s89162504 (奕(25th))

學校 : 國立新竹高級中學
編號 : 8643
來源 : [111.243.105.123]
最後登入時間 :
2022-11-19 23:56:52
d501. 第二題:數列最小值 -- 98學年度高雄市資訊學科能力競賽 | From: [61.230.186.212] | 發表日期 : 2010-07-17 18:10

#include<iostream>
#include<algorithm>

using namespace std;

main()
{
      int t,temp;
      int num[10000];
      int flag;
      
      while(cin>>t)
      {
            for(int i=0;i<t;i++)
            {
                    cin>>num[i];
            }
            for(int i=0;i<t;i++){
                    flag=0;
                    for(int j=1;j<t;j++)
                            if(num[j]<num[j-1]){
                                                swap(num[j],num[j-1]);
                                                flag++;
                                                }
                    if(flag==0)break;
            }
            
            if(t%2==1)cout<<"A="<<num[((t+1)/2)-1]<<endl;
            if(t%2==0){
                            temp=num[(t/2)-1];
                            cout<<"A="<<temp;
                            temp++;
                            while(temp<=num[(t/2)])
                            {
                                  cout<<"、"<<temp;
                                  temp++;
                            }
                            cout<<endl;
                      }
      }
      
     
      return 0;
      }
                      

該如何讓程式加速

問題是出在排序嗎??

 
#4007: Re:TLE......該如何讓程式變快??


leopan0922 (zz)

學校 : 臺北市立成功高級中學
編號 : 6612
來源 : [140.113.225.106]
最後登入時間 :
2016-08-15 15:44:07
d501. 第二題:數列最小值 -- 98學年度高雄市資訊學科能力競賽 | From: [219.70.171.51] | 發表日期 : 2010-07-19 14:40

#include
#include

using namespace std;

main()
{
      int t,temp;
      int num[10000];
      int flag;
      
      while(cin>>t)
      {
            for(int i=0;i
            {
                    cin>>num[i];
            }
            for(int i=0;i
                    flag=0;
                    for(int j=1;j
                            if(num[j]
                                                swap(num[j],num[j-1]);
                                                flag++;
                                                }
                    if(flag==0)break;
            }
            
            if(t%2==1)cout<<"A="<<
            if(t%2==0){
                            temp=num[(t/2)-1];
                            cout<<"A="<
                            temp++;
                            while(temp<=num[(t/2)])
                            {
                                  cout<<"、"<
                                  temp++;
                            }
                            cout<
                      }
      }
      
     
      return 0;
      }
                      

該如何讓程式加速

問題是出在排序嗎??


我幫你測試過換了一種排序就過了

如果加上優化輸入可以更快

 
ZeroJudge Forum