0. [c++] sw 7812


https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWslJ57qFS0DFASy&categoryId=AWslJ57qFS0DFASy&categoryType=CODE


 1. 풀이





 2. 소스코드


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
#include<iostream>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
int arr[10001];
 
int makePortfolio(const int& N, const int& M) {
    int count = 0;
    pair<intint> idx;
    idx = { 0,0 };
    int sum = 0;
    for (int i = 0; i < N; i++) {
        if (sum < M) {
            sum += arr[i];
            idx.second = i;
        }
        if (sum == M) {
            count++;
            sum -= arr[idx.first];
            idx.first++;
        }
        if (sum > M)
        {
            while (1)
            {
                if (sum <= M)
                    break;
                sum -= arr[idx.first];
                idx.first++;
            }
            
        }
        if (sum == M) {
            count++;
            sum -= arr[idx.first];
            idx.first++;
        }
    }
    return count;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T, N, M;
    cin >> T;
    for (int i = 1; i <= T; i++) {
        cin >> N >> M;
        for (int j = 0; j < N; j++) {
            cin >> arr[j];
        }
        cout << "#" << i << " " << makePortfolio(N, M) << "\n";
    }
    return 0;
}
cs


 3. 참고




질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


 0. [c++] 백준  - 


https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWq44zy6L_kDFARG&categoryId=AWq44zy6L_kDFARG&categoryType=CODE


 1. 풀이


1) 

2)

3)



 2. 소스코드


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
106
107
108
109
110
111
112
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
 
using namespace std;
 
string Board;
int Size, Trans;
 
string conversion() {
    string ret;
    int count = 0;
    ret = Board;
    for (count; count < Trans; count++) {
        string thisTime = ret;
        for (int i = 0; i < Size; i++) {
            if (i == 0) {
                //만일 3개가 전부 같다면 B로 변환한다.
                if ((ret[0== ret[Size - 1== ret[1]) || ((ret[0!= ret[Size - 1])&&(ret[0!= ret[1])&&(ret[1!=ret[Size-1])))
                    thisTime[i] = 'B';
                else {
                    int countB, countR, countG;
                    countB = countR = countG = 0;
                    for (int j = i; j <= i + 1; j++) {
                        if (ret[j] == 'R')
                            countR++;
                        else if (ret[j] == 'G')
                            countG++;
                        else
                            countB++;
                    }
                    if (ret[Size - 1== 'R')
                        countR++;
                    else if (ret[Size - 1== 'G')
                        countG++;
                    else
                        countB++;
                    //변환해준다.
                    if ((countR == 2 && countG == 1|| (countG == 2 && countB == 1|| (countB == 2 && countR == 1))
                        thisTime[i] = 'R';
                    else
                        thisTime[i] = 'G';
                }
            }
            else if (i == Size - 1) {
                //만일 3개가 전부 같다면 B로 변환한다.
                if ((ret[0== ret[Size - 1== ret[Size - 2])||((ret[0!= ret[Size - 1])&&(ret[0!= ret[Size - 2])&&(ret[Size-1!= ret[Size-2])))
                    thisTime[i] = 'B';
                else {
                    int countB, countR, countG;
                    countB = countR = countG = 0;
                    for (int j = i - 1; j <= i; j++) {
                        if (ret[j] == 'R')
                            countR++;
                        else if (ret[j] == 'G')
                            countG++;
                        else
                            countB++;
                    }
                    if (ret[0== 'R')
                        countR++;
                    else if (ret[0== 'G')
                        countG++;
                    else
                        countB++;
                    //변환해준다.
                    if ((countR == 2 && countG == 1|| (countG == 2 && countB == 1|| (countB == 2 && countR == 1))
                        thisTime[i] = 'R';
                    else
                        thisTime[i] = 'G';
                }
            }
            else {
                //만일 3개가 전부 같다면 B로 변환한다.
                if ((ret[i] == ret[i + 1== ret[i - 1])|| ((ret[i] != ret[i + 1])&&(ret[i] != ret[i - 1])&&(ret[i-1!= ret[i+1])))
                    thisTime[i] = 'B';
                else {
                    int countB, countR, countG;
                    countB = countR = countG = 0;
                    for (int j = i - 1; j <= i + 1; j++) {
                        if (ret[j] == 'R')
                            countR++;
                        else if (ret[j] == 'G')
                            countG++;
                        else
                            countB++;
                    }
                    //변환해준다.
                    if ((countR == 2 && countG == 1|| (countG == 2 && countB == 1|| (countB == 2 && countR == 1))
                        thisTime[i] = 'R';
                    else
                        thisTime[i] = 'G';
                }
            }
 
        }
        ret = thisTime;
    }
 
    return ret;
}
 
int main() {
    int T;
    cin >> T;
    for (int i = 1; i <= T; i++) {
        cin >> Size >> Trans;
        cin >> Board;
        cout << "#" << i << " " << conversion() << endl;
    }
}
cs


 3. 참고


구종만, 「프로그래밍 대회에서 배우는 알고리즘 문제 해결 전략」, 인사이트, 2012, p.216~236.



질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


'<SW Expert Academy> > |C++| normal' 카테고리의 다른 글

[c++] sw 7812 - 옥희의 OK! 부동산  (0) 2019.06.14

+ Recent posts