Ultraviolet A -- 11054 Wine trading in Gergovia + greedy

Source: Internet
Author: User

Ultraviolet A -- 11054 Wine trading in Gergovia + greedy

Question:

Everyone in a street makes a living by selling wine. Every day, some people need to sell wine while others need to buy wine; then, it takes a unit of money to transport a unit of wine between adjacent people. Ask how to arrange transactions between people to minimize the total computing cost.

Ideas:

At first, I thought of a greedy idea: Obviously everyone who needs to buy wine should try to buy the wine he sells on the far left. If not, I will buy the wine from the wine seller on the right. The second point is very understandable. Let's explain the first point: if the current person doesn't buy the wine sold by the leftmost person, and the wine will be sold anyway, these wines will be bought by the people on the right, so the cost of selling these wines will inevitably increase.

In actual implementation, we need to determine whether there is any wine that can be purchased on the left of each person. In this way, if we directly judge through the loop, it will definitely time out (I wrote it once at the beginning, the result is TL). In fact, we can use another array to record the coordinates of all wine sellers, in this way, we only need to scan this array every time (the element starting with this array must be the coordinate of the leftmost wine buyer). If the amount of wine corresponding to the item before this array changes to 0, we will shift the start coordinate right (if this step is not completed, it will definitely time out ).

However, I just saw another way on the Internet, that is, to buy the wine of the I + 1 person without considering the trading relationship specified in the question. This is the right answer, but I still cannot figure out the principle.


The Code is as follows:


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define LL long longint main () {int I, j, k, n, a [110000], B [110000], left; while (scanf ("% d", & n) {LL sum = 0; k = 0; for (I = 0; I
     
      
0) {for (j = left; j
      
       
Code for another method:


#include
        
         #include
         
          #include
          
           #include
           
            using namespace std;#define LL long longint main(){    int i,j,k,n,a[110000],b[110000],left;    while(scanf("%d",&n)&&n)    {         LL sum=0; k=0;         for(i=0;i
            
             

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.