Middle school math [GDOI2016]

Description [title]
junior high school on Xiao Ming, he has a very strong interest in mathematics.

After completion of a function Xiao Ming, a teacher left a word problem:

Commodity price and sales volume of commodities constitute a linear function. If the price of a commodity is \ (the p-\) , the number of people willing to buy this item as \ (the n-\) , meet between them \ (the n-= \ lfloor n_0-KP \ rfloor \) , where \ (p, k \) is a real number, \ (n-, N - 0 \) is an integer, \ (\ lfloor X \ rfloor \) represents not more than \ (X \) is the largest integer.

The cost of each product was \ (P_0 \) , the sales profit \ (the n-(the p--P_0) \) . Please select an appropriate price, making the maximum profit sales. Assuming that the number of unrestricted commodity, unsold merchandise has no effect on profits.

Xiao Ming This question is do it quickly, so the performance is very proud of his brother in front of the Ming dynasty. Ming thought, and asked Xiao Ming: "If you can set two prices \ (p_1 \) and \ (P_2 \) \ ((p_1> P_2) \) , there n_1 \ (= \ lfloor n_0- kp_1 \ rfloor \ ) people price \ (p_1 \) buy, \ (n_2 = \ lfloor n_0-kp_2 \ rfloor - n_1 \) people price \ (p_2 \) to purchase, at this time how to set these two prices makes the maximum profit? "

Xiao Ming thought, soon came up with the practice, and she ran and told my brother Ming dynasty. Ming Xiao Ming did not have time to check the practice, so to find the answers you. Can you write a program to help verify the Ming Xiao Ming answer?

[Input format
input file is only one line: an integer \ (n_0 \) and two real numbers \ (P_0 \) , \ (k \) , see the specific meaning of the title description.

[] Output format
output file line: two real numbers, respectively, to set a maximum price of profit and set two prices to maximize profits.

answer

A friendly math problem

Look at the first question in the \ (n \) under conditions of constant \ (p \) is obviously the bigger the better so \ (\ lfloor n_0-kp \ rfloor \) will be as small as possible then we can put that down rounding lost

The change to give marvelous \ (p = \ frac {n_0 -n} {k} \)

Profit is \ (Y = n (p- p_0) \) to \ (p = \ frac {n_0 -n} {k} \) is substituted, to give \ (Y = - \ frac { 1} {k} n ^ 2 + (\ frac {n0} { k} -p_0) n \) which it is clearly about \ (n-\) a quadratic function is then engaged with what can be trichotomy

The second question is assumed that \ (n_1 \) determines that the second item of the profit is also about \ (n_2 \) quadratic function and total profits and \ (n_1 \) will be a function of the relationship between a single peak is not specific certificate the so-thirds in another set of three-pointers on the line

It is said that \ (O (1) \) solution Anyway I konjac up too

Code

#include <bits/stdc++.h>
using namespace std;

int n0;
double k, p0;

inline double calc(int n2, int n1) {
    double p = (n1 - n2) * 1.0 / k;
    return n2 * (p - p0);
}

inline double solve(int n1) {
    double p = (n0 - n1) * 1.0 / k;
    int l = 1, r = n0 - n1;
    while (l < r) {
        int m1 = l + (r - l) / 3, m2 = m1 + (r - l) / 3 + 1;
        if (calc(m1, n0 - n1) < calc(m2, n0 - n1)) l = m1 + 1;
        else r = m2 - 1;
    }
    return n1 * (p - p0) + calc(l, n0 - n1);
}

int main() {
    scanf("%d %lf %lf", &n0, &p0, &k);
    printf("%.3lf ", solve(0));
    int l = 1, r = n0;
    while (l < r) {
        int m1 = l + (r - l) / 3, m2 = m1 + (r - l) / 3 + 1;
        if (solve(m1) < solve(m2)) l = m1 + 1;
        else r = m2 - 1;
    }
    printf("%.3lf", solve(l));
    return 0;
}

Guess you like

Origin www.cnblogs.com/ak-dream/p/12398636.html