[Zhengqing] [1896] 985's Drink-Buying Challenge

Problem Description

985的买饮料难题
Time limit: 1 SecMemory limit: 128 MB

Description

The weather is too hot, so 985 made a plan for the future: on day i he will drink a[i] bottles of beverage. To save expenses, he found out the price p[i] per bottle on day i.
To keep his plan from falling through, he wants to know the minimum amount he must spend over these n days.
The shrewd 985 can choose to buy any number of beverages on any day, and he has a magic refrigerator that can preserve beverages bought in advance (at least they are drinkable during these n days).

Input

The first line contains an integer t, representing t test cases.
For each test case, the first line contains an integer n, the total number of planned days, followed by n lines, each containing two integers a[] and p[], which represent the information mentioned above.
Note: 1 <= t <= 30, 1 <= n <= 1000, 1 <= a[], p[] <= 100.

Output

Output an integer representing the final result.

Sample Input

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
2
2
25 56
94 17
5
39 21
95 89
73 90
9 55
85 32

Sample Output

1
2
2998
6321

Problem Analysis

The optimal unit price for buying on day i is min(p[j]) (1 <= j <= i)
so a single scan is enough.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h> 
#include<algorithm> 
int a[1200],p[1200]; 
int main() { 
    int T; 
    scanf("%d",&T); 
    while(T--) { 
        int n; 
        scanf("%d",&n); 
        for(int i=0; i<n; i++) 
            scanf("%d %d",&a[i],&p[i]); 
        int res=0; 
        for(int i=n-1; i>=0; i--) { 
            int max=p[0]; 
            for(int j=1; j<=i; j++) { 
                if(max>p[j]) 
                    max=p[j]; 
            } 
            res+=max*a[i]; 
        } 
        printf("%d\n",res); 
    } 
    return 0; 
}

Problem link: ZZULI [1896] 985’s Beverage Purchasing Problem

Loading...