<7-3> DP - 나쁜 이웃집 사람들 (BadNeighbors)
PS/Topcoder training2018. 7. 1. 12:37
### DP ###
### 2004 TCCC Online Round 4 Div 1 Level 1 ###
<소스코드>
- d[i][j][k] : i번째 차례, j == 0 이면 이전 이웃에게 기부를 받음, k == 1 이면 첫번째 집에서 기부를 받음
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; int d[41][2][2]; class BadNeighbors { public: int n; int donation(vector<int> &donations, int i, bool prev, bool first) { int *ret = &d[i][prev][first]; if(first && i == n-1) return 0; if(!first && i == n) return 0; if(*ret) return *ret; *ret = donation(donations, i+1, 0, first); if(!prev) *ret = max(*ret, donation(donations, i+1, 1, first)+donations[i]); return *ret; } int maxDonations(vector<int> donations) { n = donations.size(); int ans = donation(donations, 1, 1, 1)+donations[0]; return max(ans, donation(donations, 1, 0, 0)); } }; | cs |
>> first 가 1 이면 마지막 집은 n-2 번째 집이 되고, first 가 0이면 마지막 집은 n-1번째 집이 된다. (첫번째 집이 0번째 집이라고 생각)
<해설 소스코드>
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 | #include <vector> using namespace std; class BadNeighbors { public: int maxDonations(vector<int> donations) { int i; int ans = 0; int N = donations.size(); int *dp = new int[N]; for(i=0; i<N-1; i++) { dp[i] = donations[i]; if(i > 0) dp[i] = max(dp[i], dp[i-1]); if(i > 1) dp[i] = max(dp[i], dp[i-2]+donations[i]); ans = max(ans, dp[i]); } for(i=0; i<N-1; i++) { dp[i] = donations[i+1]; if(i > 0) dp[i] = max(dp[i], dp[i-1]); if(i > 1) dp[i] = max(dp[i], dp[i-2]+donations[i+1]); ans = max(ans, dp[i]); } delete []dp; return ans; } }; | cs |
'PS > Topcoder training' 카테고리의 다른 글
<7-5> DP - 악수 (HandShaking) (0) | 2018.07.04 |
---|---|
<5-7> 전체탐색 - 고장난 로봇 (CrazyBot) (0) | 2018.07.03 |
<5-4> 전체탐색 - 회문 (ThePalindrome) (0) | 2018.07.01 |
<7-4> DP - 킹 나이트 체스 (ChessMetric) (0) | 2018.07.01 |
<7-2> DP - 회사 조직과 급여 (CorporationSalary) (0) | 2018.07.01 |
<7-2> DP - 회사 조직과 급여 (CorporationSalary)
PS/Topcoder training2018. 7. 1. 11:06
### DP ###
### SRM407 Div2 Level 2 ###
<소스코드>
- d[i] : i번 매니저의 부하직원들에 대한 salary의 합.
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 | #include <vector> #include <algorithm> using namespace std; class CorporationSalary { public: vector<int> child[51]; long long d[51]; long long salary(int k) { if(child[k].size() == 0) return 1; if(d[k]) return d[i]; long long ret = 0LL; for(int i=0; i<child[k].size(); i++) ret += salary(child[k][i]); return d[k] = ret; } long long totalSalary(vector<string> relations) { for(int i=0; i<relations.size(); i++) { d[i] = 0; for(int j=0; j<relations[i].size(); j++) { if(relations[i][j] == 'Y') child[i].push_back(j); } } long long ans = 0LL; for(int i=0; i<relations.size(); i++) ans += salary(i); return ans; } }; | cs |
'PS > Topcoder training' 카테고리의 다른 글
<7-5> DP - 악수 (HandShaking) (0) | 2018.07.04 |
---|---|
<5-7> 전체탐색 - 고장난 로봇 (CrazyBot) (0) | 2018.07.03 |
<5-4> 전체탐색 - 회문 (ThePalindrome) (0) | 2018.07.01 |
<7-4> DP - 킹 나이트 체스 (ChessMetric) (0) | 2018.07.01 |
<7-3> DP - 나쁜 이웃집 사람들 (BadNeighbors) (0) | 2018.07.01 |
[POJ/2431] Expedition
PS/etc.2018. 6. 27. 10:38
[2431] Expedition : http://poj.org/problem?id=2431
### Priority Queue ###
<소스코드>
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 | #include <cstdio> #include <vector> #include <queue> #include <algorithm> using namespace std; struct Exp { int dist, fuel; }; int n, L, P; bool cmp(Exp a, Exp b) { return L-a.dist < L-b.dist; } int main() { scanf("%d", &n); vector<Exp> v; for(int i=0; i<n; i++) { int d, f; scanf("%d%d", &d, &f); v.push_back((Exp){d, f}); } scanf("%d%d", &L, &P); sort(v.begin(), v.end(), cmp); int ans=0; int curL = P; priority_queue<int> pq; v.push_back((Exp){0, 0}); for(int i=0; i<=n; i++) { while(L-v[i].dist > curL && !pq.empty()) { curL += pq.top(); pq.pop(); ans++; } if(curL < L-v[i].dist) break; pq.push(v[i].fuel); } if(curL < L) ans = -1; printf("%d\n", ans); return 0; } | cs |
>> 34 번 라인이 없으면 틀렸다고 뜬다.
'PS > etc.' 카테고리의 다른 글
[POJ/1182] 먹이 사슬 (0) | 2018.07.25 |
---|---|
[카카오 코드 / 예선] 카카오프렌즈 컬러링북 (0) | 2018.05.04 |
<3-1> 나열하기 - 경우의 수 (0) | 2018.03.06 |