반응형

문제 출처 :


https://www.acmicpc.net/problem/11332



알고리즘 분석 :


문제 해결에 필요한 사항

1. 구현


실제로 시간 복잡도를 계산해보는 문제이다.


테스트케이스와 n이 어떻게 동작하는지 생각해보면 문제를 해결할 수 있다.


이때 모든 수를 처리한 후 TLE인지 파악하면 long long 범위마저 벗어나므로 계산하며 TLE인지 파악해보자.






소스 코드 : 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <cstdio>
 
typedef long long ll;
 
using namespace std;
 
int main()
{
    long long n;
    cin >> n;
    
    while (n--)
    {
        string str;
        cin >> str;
 
        long long n, tc, l;
        cin >> n >> tc >> l;
 
        long long ans = 0;
        if (str == "O(N)")
        {
            ans = tc * n;
            if (ans > l *(ll)100000000)
            {
                cout << "TLE!" << endl;
                continue;
            }
        }
        else if (str == "O(2^N)")
        {
            bool chk = true;
            ans = tc;
            for (int i = 1; i <= n; i++)
            {
                ans *= 2;
                if (ans > l * (ll)100000000)
                {
                    cout << "TLE!" << endl;
                    chk = false;
                    break;
                }
            }
            if (!chk)
                continue;
        }
        else if (str == "O(N!)")
        {
            bool chk = true;
            ans = tc;
            for (int i = 1; i <= n; i++)
            {
                ans *= i;
 
                if (ans > l *(ll)100000000)
                {
                    cout << "TLE!" << endl;
                    chk = false;
                    break;
                }
            }
            if (!chk)
                continue;
        }
 
        else if (str == "O(N^2)")
        {
            ans = tc*n*n;
            if (ans > l *(ll)100000000)
            {
                cout << "TLE!" << endl;
                continue;
            }
        }
        else if (str == "O(N^3)")
        {
            ans = tc*n;
            if (ans > l * (ll)100000000)
            {
                cout << "TLE!" << endl;
                continue;
            }
            ans *= n;
            if (ans > l * (ll)100000000)
            {
                cout << "TLE!" << endl;
                continue;
            }
            ans *= n;
            if (ans > l * (ll)100000000)
            {
                cout << "TLE!" << endl;
                continue;
            }
        }
 
        cout << "May Pass." << endl;
    }
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[2257번] 화학식량  (2) 2018.02.06
[9536번] 여우는 어떻게 울지?  (0) 2018.02.05
[5670번] 휴대폰 자판  (0) 2018.01.26
[13458번] 시험 감독  (2) 2018.01.26
[11559번] Puyo Puyo  (0) 2018.01.26