# "Reasoning, greedy" UVa 1319-maximum

Source: Internet
Author: User

See the Code of the Great God. Understand for a long time ... It's really a gap.

Test instructions: Given m, p, a, B, then XI satisfies the two formula, and the maximum value of xp1 + xp2 +...+ xPM is obtained.

1, -1/sqrt (a) <= XI <= sqrt (a); (a>0)

2, X1+X2+...+XM = B*sqrt (a);

Note: P is even.

Problem solving: Because P is even, the P-sqrt (a) and -1/sqrt (a) are positive and sqrt (a) > 1/sqrt (a). So greedy thought when try to take sqrt (a); When the sum of the XI is already taken and greater than B*SQRT (a), then fetch ( -1/ sqrt (a)) offset the extra sqrt (a).

To simplify the calculation and reduce the error caused by sqrt (). The 2 can now be multiplied by sqrt (a).

`1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 intMain () {7     intm, p, a, B;8      while(~SCANF ("%d%d%d%d", &m, &p, &a, &b)) {9         intL =0, r =0;//L, R represents the number of -1/sqrt (a) and sqrt (a), respectivelyTen         intsum = A *b; One          for(inti =1; I < m; i++) {//Note that the last number is not considered A             if(Sum >=a) { -r++; -Sum-= A;//after Xi has taken sqrt (a), sum remembers to update the             } -             Else { -l++; -sum++; +             } -         } +         DoubleAns = L/pow (sqrt (a), p) + R *Pow (sqrt (a), p); AAns + = POW (sum/sqrt (a), p);//Remember to remove the sqrt (a); atprintf"%d\n", (int) (ans +0.5)); -     } -     return 0; -}`

"Reasoning, greedy" UVa 1319-maximum

Related Keywords:

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.