#29876: 測資二TLE,如果可以請幫個忙,謝謝!想試試看不用set寫。


new.k.potato.king@gmail.com (alvis lol)

學校 : 不指定學校
編號 : 157926
來源 : [223.137.142.26]
最後登入時間 :
2022-04-08 06:13:21
d478. 共同的數 - 簡易版 | From: [114.43.193.181] | 發表日期 : 2022-04-08 07:46

#include<bits/stdc++.h>

#define endl '\n'

using namespace std;

int first[10000];

int second[10000];

int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  cout.tie(0);

  int n, m, ans, temp = 0;

  cin >> n >> m;

  while(n--){

    for(int i = 0; i < m; i++){

      cin >> first[i];

    }

    for(int i = 0; i < m; i++){

      cin >> second[i];

    }

    ans = 0;

    for(int i = 0; i < m; i++){

      for(int j = temp; j < m; j++){

        if(first[i] == second[j]){

          ans++;

          int temp = j;//不用從0開始跑

          break;

        }

      }

    }

    cout << ans << endl;

  }

    return 0;

}

 
#29879: Re:測資二TLE,如果可以請幫個忙,謝謝!想試試看不用set寫。


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
d478. 共同的數 - 簡易版 | From: [101.137.16.148] | 發表日期 : 2022-04-08 08:40


    for(int i = 0; i < m; i++){

      for(int j = temp; j < m; j++){

        if(first[i] == second[j]){

          ans++;

          int temp = j;//不用從0開始跑

          break;

        }

      }

    }


1. 你temp宣告了兩次,導致j還是從0開始跑

2. 當first[i]<second[j]時,就可以break了,之後second一定越來越大,不可能重複

 

 
ZeroJudge Forum