POJ 1260-Pearls (DP)

Source: Internet
Author: User

POJ 1260-Pearls (DP)
Pearls

Time Limit:1000 MS Memory Limit:10000 K
Total Submissions:7465 Accepted:3695

Description

In Pearlania everybody is fond of pearls. one company, called The Royal Pearl, produces a lot of jewelry with pearls in it. the Royal Pearl has its name because it delivers to the royal family of Pearlania. but it also produces bracelets and necklaces for ordinary people. of course the quality of the pearls for these people is much lower then the quality of pearls for the royal family. in Pearlania pearls are separated into 100 different quality classes. A quality class is identified by the price for one single pearl in that quality class. this price is unique for that quality class and the price is always higher then the price for a pearl in a lower quality class.
Every month the stock manager of The Royal Pearl prepares a list with the number of pearls needed in each quality class. the pearls are bought on the local pearl market. each quality class has its own price per pearl, but for every complete deal in a certain quality class one has to pay an extra amount of money equal to ten pearls in that class. this is to prevent tourists from buying just one pearl.
Also The Royal Pearl is suffering from the slow-down of the global economy. therefore the company needs to be more efficient. the CFO (chief financial officer) has discovered that he can sometimes save money by buying pearls in a higher quality class than is actually needed. no customer will blame The Royal Pearl for putting better pearls in the bracelets, as long as
Prices remain the same.
For example 5 pearls are needed in the 10 Euro category and 100 pearls are needed in the 20 Euro category. that will normally cost: (5 + 10) * 10 + (100 + 10) * 20 = 2350 Euro. buying all 105 pearls in the 20 Euro category only costs: (5 + 100 + 10) * 20 = 2300 Euro.
The problem is that it requires a lot of computing work before the CFO knows how can pearls can best be bought in a higher quality class. you are asked to help The Royal Pearl with a computer program.

Given a list with the number of pearls and the price per pearl in different quality classes, give the lowest possible price needed to buy everything on the list. pearls can be bought in the requested, or in a higher quality class, but not in a lower one.

Input

The first line of the input contains the number of test cases. each test case starts with a line containing the number of categories c (1 <= c <= 100 ). then, c lines follow, each with two numbers ai and pi. the first of these numbers is the number of pearls ai needed in a class (1 <= ai <= 1000 ).
The second number is the price per pearl pi in that class (1 <= pi <= 1000 ). the qualities of the classes (and so the prices) are given in ascending order. all numbers in the input are integers.

Output

For each test case a single line containing a single number: the lowest possible price needed to buy everything on the list.

Sample Input

22100 1100 231 101 11100 12

Sample Output

3301344
Water DP
Question: n items, quantity and price (note that both quantity and price are in ascending order. This is the key to DP). If you want to buy a product for each type of item, the required price is (a [I] + 10) * p [I], that is, 10 more items are required, you can also combine low-price items into high-price items (a [j] + a [I] + 10) * p [j]; it means to merge the items whose price is I into j. Set dp [I] as the optimal solution for buying Category I items
dp[i]=min(dp[i],dp[j]+(a[j+1]+a[j+2]+a[i]+10)*p[i]);
That is, for category I items, it can merge up to the previous I-1 items, and because the number and price of items are in ascending order, it can prove that as long as the category j item can be combined, then it can be merged in the future, because the money saved by the merging of Category j items can be expressed as a [j] * p [I]-(a [j] + 10) * p [j]; if the preceding formula is smaller than 0, j can be merged. When j increases, the number and price increase, so j can only save more money, that is, it can be replaced.
To sum up, there are two methods:
1. The state transition equation is given above.
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
# Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            # Include
             
               # Define ll long # define maxn 116 # define pp pair
              
                # Define INF 0x3f3f3f # define max (x, y) (x)> (y ))? (X): (y) # define min (x, y) (x)> (y ))? (Y): (x) using namespace std; int n, a [110], p [110], dp [110], s [110]; int main () {int T; scanf ("% d", & T); while (T --) {scanf ("% d", & n); s [0] = 0; for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I], & p [I]); s [I] = s [I-1] + a [I];} dp [0] = 0; for (int I = 1; I <= n; I ++) {dp [I] = dp [I-1] + (a [I] + 10) * p [I]; for (int j = 0; j
               
                
2. push from the back. Because for the last state n, it is required to merge consecutively from the first n-1 state.
#include #include 
                 
                  #include 
                  
                   #include 
                   
                    #include 
                    
                     #include 
                     
                      #include 
                      
                       #include 
                       
                        #include 
                        
                         #include 
                         
                          #include 
                          
                           #include 
                           #include 
                            
                             #define ll long long#define maxn 116#define pp pair
                             
                              #define INF 0x3f3f3f3f#define max(x,y) ( ((x) > (y)) ? (x) : (y) )#define min(x,y) ( ((x) > (y)) ? (y) : (x) )using namespace std;int n,a[110],p[110],dp[110],s[110];int dfs(int x){if(x<1)return 0;if(dp[x]>=0)return dp[x];if(x==1)return dp[x]=(a[x]+10)*p[x];dp[x]=(a[x]+10)*p[x]+dfs(x-1);for(int i=x-1;i>=1;i--)dp[x]=min(dp[x],dfs(i-1)+(s[x]-s[i-1]+10)*p[x]);return dp[x];}int main(){int T;scanf("%d",&T);while (T--){s[0]=0;memset(dp,-1,sizeof(dp));scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&a[i],&p[i]);s[i]=s[i-1]+a[i];}printf("%d\n",dfs(n));}return 0;}
                             
                            
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 

Related Article

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.