USACO Section 1.4: Arithmetic Progressions

这题一直在TLE,直到我放弃用set,set这玩意最好还是用int会比较好,太耗时了,下次试试unordered_set。

 1 /*

 2 ID: leetcod3

 3 PROG: ariprog

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <queue>

14 #include <cmath>

15 #include <list>

16 #include <cstring>

17 #include <cstdlib>

18 #include <limits>

19 #include <stack>

20 

21 using namespace std;

22 

23 ofstream fout ("ariprog.out");

24 ifstream fin ("ariprog.in");

25 

26 bool tmp(const pair<int, int> &a, const pair<int, int> &b) {

27     if (a.second == b.second) return a.first <= b.first;

28     else return a.second < b.second;

29 }

30 

31 int hashs[999999];

32 

33 int main()

34 {

35     int N, M;

36     fin >> N >> M;

37     vector<pair<int, int> > ans;

38     memset(hashs, 0, sizeof(hashs));

39     vector<int> T;

40     for (int i = 0; i <= M; i++) {

41         for (int j = i; j <= M; j++) {

42             if (hashs[i*i + j*j]) continue;

43             hashs[i*i + j*j] = 1;

44             T.push_back(i*i + j*j);

45         }

46     }

47     int maxnum = M * M + M * M;

48     sort(T.begin(), T.end());

49     for (int i = 0; i < (int)T.size() - N + 1; i++) {

50         for (int j = i+1; j < T.size(); j++) {

51             if (T[i] + (N-1)*(T[j]-T[i]) > maxnum) break;

52             int k = 1;

53             while (hashs[T[i]+k*(T[j]-T[i])] && k < N) k++;

54             if (k == N) ans.push_back(make_pair(T[i], T[j] - T[i]));

55         }

56     }

57     sort(ans.begin(), ans.end(), tmp);

58     if (ans.size() == 0) fout << "NONE" << endl;

59     else for (int i = 0; i < ans.size(); i++) fout << ans[i].first << " " << ans[i].second << endl;

60     return 0;

61 }

 

你可能感兴趣的:(progress)