#45379: 測試AC但送出答案會TLE


shinbeforedawn@gmail.com (黃冠融)

學校 : 不指定學校
編號 : 179473
來源 : [118.170.155.184]
最後登入時間 :
2025-02-22 18:34:09
a291. nAnB problem | From: [118.170.155.184] | 發表日期 : 2025-02-22 13:24

#include <iostream> 
#include <string>
#include <algorithm>
using namespace std;

int main(){
    
   int a,b,c,d;
   while(cin >> a >> b >> c >> d){
       int n;
       int arr[] = {a,b,c,d};
       cin >> n;
       for (int i = 0; i < n; i++) {
           int a1, b1, c1, d1;
           cin >> a1 >> b1 >> c1 >> d1;
           int arr1[] = {a1,b1,c1,d1};
            int A = 0;
           for(int k = 0; k < 4 ; k ++){
               if(arr[k] == arr1[k])
                   A++;
           }
       int freq[10] = {0};
       for(int num:arr)
           freq[num]++;
       int freq1[10] = {0};
       for(int num:arr1)
           freq1[num]++;
           int count = 0;
        for(int j = 0; j < 10;j++){
                 count += min(freq[j], freq1[j]);
        }
        int B = count - A;
        cout << A << "A" << B << "B";
        cout << "\n";
         

        }
   }
   return 0;
}

 

請問是哪邊有問題?

 
#45380: Re: 測試AC但送出答案會TLE


leeguanhan0909@gmail.com (李冠翰)

學校 : 高雄市苓雅區復華高級中學國中部
編號 : 276558
來源 : [218.166.10.225]
最後登入時間 :
2025-02-10 00:28:08
a291. nAnB problem | From: [36.238.140.196] | 發表日期 : 2025-02-22 15:40

#include  
#include
#include
using namespace std;

int main(){
    
   int a,b,c,d;
   while(cin >> a >> b >> c >> d){
       int n;
       int arr[] = {a,b,c,d};
       cin >> n;
       for (int i = 0; i < n; i++) {
           int a1, b1, c1, d1;
           cin >> a1 >> b1 >> c1 >> d1;
           int arr1[] = {a1,b1,c1,d1};
            int A = 0;
           for(int k = 0; k < 4 ; k ++){
               if(arr[k] == arr1[k])
                   A++;
           }
       int freq[10] = {0};
       for(int num:arr)
           freq[num]++;
       int freq1[10] = {0};
       for(int num:arr1)
           freq1[num]++;
           int count = 0;
        for(int j = 0; j < 10;j++){
                 count += min(freq[j], freq1[j]);
        }
        int B = count - A;
        cout << A << "A" << B << "B";
        cout << "\n";
         

        }
   }
   return 0;
}

 

請問是哪邊有問題?

加上IO優化
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

 
ZeroJudge Forum