gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
다음 문제를 해결하는 C++ 프로그램을 작성하려고 합니다. [code] 다음과 같은 코드를 작성하였습니다. [code] 그런데 다음과 같은 입력에서 시간이…
- User
다음 문제를 해결하는 C++ 프로그램을 작성하려고 합니다.
문제 George has won the lottery and, being a nice guy, has decided to spread the wealth around. However, monetary gifts can be taxed once they get over a certain size—the amount of tax depends on how much his friends have earned that year. The amount of tax paid depends on tax bands. The bands start at zero. Each one takes a certain cut of income from the range of pre-tax income it covers. The final tax band applies to all income above its lower bound. George is a savvy fellow and knows the number of tax bands, the amount of money each friend has earned and the amount he wants each friend to walk away with. How much should George give to each friend before tax? 입력 One line containing an integer B (1 ≤ B ≤ 20): the number of tax bands. B further lines, each containing two real numbers: si (0 < si ≤ 106): the size in pounds of the ith tax band, and pi (0 ≤ pi ≤ 100): the percentage taxation for that band. One line containing a real number P (0 ≤ P ≤ 99.999): the percentage tax on all income above other bands. One line containing an integer F, (0 < F ≤ 20): the number of friends George wants to pay. F further lines, each containing two real numbers ej and mj (0 ≤ ej ≤ 106, 0 < mj ≤ 106): the amount of money the jth friend has earned, and the amount of money they should receive after tax respectively. Tax bands will be given in increasing order. 출력 F lines, one for each friend specified in the input and in the same order. Each line should contain one real number: the amount of money George will give to his friend, in order to arrive at the correct amount after they have paid tax. All output must be accurate to an absolute or relative error of at most 10−6.다음과 같은 코드를 작성하였습니다.
#include <bits/stdc++.h> using namespace std; double s[21], p[21]; double P; int B, F; double calc(double n) { double tax = 0.0; for (int i = 0; i < B; i++) { if (n <= s[i]) { tax += (n * p[i] / 100); return tax; } else { tax += (s[i] * p[i] / 100); n -= s[i]; } } if (n > 0) tax += (n * P / 100); return tax; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> B; for (int i = 0; i < B; i++) cin >> s[i] >> p[i]; cin >> P; cin >> F; for (int i = 0; i < F; i++) { double e, m; cin >> e >> m; double tax = calc(e); double st = 0, en = e * 1e6; while (en - st > 1e-6) { double mid = (st + en) / 2; if (mid + tax - calc(e + mid) < m) { st = mid; } else { en = mid; } } cout.precision(6); cout << fixed << st << '\n'; } return 0; }그런데 다음과 같은 입력에서 시간이 매우 오래 걸리는 문제가 발생합니다.
20 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 1000000.000000 100 99.999 20 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 1000000.000000 - Assistant
시간이 오래 걸리는 문제는 이분 탐색 범위가 너무 넓기 때문입니다. 현재 코드에서 이분 탐색의 우측 경계가
e * 1e6으로 설정되어 있는데, 이것은 매우 큰 값이며 따라서 시간이 너무 오래 걸릴 수 있습니다. 문제 조건에 맞게 탐색 범위를 조절함으로서 이를 해결할 수 있습니다.크게 두 가지 부분에서 개선이 필요합니다:
-
이분 탐색 범위 조정: 주어진 소득
e와 원하는 소득m과의 차이를 고려하여 이분 탐색의 우측 경계를 더 현실적인 값으로 줄일 필요가 있습니다.m보다 크게 받을 수 없기 때문에 탐색 범위를0부터m-e로 설정해 볼 수 있습니다. -
초기 세금 계산량 최적화: 매 반복마다 세금 계산을 하는 것보다 함수 호출을 줄이고 일정 부분을 먼저 계산해 저장함으로써 연산량을 줄일 수 있습니다.
다음은 수정된 코드입니다:
#include <bits/stdc++.h> using namespace std; double s[21], p[21]; double P; int B, F; double calc(double n) { double tax = 0.0; for (int i = 0; i < B; i++) { if (n <= s[i]) { tax += (n * p[i] / 100); return tax; } else { tax += (s[i] * p[i] / 100); n -= s[i]; } } if (n > 0) tax += (n * P / 100); return tax; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> B; for (int i = 0; i < B; i++) cin >> s[i] >> p[i]; cin >> P; cin >> F; for (int i = 0; i < F; i++) { double e, m; cin >> e >> m; // 초기 소득 세금 계산 double initialTax = calc(e); // 이분 탐색의 범위를 0부터 (m - e)로 설정 double st = 0, en = m; while (en - st > 1e-6) { double mid = (st + en) / 2; if (mid + e - calc(e + mid) - initialTax < m) { st = mid; } else { en = mid; } } cout.precision(6); cout << fixed << st << '\n'; } return 0; } -