Ural 1200. Horns and hoofs enumeration + mathematics

Source: Internet
Author: User

More and more things are exposed, but they are not flexible enough. In the past, all such questions were violent, enumerative, and two for loops, today, when I saw it, I went on to think that the violence would time out, but several people had already passed 200 + Ms. I have been deriving it, proving that the more difficult it was to push, I have not made a question. In fact, this question is very simple.Algorithm), A for loop, enumerative the number of A, and then according to profit = A * A + B * B-A * A-B * B, is the quadratic function of y, based on the maximum value of Image B = B/2 and then the axis, the two sides decrease progressively.

 
# Include <cstdio> # include <iostream> # include <cstring> # include <cstdlib> using namespace STD; int main () {// freopen ("in.txt ", "r", stdin); Double A, B, Maxx; int K, a, B; scanf ("% lf % d", & A, & B, & K); Maxx =-100000; a = 0; B = 0; For (INT I = 0; I <= K; I ++) {int y = B/2; double temp; If (Y + I> K) y = k-I; If (Y <0) y = 0; temp = A * I + B * Y-I * I-y * Y; If (temp> Maxx) {Maxx = temp; A = I; B = y ;}} printf ("%. 2lf \ n % d \ n ", Maxx, a, B); Return 0 ;}

The algorithm of pure brute-force enumeration.

 
# Include <cstdio> # include <iostream> # include <cstring> # include <cstdlib> using namespace STD; int main () {// freopen ("in.txt ", "r", stdin); Double A, B, Maxx; int A = 0, B = 0, K; Maxx =-100000; scanf ("% lf % d", & A, & B, & K); For (INT I = 0; I <= K; I ++) {for (Int J = 0; j + I <= K; j ++) {double temp = A * I + B * j-I * I-j * j; if (temp> Maxx) {Maxx = temp; A = I; B = J ;}} printf ("%. 2lf \ n % d \ n ", Maxx, a, B); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.