아직 멀었나봅니다 ㅡ.ㅡ;;
그래프 관련 문제는 아직....너무 머리가 아파용 ㅠㅠ 문제 나오면 휴리스틱이고 다익스트라고 뭐고 다 안드로메다로..ㅠ
SRM 429번 DIV2 1000P에서....일단 파싱하고 쭉 그래프 따라간 담에 만약 방문한 노드가 아닐 경우는
따라가서 방문하면서 곱해주는 방식으로 문제를 풀었는데요...
좀 쉽게 푸는 방법 없을까용 ㅡ.ㅡ;; 너무 머리가 아팠어요.;;
그리고 약분...gcd... 흙흙...ㅠㅠㅠ 이런거 빨리 구현하는 노하우나 그런것도 있으면 좀 알려주세요~~~
역시 문제는 맨 아래에!!!!
public class IngredientProportions {
public int[][] p;
public int[] getMasses(String[] pt) {
int sz = pt.length;
int[] ret = new int[sz+1];
p = new int[sz+1][sz+1];
// 파싱
for (int i = 0; i < sz; i++) {
String str = pt[i];
int m = str.charAt(1)-'0';
int t = str.charAt(8)-'0';
int a = str.charAt(13)-'0';
int b = str.charAt(15)-'0';
p[m][t] = a;
p[t][m] = b;
}
for (int i = 0; i < sz+1; i++) {
boolean[] visited = new boolean[sz+1];
for (int j = 0; j < sz+1; j++)
visited[j] = false;
visited[i] = true;
ret[i] = visit(visited, i);
}
return yb(ret);
}
public int visit(boolean[] v, int i) {
boolean changed = false;
int j = 1;
for (int k = 0; k < v.length; k++) {
if (!v[k] && p[i][k] > 0) {
changed = true;
v[k] = true;
j *= p[i][k] * visit(v, k);
}
}
if (changed) return j;
return 1;
}
public int gcd(int a, int b)
{
if( a == 0 ) return b;
while( b > 0 )
{
if( a > b ) a = a-b;
else b = b-a;
}
return a;
}
// 약분하기 ㅡ.ㅡ;
public int[] yb(int[] a) {
int gb = a[0];
for (int i = 1; i < a.length; i++) {
gb = gcd(gb, a[i]);
}
for (int i = 0; i < a.length; i++) {
a[i] /= gb;
}
return a;
}
}
<출처 : TopCoder SRM>
Problem Statement
Your friend has invented a splendid cocktail consisting of N ingredients. However, she has forgotten the amount of each ingredient that goes into the recipe.
For N-1 pairs of ingredients, she remembers the proportion in which the ingredients within each pair should be added to the cocktail. Fortunately, these N-1 proportions are sufficient to restore the recipe of the entire cocktail.
You are given a String[] proportions containing the N-1 proportions. Each element is formatted "#<a> and #<b> as <p>:<q>" (quotes for clarity), which means that the mass of ingredient <a> divided by the mass of ingredient <b> in the cocktail must be equal to <p>/<q> (all ingredients are 0-indexed). Return a int[] containing exactly N elements, where the i-th element is the mass of ingredient i, such that all the given proportions are satisfied and the total mass is as small as possible. The total mass must be greater than 0.
Definition
Class:
IngredientProportions
Method:
getMasses
Parameters:
String[]
Returns:
int[]
Method signature:
int[] getMasses(String[] proportions)
(be sure your method is public)
Constraints
proportions will contain between 1 and 9 elements, inclusive.
proportions will contain exactly N-1 elements, where N is the number of ingredients in the cocktail.
Each element of proportions will contain exactly 16 characters.
Each element of proportions will be formatted as described in the statement.
The information given in proportions will be sufficient to restore the recipe of the cocktail uniquely up to a constant factor.
Examples
0)
{"#0 and #1 as 6:4"}
Returns: {3, 2 }
Taking 6 units of ingredient #0 and 4 units of ingredient #1 would satisfy the proportion, but it wouldn't give the smallest possible total mass. To minimize the total mass, divide the masses by 2.
1)
{"#0 and #1 as 9:8","#1 and #2 as 9:8"}
Returns: {81, 72, 64 }
2)
{"#4 and #0 as 1:1", "#4 and #1 as 3:1", "#4 and #2 as 5:1", "#4 and #3 as 7:1"}
Returns: {105, 35, 21, 15, 105 }
The mass of ingredient #4 should be divisible by 3, 5 and 7. The smallest such number is 105.
3)
{"#2 and #3 as 6:8", "#0 and #1 as 9:3", "#3 and #0 as 7:5"}
Returns: {60, 20, 63, 84 }
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
gcd는 위와 같은 방법보다는
int gcd(int a, int b)
{
if (a == 0 || b == 0) return a + b;
int c = a % b;
while (c) { a = b, b = c, c = a % b; }
return b;
}
정도로 구현하시거나 __gcd(a, b) 를 사용하시면 될 듯 싶군요 :$
UNKI
아직 멀었나봅니다 ㅡ.ㅡ;;
그래프 관련 문제는 아직....너무 머리가 아파용 ㅠㅠ 문제 나오면 휴리스틱이고 다익스트라고 뭐고 다 안드로메다로..ㅠ
SRM 429번 DIV2 1000P에서....일단 파싱하고 쭉 그래프 따라간 담에 만약 방문한 노드가 아닐 경우는
따라가서 방문하면서 곱해주는 방식으로 문제를 풀었는데요...
좀 쉽게 푸는 방법 없을까용 ㅡ.ㅡ;; 너무 머리가 아팠어요.;;
그리고 약분...gcd... 흙흙...ㅠㅠㅠ 이런거 빨리 구현하는 노하우나 그런것도 있으면 좀 알려주세요~~~
역시 문제는 맨 아래에!!!!
public class IngredientProportions {
public int[][] p;
public int[] getMasses(String[] pt) {
int sz = pt.length;
int[] ret = new int[sz+1];
p = new int[sz+1][sz+1];
// 파싱
for (int i = 0; i < sz; i++) {
String str = pt[i];
int m = str.charAt(1)-'0';
int t = str.charAt(8)-'0';
int a = str.charAt(13)-'0';
int b = str.charAt(15)-'0';
p[m][t] = a;
p[t][m] = b;
}
for (int i = 0; i < sz+1; i++) {
boolean[] visited = new boolean[sz+1];
for (int j = 0; j < sz+1; j++)
visited[j] = false;
visited[i] = true;
ret[i] = visit(visited, i);
}
return yb(ret);
}
public int visit(boolean[] v, int i) {
boolean changed = false;
int j = 1;
for (int k = 0; k < v.length; k++) {
if (!v[k] && p[i][k] > 0) {
changed = true;
v[k] = true;
j *= p[i][k] * visit(v, k);
}
}
if (changed) return j;
return 1;
}
public int gcd(int a, int b)
{
if( a == 0 ) return b;
while( b > 0 )
{
if( a > b ) a = a-b;
else b = b-a;
}
return a;
}
// 약분하기 ㅡ.ㅡ;
public int[] yb(int[] a) {
int gb = a[0];
for (int i = 1; i < a.length; i++) {
gb = gcd(gb, a[i]);
}
for (int i = 0; i < a.length; i++) {
a[i] /= gb;
}
return a;
}
}
<출처 : TopCoder SRM>
Problem Statement
Your friend has invented a splendid cocktail consisting of N ingredients. However, she has forgotten the amount of each ingredient that goes into the recipe. For N-1 pairs of ingredients, she remembers the proportion in which the ingredients within each pair should be added to the cocktail. Fortunately, these N-1 proportions are sufficient to restore the recipe of the entire cocktail. You are given a String[] proportions containing the N-1 proportions. Each element is formatted "#<a> and #<b> as <p>:<q>" (quotes for clarity), which means that the mass of ingredient <a> divided by the mass of ingredient <b> in the cocktail must be equal to <p>/<q> (all ingredients are 0-indexed). Return a int[] containing exactly N elements, where the i-th element is the mass of ingredient i, such that all the given proportions are satisfied and the total mass is as small as possible. The total mass must be greater than 0.
Definition
Constraints
proportions will contain between 1 and 9 elements, inclusive.
proportions will contain exactly N-1 elements, where N is the number of ingredients in the cocktail.
Each element of proportions will contain exactly 16 characters.
Each element of proportions will be formatted as described in the statement.
Each will be between 0 and N-1, inclusive.
Each will be between 0 and N-1, inclusive.
Each
will be between 1 and 9, inclusive.
Each
The information given in proportions will be sufficient to restore the recipe of the cocktail uniquely up to a constant factor.
Examples
0)
Returns: {3, 2 }
1)
Returns: {81, 72, 64 }
2)
Returns: {105, 35, 21, 15, 105 }
3)
Returns: {60, 20, 63, 84 }
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
15년 전
6개의 댓글이 있습니다.
astein
gcd는 위와 같은 방법보다는
int gcd(int a, int b)
{
if (a == 0 || b == 0) return a + b;
int c = a % b;
while (c) { a = b, b = c, c = a % b; }
return b;
}
정도로 구현하시거나 __gcd(a, b) 를 사용하시면 될 듯 싶군요 :$
15년 전 link
UNKI
역시 C++이 간결하네요 :(
15년 전 link
nocut98
저 문제 깔끔하게 잘 안되던데, 두시간만에 푸신 거면 잘 푸신듯...
15년 전 link
UNKI
저도 잘 나올까..?? 하고 서밋 했는데 통과하더라고요;;
15년 전 link
wsong
Topcoder에서
Algorithm부문 에서 C++이나 C로 작성할 수 있나요?
development부문에 가보면 Java나 .Net만 쓸 수 있는거 같던데요.
15년 전 link
음매~@
default는 java로 되어 있지만 우측 상단에 보시면 Language를 선택하실 수 있습니다. C는 없고 C++는 있습니다.
15년 전 link
정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.