Hdu 4296 Buildings greedy algorithm today's first a detailed analysis, some data types preferably with a long long

Source: Internet
Author: User

BuildingsTime limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2278 Accepted Submission (s): 870


Problem Description has you ever heard the stories of Blue.mary, the great civil engineer? Unlike Mr Wolowitz, Dr. Blue.mary has accomplished many great projects, one of the which is the Guanghua Building.
The public opinion are that Guanghua Building are nothing more than one of hundreds's modern skyscrapers recently built in Shanghai, and sadly, they is all wrong. Blue.mary The Great civil engineer had try a completely new evolutionary building method in project of Guanghua building. That's, to build all the floors at first, then stack them up forming a complete building.
Believe it or not, he does it (in secret manner). Now is the face of the same problem blue.mary once stuck in:place floors in a good the.
Each floor have its own weight wi and strength Si. When floors is stacked up, each of the floor have PDV (potential Damage Value) equal to (ΣWJ)-si, where (ΣWJ) stands for sum of We Ight of all floors above.
Blue.mary, the Great civil engineer, would as-minimize PDV of the whole building, denoted as the largest PDV of all F Loors.
Now, it's up to the calculate this value.

Input there ' re several test cases.
In the all test case, the first line is a single integer n (1 <= n <=) denoting the number of building ' s floors. The following N lines specify the floors. Each of them contains-integers wi and Si (0 <= wi, si <= 100000) separated by single spaces.
Please process until EOF (End of File).

Output for each test case, your program should output a single integer in a single line-the minimal PDV of the whole BU Ilding.
If no floor would are damaged in a optimal configuration (so is, minimal PDV are non-positive) you should output 0.

Sample Input
310 62 35 422 22 2310 32 53 3

Sample Output
102
This problem is simple greed, the first analysis of the meaning of the problem is that there are n blocks, one plate of the PDV = on it all the board weight-the strength of the board. Then find out the maximum value of the PDV of these plates and ask you how to make this maximum minimum.
Analysis: We can write down the formula,pdv[i]=sum (W[i+1]->w[n])-s[i], this equation is equivalent topdv[i]=sum (W[i]->w[n])-w[i]-s[i]; Shut up PDV minimum, even if it is necessaryW[i]+s[i]To reach the maximum, that's fine, we just have to sort by the w[i]+s[i] and then find the biggest PDV. Complexity O (n*logn+n); N*logn is the sort complexity, I use the sort order in STL. Another thing is that some data types need to be long long, at least my program needs. Code:
#include <cstdio> #include <cstring> #include <climits> #include <algorithm> #define MAX 100100using namespace std; typedef long long ll; struct Floor{int W, s;} F[max];bool cmp (const floor &a, const floor &b) {return A.W+A.S>B.W+B.S;} int main () {int n; ~scanf ("%d", &n) {ll sum = 0; for (int i = 0; i < n; ++i) {scanf ("%d%d", &f[i].w,&f[i ].S); sum + = F[I].W;}  Sort (f,f+n,cmp); ll PDV = llong_min; for (int i = 0; i < n; ++i) {sum-= F[I].W; if (SUM-F[I].S>PDV) {PDV = Sum-f[i].s ;}} if (pdv<=0) {puts ("0");} else{printf ("%i64d\n", PDV);}} return 0;}
with June

Hdu 4296 Buildings greedy algorithm today's first a detailed analysis, some data types preferably with a long long

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.